From time to time, I like to capture all of the information gleaned from threads that I've been tracking in this forum to a private wiki that I have running off my home server.
Based on the discussion that occurred in this thread regarding memory segments and what is stored in each, I've come up with the following entry that I'd like to have checked for accuracy.
Any feedback? Any more information you'd add? Thanks!
---
Memory segments in a C program
Typically, there are four different memory segments in a program: (1) data, (2) stack, (3) code, and (4) read-only data.
Stack segment:
Stores local variables.
A stack of a fixed size is allocated to the process under which the program is executing when the program is loaded into memory. Depending on the compiler, it might be possible to configure the default size at compile time.
Data segment:
Stores global, static and dynamically allocated data.
Code segment:
Stores the machine-level instructions for the program. This is typically read-only and you'll get errors if you attempt to write to a location within this segment.
Read-only segment:
Non-writable, may also be non-executable. All string constants are stored here.
The following example demonstrates an error that occurs when attempting to write to data stored in the read-only segment.
Code:int main(int argc, char *argv[]) { char *foo; foo = "foo"; // constants are stored in the read-only data segment // causes a bus error on OS X 10.5 using gcc strcpy(foo, "bar"); return 0; }