• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

old-school C strings

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.
Originally posted by: kamper
The stack is function-specific memory. It stores function parameters and return values, stack data and other such things. It grows with every function call and shrinks with every function return. Anything that you declare local to a function gets placed on the stack (and that's why you can't pass pointers to local variables as returns, because once a function returns, it's memory on the stack is invalid).

So that's why my program's pointer was invalid. Thanks for clarifying that. I've actually come across that problem myself and have been totally puzzled. As they say, knowledge is power.
 
Originally posted by: xtknight
What's the difference between the heap and stack? How does one denote that a variable should be on the heap, or on the stack and what difference does it make? Is one always read-only? (And what are they in general??)

char *string = "I like to eat pork chops."; // this is a constant?
char string[] = "I like to eat pork chops."; // yet this is modifiable?

in the first case, the memory is probably neither on the stack nor on the heap. it's probably somewhere along with the program's code section.
 
Originally posted by: DaveSimmons
Originally posted by: kamper
Shouldn't the stack be allowed to grow as long as there is space? Like if the heap is relatively small and defragmented, it should be able to take up most of the address space.
It doesn't work that way, the stack is given a fixed block of memory at process startup that can't be moved or grown. You also set a fixed size when creating threads.

It's an assembly-level, CPU-hardware-supported allocation for the process that's independent of whatever mix of language(s) you are using, though you can set a desired (fixed) size for the process using linker flags.
Threads, yeah, that's the big problem with my model 😛 Thanks.
 
Originally posted by: kamper
The stack is function-specific memory. It stores function parameters and return values, stack data and other such things. It grows with every function call and shrinks with every function return. Anything that you declare local to a function gets placed on the stack (and that's why you can't pass pointers to local variables as returns, because once a function returns, it's memory on the stack is invalid).

The heap is kept at the opposite end of memory and is just a big blob that you make allocations out of. Any call to malloc and friends gives you heap memory. The char * declaration goes somewhere else altogether, I'm guessing, in a section where lots of read-only data goes.
yea.. it goes below the heap in the text section.

as people have already pointed out.. in both instances, the string that you assign to the variable is allocated in the text section (masm also calls it const).. when you declare it as an array, a copy is made on the stack of that particular string.. when you declare it as a pointer, the variable physically points to the constant. the data referenced is contained in the segment of memory that the cpu reads code from.. which is set to read-only (when you try and write to the code segment, you get a segmentation fault).

if you're using visual c++ 2005, then you can rest assured that microsoft has gone ahead and made their product incompatible with the competition once more. the code is compliant with iso standards, but visual c++ won't accept it.
 
Back
Top