Re: C local auto-initialized?
Re: C local auto-initialized?
- Subject: Re: C local auto-initialized?
- From: Steve Sisak <email@hidden>
- Date: Fri, 21 Sep 2007 08:11:56 -0400
At 7:21 PM -0500 9/20/07, Fritz Anderson wrote:
Yes, that is good. However, people seem to forget what I'm doing:
I'm trying to teach how to debug something. I DON'T WANT TO AVOID
THE BUG. And, if I'm debugging, there's no optimization. Besides,
I'm writing for instructional purposes, teaching Xcode, and at the
very beginning, I can't detour people away from instructional tasks
to adjusting the development environment.
Hi Fritz,
I think the guys that suggest that the fact that you're adding a very
large value and a very small value cause the small value to
"disappear" due to rounding error are on the right track. Also, zero
is a statistically likely value to find on the stack.
You might eliminate the first issue by using integer rather than
floating point math -- this will show all bits of the result.
(Assuming you are just trying to show the effect of uninitialized
variables, rather than doing something than inherently requires
floating point)
The other thing you could do is split your example into 3 functions:
1) A function which has a local stack-based array and writes garbage to it
2) Your example function
3) A "main" that calls the two in sequence
This would illustrate the effects of garbage left on the stack.
void partial(void)
{
#if STRANGENESS
double sumY, sumY2;
#else
double sumY2, sumY;
#endif
// [1]: sumY2 is garbage
double y = 4.0;
sumY2 += y * y; // [2]: sumY2 used uninitialized
printf("sumY2 = %f", sumY2);
#if STRANGENESS
// [3a]: sumY2 is nonetheless 16.0
#else
// [3b]: sumY2 is garbage plus 16.0
#endif
}
void smash(void)
{
long data[512];
for (int i = 0; i < (sizeof(data)/sizeof(data[0])); i++)
{
data[i] = random();
}
}
void test(void)
{
smash();
partial();
}
(this code typed in an email client, YMMV)
HTH,
-Steve
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden