Thread: Where does compiler allocate memory for Char x[] = "abc" ?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Where does compiler allocate memory for Char x[] = "abc" ?

    Where does compiler allocate memory for "abc" that x points to?
    I think it's on heap. Am I right?
    (assume Char x[] = "abc" is defined at global scope)

    Added: Please note that I mean the "abc" x is pointing to. It's different from the original "abc" literal. It's initialized by the original "abc" literal.
    Added: After discussion we believe it's compiler depended. But in most cases it's on data segment. Any comments?
    Last edited by meili100; 10-19-2009 at 04:19 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Where "x" is allocated depends on where the declaration is at. It can be on the stack if that line of code is in a function somewhere.

    If you mean where is "abc" allocated at, that would be in the data section of the executable.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thanks. x is defined at global scope. I think it's on heap, right?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    No, that would also go in the data section.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Quote Originally Posted by bithub View Post
    No, that would also go in the data section.
    If it's
    Code:
    Char * x = "abc";
    I think you are right.

    But for
    Code:
    Char x[] = "abc";
    I think compiler allocate 4 bytes memory on heap and copy "abc\0" to the space.
    Am I right?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The standard doesn't specify where exactly the literal exists in memory, of course, but yes, it generally ends up in the 'data' section (or some other read-only area).

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by meili100 View Post
    If it's
    Code:
    Char * x = "abc";
    I think you are right.

    But for
    Code:
    Char x[] = "abc";
    I think compiler allocate 4 bytes memory on heap and copy "abc\0" to the space.
    Am I right?
    Again, it's free to put it wherever is pleases, but for all implementations I can think of the answer would be "no". For one thing, it isn't practical (or even possible) to mark an area of the heap as "read-only", so bithub's response(s) are most likely going to be in-line with how your particular compiler implements it.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Hang on a minute. Is there any normally declared array that ends up on the heap? Just because you're using an initializer doesn't make this act like a call to malloc. (EDIT: Obviously this is all system-dependent etc etc etc. But using the general idea of "stack" and "heap" and "data segment" etc.)

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I think he's referring to the initializer itself.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If the array is a local variable, then it exists on the stack. When the function is entered, space for the array is allocated on the stack, and then the string literal is copied into the array.

    The string literal itself may or may not exist in the data segment. For longer strings, it almost certainly does. For very short strings, the compiler may choose to inline the copy (embed the string literal into the code as immediate data).

    The heap has nothing to do with any of this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Quote Originally Posted by Sebastiani View Post
    For one thing, it isn't practical (or even possible) to mark an area of the heap as "read-only".
    I think it's not "read only". In fact you can modify it:

    Code:
    Char x [] = "abc";
    x[0] = 'z';
    The memory that x points to is NOT a literal.

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by meili100 View Post
    I think it's not "read only". In fact you can modify it:

    Code:
    Char x [] = "abc";
    x[0] = 'z';
    The memory that x points to is NOT a literal.
    I was referring to the literal "abc" - not the array that is initialized by it. Point is, whether it exists in the data section, or as brewbuck pointed out, directly in the code, it's generally going to be in a read-only section (though I'm not even sure if the standard mandates that).

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Since x is global, it too would be in the data segment, albeit in read-write data area.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by Sebastiani View Post
    For one thing, it isn't practical (or even possible) to mark an area of the heap as "read-only"
    At least using system calls you can make any (virtual) memory region read-only.
    Memory Protection Constants (Windows)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 11:07 AM
  3. OpenScript2.0 Compiler
    By jverkoey in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 01:52 PM
  4. Compiler Design... Anyone That Can Help!
    By ComputerNerd888 in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2003, 09:48 AM
  5. when to allocate memory
    By SAMSAM in forum Windows Programming
    Replies: 3
    Last Post: 01-22-2003, 11:40 PM