|
From: David H. <dw...@ov...> - 2004-04-30 17:22:28
|
> His response was that it looked like there were two char[3]
> arrays, but the last element of the array was unitialised,
> causing the valgrind error. He believed that compilers
> should normally initialize a character array to
> binary zero and so it shouldn't be a problem.
In general if the array is 'on the stack', i.e. a local
variable within the scope of a function call, then the
variables are NOT initialized.
Go ahead and take a look at the assembly output, if you
make sure you haven't omitted frame pointers, then you'll
see the compiler setup the frame pointer, and then
move the frame pointer by an amount equivalent to the
number of bytes of local variables. Variables in
the source are referenced then with respect to the
frame pointer. There is usually NO initialization
of these variables.
so ideally, you want to code up something like
int some_function(int some_arg)
{
char str[3] = {0, 0, 0};
....
if (some_condition) {
str[0] = 1;
str[1] = 2;
/* no assignment to str[3] */
/* call a function, passing it str */
some_other_function(str);
}
}
and in this case, all variables are initilized before
passing the array to another function.
Cheers
Dave
|