Thread: segment notes

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    69

    segment notes

    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;
    
    }
    Last edited by cs32; 03-05-2008 at 03:30 AM. Reason: editing code comment as per mats' comment

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    foo = "foo"; // constants are stored in the code segment
    That comment is wrong, the constant is stored in "Read-only data".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    That comment is wrong, the constant is stored in "Read-only data".
    My bad - thanks for correcting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. execute a code in data segment...
    By ravindragjoshi in forum Windows Programming
    Replies: 1
    Last Post: 06-12-2007, 11:43 PM
  3. PC-Lint, Notes 964 and 966
    By Mario F. in forum Tech Board
    Replies: 1
    Last Post: 06-30-2006, 01:00 PM
  4. Declare an array in the BSS segment
    By ^xor in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 05:12 PM
  5. Function: convert to notes
    By alice in forum C Programming
    Replies: 8
    Last Post: 05-03-2004, 02:56 AM