Win32 question

Started by
5 comments, last by Nervo 20 years, 2 months ago
In Petzolds book, he has a statement that says
quote: char a[] = "hello"; if you define this array as a local variable to a function, it must be defined as a static variable as follows: static char a[] = "hello";
Well, I do understand what a static is of course, but what puzzles me if why does it have to be static? What could happen if I just declared it on the stack? The book gives no explanation and neither does my searches through google.
Well, R2D22U2..
Advertisement
That quote is awfully short on context. How does he use that a[] variable? It could be that it needs to outlive the stack frame that the function call lives in.
I'm just speaking of chapter 2 on page 27 here. There is specific context he's speaking of[EDIT: Well actually there isn't]. The section its in is just titled "The char data type"


He's saying that a char array, if global, does not need a static declaration so char a[] = "blah" is fine

[edited by - nervo on February 8, 2004 3:44:16 PM]
Well, R2D22U2..
It doesn''t, hes speaking about inside the WndProc because tahts a separate thread...
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
quote:Original post by Ademan555
It doesn''t, hes speaking about inside the WndProc because tahts a separate thread...


WndProc isn''t even discussed until chapter 3. Well, I just don''t know what he''s talking about but obviously a static declaration does not seem to be needed in all cases. ugh.
Well, R2D22U2..
If you try to return the variable and its not static, then its declared on the stack (as you said). When the function returns, the stack pointer gets shifted, and whatever was in the stack frame for the function gets lost (well, it doesn''t immediately, but it gets marked as free). So, the string will get corrupted.
Actually, constant strings are stored inside the exe file, and when you return one from a function you get the address of the string in the processes address space. To see what i mean, try this:
const char* foo() {return "Hello World!";}int main(int argc char** argv){   printf("0x%08X\n",foo());}

You should get some value thats just over 0x00400000

quote:Original post by Evil Steve
If you try to return the variable and its not static, then its declared on the stack (as you said). When the function returns, the stack pointer gets shifted, and whatever was in the stack frame for the function gets lost (well, it doesn't immediately, but it gets marked as free). So, the string will get corrupted.
Actually, constant strings are stored inside the exe file, and when you return one from a function you get the address of the string in the processes address space. To see what i mean, try this:
const char* foo() {return "Hello World!";}int main(int argc char** argv){   printf("0x%08X\n",foo());}  

You should get some value thats just over 0x00400000



I do get the fact of non-static being popped off the stack. I guess I just need to work with more win32 examples where persistence of a char array would be critical. Its just that that the book just shoved it in my face without giving me specific win32 example where something might bomb without it. (I generally hate accepting something just because the author said so)

So with that said then, I suppose that now I should just declare all char arrays as static inside a function or just do a char* x = "whatever";

EDIT: Perhaps what the author might have implied was that if I want a variable to have persistence I could declare it global, or if local to a function, then do a static char....I'm going with that one. >_<

[edited by - nervo on February 8, 2004 4:19:58 PM]
Well, R2D22U2..

This topic is closed to new replies.

Advertisement