Thread: some questions about memory layout of a c program?

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    11

    Question some questions about memory layout of a c program?

    when we talk about memory layout of a cprogram ....is it hapning in compile step or link or when we writ a c program immediatly?
    another point :text segment is it a-out?
    as i understood the text segment is machine instruction it's often read only to prevent a program from accidentally modifying its instructions.
    so it's like a contant in memory,is'nt it?
    and preporessore directive(such as #include stdin.h or #define ..) where is it in memory layout? i mean where is its position or section in memory ?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    As a general rule, memory layout of the program is irrelevant to the programmer. The only exceptions to this, of which I am aware, are in embedded and operating system kernel programming.

    Quote Originally Posted by sweezy View Post
    when we talk about memory layout of a cprogram ....is it hapning in compile step or link or when we writ a c program immediatly?
    The linker (last stage) is generally responsible for the final memory layout.

    Quote Originally Posted by sweezy View Post
    another point :text segment is it a-out?
    If you're talking about the a.out output of gcc, the text segment is just one part of it. There are several other segments that are part of the final output of the build process.

    Quote Originally Posted by sweezy View Post
    as i understood the text segment is machine instruction it's often read only to prevent a program from accidentally modifying its instructions.
    This is often true. Most modern architectures have a way to protect code from being written, after it's loaded.

    Quote Originally Posted by sweezy View Post
    so it's like a contant in memory,is'nt it?
    You could look at it this way.

    Quote Originally Posted by sweezy View Post
    and preporessore directive(such as #include stdin.h or #define ..) where is it in memory layout? i mean where is its position or section in memory ?
    It isn't. Nothing of the source generally remains in the binary output of the build process, with the exception of literal strings.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Some of the actual layout is done by the loader. For example, a.out usually contains a number for how many bytes are in the uninitialized data section, sometimes called the .bss segment (block started by symbol), which the loader takes into account when allocating memory and defining access right to the various sections of a program. For most PC based compilers the main areas are .text or .code (for code and data that reside in the code such as literals or constants), .data, .bss (block started by symbol), and .stack. Some mainframes have a more generic .bes (block ended by symbol), where the memory is used similar to a stack, but with multiple symbols if wanted. For embedded systems, a copy of the data ends up in the ROM part of the code, and at start up the embedded program copies that data into the actual data region in RAM before the program actually starts up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory layout of c program
    By sujji in forum C Programming
    Replies: 3
    Last Post: 10-15-2013, 12:08 AM
  2. Replies: 14
    Last Post: 10-12-2012, 02:39 AM
  3. memory layout??
    By ungainly_one in forum C Programming
    Replies: 14
    Last Post: 11-14-2010, 10:12 AM
  4. Memory Layout
    By chris.r in forum C Programming
    Replies: 5
    Last Post: 04-18-2010, 02:41 PM
  5. memory layout and declaration
    By cbastard in forum C Programming
    Replies: 6
    Last Post: 09-13-2005, 12:24 PM

Tags for this Thread