Zip the exe and post it. I seriously doubt that the compiler can use overlapping structures for
test2 t = {"I am the first string"};
printf("%p\n", &t);
test u = {"I am the second string"};
printf("%p\n", &u);
printf("%p\n", &t); // use the first string again
JJ... in all due respect your code is as different from mine as night and day...
In C an opening bracket
{ signals the beginning of a scope that continues until the matching close bracket
} Variables created inside that scope are "encapsulated" by the scope and are unavailable outside the scope... Variables created at larger scope are available inside the smaller scope, except where hidden by a local variable of the same name.
Try this... *with the brackets exactly as I have them*...
int main (void)
{
{
int y = 20;
}
printf("%d", y);
return 0;
}
Unless your compiler is seriously braindead you should get an error at the printf() line saying that y is undefined. This is because it is encapsulated in it's own little scope...
Now back to my original sample:
#include <stdio.h>
typedef struct t_test
{
char str[256];
int x;
} test;
typedef struct t_test2
{
char str[256];
int x[2];
} test2;
int main (void)
{
puts("Hello");
{ <-- begin scope 1
test2 t = {0}; <-- t is encapsulated
printf("%p\n", &t);
} <-- end scope 1 (Is t destroyed?)
{ <-- new scope 2
test u = {0}; <-- u is encapsulated
printf("%p\n", &u);
} <-- end scope 2 (Is u destroyed?)
puts("bye");
return 0;
You cannot access either t or u outside it's own scope. Moreover; since each is in it's own private scope, the two structs should not exist in memory at the same time. The only question here is whether there is a stack cleanup --releasing the space occupied by the structs-- at the end of each scope...
In terms of stack usuage, it should be functionally equivalent to...
int main (void)
{
puts("Hello");
test2* t = calloc(1, sizeof(test2));
printf("%p\n", &t);
free(t);
test* u = calloc(1, sizeof(test));
printf("%p\n", &u);
free(u);
puts("bye");
return 0;
And has the additional benefit of encapsulating large segments of unrelated data.
In the particular problem I'm having, I've written a long function (~ 200 lines) that doesn't seem to want to break up into smaller functions easily; I would have to pass a dozen or more parameters each time... The program crashes near the end of this function in an apparent stack overflow that should not happen if the variables are being removed (released, overwritten, re-used...) at the end of each of the scopes within the function as they should.