How to catch this?

M

MN

I wrote a function that do some some computation and returns result as
a char* [5] type. After, I used this function in other program that
executes the same function 10 times, then store each result in each
array's element with type char* big_array[5], so the total length of
big_array must be (5+1)*10, where 1 bit is added to store the '\0'
character .

To show result I used printf (see code 1) and I noticed that if I use
printf in the loop, I see that at each iteration the result changes,
but if I use printf out of loop (see code 2) , I see that only the
last result is shown at each iteration. This mean that at each
iteration the new value given by function erases the old result.
How to avoid this?

My code is like this

/************ code 1 *********************/
char* function (int in1, int in2)
{
static char array [5];

/*do some computation*/

return(array);
}

int main ()
{
int i = 0;
char* big_array [(5+1)*10];
for (i = 0; i<6*9; i=i+6)
{
big_array [i+6] = function( i , 2);

printf("%s\n",big_array [i+6]);
}
return 0;
}

/************** End code 1 *****************/


/************ code 2 *********************/
char* function (int in1, int in2)
{
static char array [5];

/*do some computation*/

return(array);
}

int main ()
{
int i = 0;
int j = 0;
char* big_array [(5+1)*10];
for (i = 0; i<6*9; i=i+6)
{
big_array [i+6] = function( i , 2);
}

for (j=0;j<6*9; j=j+6)
{
printf("%s\n",big_array [j+6]);
}

return 0;
}

/************** End code 2 *****************/
 
A

Andrew Poelstra

To show result I used printf (see code 1) and I noticed that if I use
printf in the loop, I see that at each iteration the result changes,
but if I use printf out of loop (see code 2) , I see that only the
last result is shown at each iteration. This mean that at each
iteration the new value given by function erases the old result.
How to avoid this?

My code is like this

/************ code 1 *********************/
char* function (int in1, int in2)
{
static char array [5];

/*do some computation*/

return(array);
}

<snip>

You are returning the address of a static char[5]. Because it is static,
there is only one instance, that is changed every time the function is
called.

So all pointers you assign with the return value of this function will
point to the same place, and therefore doing printf() on them will return
the same value. Does that make sense?

Now, the reason you saw different "correct" results when your printf()
was inside a loop immediately after the call to function() was that
each time your array changed, it was printed before it was changed
again.

HTH.
 
U

Ulrich Eckhardt

MN said:
I wrote a function that do some some computation and returns result as
a char* [5] type. After, I used this function in other program that
executes the same function 10 times, then store each result [...] [...]
char* function (int in1, int in2)
{
static char array [5]; [..]
return(array);
}

The 'array' inside 'function' is a single object with static duration, i.e.
there will always be only one of it. Calling the function multiple times
will always yield a pointer to the same object, so if you want to store
multiple results, you to make a copy. Otherwise, the next call to the
function will overwrite the former results.

Notes:
- 'return' is not a function, drop those brackets.
- Avoid such uses of function-static objects. They seem convenient, but
they are also dangerous as you see yourself. If you one day make the
program multithreaded, this is far out anyway.
- You could also provide a pointer to where the function should write the
results instead of returning, this is probably the most flexible
alternative.

In general, btw, it is a good idea to document who owns things when passing
them between functions. In this case, it should be documented that the
memory is static, i.e. not owned by the caller. Usually, a called function
only inspects or modifies the data when it receives a pointer or handle. In
other cases, it is the caller's or callee's responsibility to release the
resource. In any case, ownership is something useful to be aware of.

Uli
 
S

santosh

MN said:
I understand What you said, but I don't know how to store data before
it gets changed.

One way is to copy the array in the caller before calling function()
again.
 
J

Jens Thoms Toerring

MN said:
I wrote a function that do some some computation and returns result as
a char* [5] type. After, I used this function in other program that
executes the same function 10 times, then store each result in each
array's element with type char* big_array[5], so the total length of
big_array must be (5+1)*10, where 1 bit is added to store the '\0'
character .
To show result I used printf (see code 1) and I noticed that if I use
printf in the loop, I see that at each iteration the result changes,
but if I use printf out of loop (see code 2) , I see that only the
last result is shown at each iteration. This mean that at each
iteration the new value given by function erases the old result.
How to avoid this?
My code is like this
/************ code 1 *********************/
char* function (int in1, int in2)
{
static char array [5];
/*do some computation*/
return(array);
}

You have already been told about the problems when you return a
pointer to a static array, i.e. that each invokation of the
function returns the same pointer and the elements of the array
get overwritten each time, so you need to make a copy of its
content before you call the function again.
int main ()
{
int i = 0;
char* big_array [(5+1)*10];
for (i = 0; i<6*9; i=i+6)

This looks a bit strange. You ask for an array of 60 pointers to
char but you use only every 6th of them. That together with the
length of the array returned by function() makes it look a bit
as if you actually wanted an array of 60 chars (but you need
only 50 for storing 10 strings returned by function()) and copy
what you get returned by function() into it.

Perhaps what you actually set out to to do is

char big_array[ 10 * 5 ]; /* get a char array with 50 elements */

/* Get 10 strings (of maximum length 5) from function() and
copy them into big_array */

for ( i = 0; i < 10 * 5; i += 5 )
strcpy( &big_array[ i ], function( i, 2 );

for ( i = 0; i < 10 * 5 ; i += 5 )
printf( "%s\n", big_array + i );

return 0;
}

Note that '&big_array[ i ]' and 'big_array + i' are the same, both
being a pointer to the i-th element of 'big_array'.

Regards, Jens
 

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,777
Messages
2,569,604
Members
45,229
Latest member
GloryAngul

Latest Threads

Top