help need for beginner

P

Pasacco

dear

I want to ask help on this problem.
Array a[N] is partitioned into a0[N/2] and a1[N/2] in main(). Then
a1[N/2] is partitioned into a2[N/4] and a3[N/4] in th_partition()
function. And I think this problem is something about parameter
passing.
If someone give me comment, it will be thankful.

thankyou very much


/***********************************************/
#include <stdio.h>

void* th_partition(void*);

#define N 20 // number of elements

struct tag{
int *a0_temp; // temporary array
int *a1_temp;
int *a2_temp;
int *a3_temp;

int *a0_N; // number of elements for each array
int *a1_N;
int *a2_N;
int *a3_N;
}arrays;

int main(int argc, char* argv[])
{
int i,j,*pt;
int a[N];

// Initial array (N elements)
for(i=0;i<N;i++)
a = i;

pt = a;

/* partition of a[] ==> a0[] and a1[] */
/* j : position in the middle */

j = N / 2;

int tmp0[j];
int tmp1[N-j];
arrays.a0_N = (void*) (j);
arrays.a1_N = (void*) (N-j);

for(i=0;i<j;i++)
tmp0=a;

for(i=j;i<N;i++)
tmp1[i-j]=a;

arrays.a0_temp = tmp0;
arrays.a1_temp = tmp1;

for(i=0;i<j;i++)
printf(" %d ",arrays.a0_temp);
printf("\n");

for(i=0;i<N-j;i++)
printf(" %d ",arrays.a1_temp);
printf("\n");
th_partition(&arrays);

// Test ===> strange !!
int test = (int)arrays.a2_N;
for(i=0;i<test;i++)
printf(" %d ", arrays.a2_temp);
printf("\n");
}

/* Partition of a1[] ==> a2[] and a3[] */
void* th_partition(void* parameters)
{
int i,j,M;
struct tag* para = (struct tag*)parameters;

M = (int) para->a1_N;
j = M / 2;

int tmp2[j];
int tmp3[M-j];
para->a2_N = (void* )(j );
para->a3_N = (void* )(M-j);

for(i=0;i<j;i++)
tmp2=para->a1_temp;

for(i=j;i<M;i++)
tmp3[i-j]=para->a1_temp;

para->a2_temp = tmp2;

for(i=0;i<j;i++)
printf(" %d ",para->a2_temp);
printf("\n");

para->a3_temp = tmp3;

for(i=0;i<M-j;i++)
printf(" %d ",para->a3_temp);
printf(" \n ");
}
 
M

Michael Mair

Pasacco said:
dear

I want to ask help on this problem.
Array a[N] is partitioned into a0[N/2] and a1[N/2] in main(). Then
a1[N/2] is partitioned into a2[N/4] and a3[N/4] in th_partition()
function. And I think this problem is something about parameter
passing.

Your problem is not about parameter passing, it is about
storage duration.
The variables declared within th_partition() live only until
the end of th_partition. So, accessing or trying to access
these temporary arrays after they ceased to exist invokes
undefined behaviour.

If you really want to double the information, you need
dynamic memory allocation.
If someone give me comment, it will be thankful.

thankyou very much


/***********************************************/
#include <stdio.h>
For dynamic memory allocation, you need to
#include said:
void* th_partition(void*);

#define N 20 // number of elements

struct tag{
int *a0_temp; // temporary array
int *a1_temp;
int *a2_temp;
int *a3_temp;

int *a0_N; // number of elements for each array
int *a1_N;
int *a2_N;
int *a3_N;

Your comment is misleading: These are addresses of numbers of
elements.
You probably want int a0_N; and so on.
}arrays;

int main(int argc, char* argv[])
{
int i,j,*pt;
int a[N];

// Initial array (N elements)
for(i=0;i<N;i++)
a = i;

pt = a;

/* partition of a[] ==> a0[] and a1[] */
/* j : position in the middle */

j = N / 2;

int tmp0[j];
int tmp1[N-j];


Note: This works only in C99.
arrays.a0_N = (void*) (j);
arrays.a1_N = (void*) (N-j);

This is complete crap. j and N-j are integer values which you
convert into void *, i.e. an address. Afterwards, they are implicitly
converted into int * (the type of .a0_N and .a1_N).
You want "int a0_N" and "arrays.a0_N = j"...
for(i=0;i<j;i++)
tmp0=a;

for(i=j;i<N;i++)
tmp1[i-j]=a;

arrays.a0_temp = tmp0;
arrays.a1_temp = tmp1;

for(i=0;i<j;i++)
printf(" %d ",arrays.a0_temp);
printf("\n");

for(i=0;i<N-j;i++)
printf(" %d ",arrays.a1_temp);
printf("\n");
th_partition(&arrays);


You fail to check the return value of th_partition().
// Test ===> strange !!
int test = (int)arrays.a2_N;
for(i=0;i<test;i++)
printf(" %d ", arrays.a2_temp);
printf("\n");


With dynamic memory allocation, you would have to
free(arrays.a2_temp);
free(arrays.a3_temp);

It is better to
return 0;
}

/* Partition of a1[] ==> a2[] and a3[] */
void* th_partition(void* parameters)
{
int i,j,M;
struct tag* para = (struct tag*)parameters;

M = (int) para->a1_N;
j = M / 2;

int tmp2[j];

tmp2 lives from here till the end of the function
int tmp3[M-j];

tmp3 lives from here till the end of the function

Dynamic memory allocation would give you storage which lives
on until free()d:
int *tmp2, *tmp3;
tmp2 = malloc(j * sizeof *tmp2);
tmp3 = malloc((M-j) * sizeof *tmp3);
if (tmp2 == NULL || tmp3 == NULL) {
/* Handle error and return NULL or abort */
}
para->a2_N = (void* )(j );
para->a3_N = (void* )(M-j);

As above: Crap.
for(i=0;i<j;i++)
tmp2=para->a1_temp;

for(i=j;i<M;i++)
tmp3[i-j]=para->a1_temp;

para->a2_temp = tmp2;

for(i=0;i<j;i++)
printf(" %d ",para->a2_temp);
printf("\n");

para->a3_temp = tmp3;

for(i=0;i<M-j;i++)
printf(" %d ",para->a3_temp);
printf(" \n ");


Your compiler should complain here:
You claim that you return void * but you don't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top