Thread: heap vs stack memory question

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    19

    heap vs stack memory question

    I think there are about three ways you can initialize a character string. I have been having some trouble understanding why I cannot write to a string allocated using the third initialization below.

    Code:
    char* str = new Char[13];
    str = "Hello World!";
    Code:
    char str[] = "Hello World!";
    Code:
    char* str = "Hello World!";
    Now let's suppose that I want to modify one of the strings by doing
    Code:
    str[0] = 'Y'
    For the first two strings, this works perfectly fine, but for the third one, I get memory access violation error. Is this happening because in the third case, the characters are written to the section of memory between stack and heap block where no user is supposed access? I'd greatly apprecaite for the help. Thanks.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    The first one is wrong because you're allocating memory for your string, then overwriting the pointer with a string literal. Use strcpy() instead of =

    The reason you can't modify the 3rd one is because it's a constant string. So in reality when you assign a string literal it's actually a const char* not a char*.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    There is a subtle difference between these two:

    Code:
    char string1[] = "Something";
    char *string2 = "Something";
    In the first case, string1 is a character array which is automatically sized large enough to hold the string "Something". When the variable is initialized, the characters making up the string are copied into this array. This array is writable, like any other array.

    In the second case, string2 is a char pointer, not an array, and it points directly to a read-only string literal, which cannot be written to.

    EDIT: This has nothing to do with the distinction between stack or heap memory.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    Code:
    char* str = new Char[13];
    str = "Hello World!";
    With this code you are not making use of your allocated space you created with new. The "Hello World!" string is hard coded into your executable during compile time. When your program is loaded in to memory, along with the hard coded string, the second line justs sets the pointer "str" to the address of the hard coded string. This hard coded string is "const", meaning constant and read only.

    Code:
    char str[] = "Hello World!";
    Please see the above post for this.

    Code:
    char* str = "Hello World!";
    Same as above.

    Code:
    str[0] = 'Y'
    The reason this code is working on the first and last is called undefined behavior. Meaning anything could happen, in this case, the way you to intended it to, but maybe not all the time. That's way its called undefined. And undefined behavior must be avoided at all times.

    The proper way for a read only string to be writable is (unfortunately this is C code, not C++, as i do not know how to make a const C string writable with C++, i am pretty certain someone else does around here):
    Code:
    #include <cstring>
    ...
    const char* src = "this is a constant string.\n";
    char* dest = new char[50]; // Allocating enough memory.
    std::strcpy(dest, src); // Copy the string to the newly allocated memory.
    dest[0] = "T"; // This will work now, since the allocated memory is writable.
    Last edited by Florian; 01-22-2009 at 05:17 PM.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    19
    Thank you guys very much. It's all clear now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A custom gets function
    By aijazbaig1 in forum C Programming
    Replies: 17
    Last Post: 02-02-2008, 05:21 PM
  2. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  3. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 05:03 AM
  4. Relocatable code and Symbol Table
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-10-2002, 11:05 AM
  5. heap memory management
    By jdinger in forum C++ Programming
    Replies: 4
    Last Post: 04-27-2002, 10:23 PM