Thread: string declaration and memory leak?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    11

    string declaration and memory leak?

    Hi, i have a question.
    I have a variable declaration like
    Code:
    char string1[255] = "Hello World";
    What's really happening here? The string "Hello World" is copied into a location in executable file and it will be replaced with his memory address...but the compiler has reserved 255 byte for string1 and this space is unused.

    I know it would be more correct use declaration like
    [code]
    char string1[255] = "Hello World";
    [code]
    but i want to know why.

    thanks.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Both of your examples are the same. I think you meant to say:

    Code:
    char string1[] = "Hello World";
    This is called a string literal, and it only allocates memory for what you initialize it with.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    char string1[255]="Hello world";
    "Hello world" is string literal that is located somewhere in the read-only memory

    string1 is character array of 255 bytes allocated on stack

    after allocation of the memory the string "Hello world" is copied in the beginning of the allocated memory.

    The rest of the buffer is available for use by the application later. for exmple
    Code:
    strcat(string1," from me on this sunny day!");
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by ignazioc View Post
    Hi, i have a question.
    I have a variable declaration like
    Code:
    char string1[255] = "Hello World";
    What's really happening here? The string "Hello World" is copied into a location in executable file and it will be replaced with his memory address...but the compiler has reserved 255 byte for string1 and this space is unused.

    I know it would be more correct use declaration like
    [code]
    char string1[255] = "Hello World";
    [code]
    but i want to know why.

    thanks.


    Code:
    char string1[255] = "Hello World";
    In the above initialization the size of array is 255 bytes.
    Where as for
    Code:
    char string1[]="Hello World";
    the size of the string1 array is just 12 bytes of memory.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    Quote Originally Posted by vart View Post
    Code:
    char string1[255]="Hello world";
    "Hello world" is string literal that is located somewhere in the read-only memory

    string1 is character array of 255 bytes allocated on stack

    after allocation of the memory the string "Hello world" is copied in the beginning of the allocated memory.

    The rest of the buffer is available for use by the application later. for exmple
    Code:
    strcat(string1," from me on this sunny day!");
    thanks for your answer now it is clear but what's appen when i use
    Code:
    char string1[] = "hello world";
    char *string2 = "hello world";
    now there isn't array allocation, but there is only one single pointer...there isn't space for coping string-literal...maybe the compiler is so smart to allocating space automatically?

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by vart View Post
    Code:
    char string1[255]="Hello world";
    "Hello world" is string literal that is located somewhere in the read-only memory
    and *string2 is assigned the memory address where that string literal has been stored.

    Nothing is copied.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by ignazioc View Post
    thanks for your answer now it is clear but what's appen when i use
    Code:
    char string1[] = "hello world";
    char *string2 = "hello world";
    now there isn't array allocation, but there is only one single pointer...there isn't space for coping string-literal...maybe the compiler is so smart to allocating space automatically?
    Incorrect. You appear to be making the mistake of believing that a pointer and an array are the same things. You also appear to believe that string2 becomes a synonym for string1: it is not.

    string1 is initialised (and sized at compile tiime) using a literal "hello world". The contents of string1 may be modified, but the array cannot be resized. So modification of a character in string1 (for example, string1[4] = 'O' to change the first 'o' from lower case to upper case) is valid. But accessing an element past the end (for example, string1[20] = 'x') gives undefined behaviour - string1 cannot be resized at run time.

    string2 is a pointer that is initialised so it points at the first character of a string literal "hello world". Any modification of that string literal using the pointer (for example, string2[4] = 'O' to change the first 'o' to 'O') gives undefined behaviour. A lot of compilers will warn about such things (as they can detect string2[4] should not be modified) but they are not required to.

    Importantly, modifying string1 (for example, string1[4] = 'O') does not modify the string literal pointed to by string2 (unless the compiler is broken, and unfortunately some such compilers exist). Because of the existence of broken compilers (or, more accurately, over-aggressive optimisers) disciplined programmers will change your definition of string2 to
    Code:
       const char *string2 = "hello world";
    which FORCES the compiler to complain about any attempt to modify the literal (so string2[4] = 'O' will yield a compilation error).

    Some compilers are are able to optimise usage of string literals. For example, in your code, they can detect there are two string literals with the same contents ("hello world") and rationalise that down to one instance of the data in memory. However, if the compiler does that, it is still required to allow string1 to be modifiable, it is still undefined behaviour to modify string2, and modifying string1 does NOT modify (your original) string2.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    ok, now it is very clear.
    thank you a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-28-2006, 01:06 PM
  2. Memory Leak
    By Berticus in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 05:11 PM
  3. c style string memory leak question
    By curlious in forum C++ Programming
    Replies: 7
    Last Post: 09-14-2003, 06:31 AM
  4. A memory leak (I think...)
    By Stevek in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2003, 03:09 PM
  5. Memory leak or no?
    By rafe in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2002, 04:33 PM