0

I'm trying to use '\0' to truncate a string:

char str[] = "hello";
str[2] = '\0';

After doing this, the string length will be reduced to 2 since the function for checking string length terminates at '\0'. I'm curious about the memory space preserved for "lo". Will this lead to memory leak?

ray
  • 183
  • 4
  • 12
  • 7
    No, the string is on the stack. Memory leaks happen on the heap generally. Anyway, just finish learning C before asking those questions. Finish your book, it explains it better than we can. – sashoalm Feb 12 '15 at 09:54
  • 3
    There's a slight error in your question ( Not that it will make much difference ). The First `"l"`of `"llo"` will be replaced by `'\0'`. – Arun A S Feb 12 '15 at 09:57
  • Similarly, if you were to have dynamically allocated `str` (so that it would be on the heap), the code still would not lead to a memory leak as long as you freed the memory. – Daniel Kleinstein Feb 12 '15 at 09:58
  • Actually, the string is likely in read-only memory, and you'll get an error when trying to overwrite one of the bytes of that string. – Jim Buck Feb 12 '15 at 09:59
  • 1
    @JimBuck, I think you mean `char *str = "hello";`. – ray Feb 12 '15 at 10:01
  • @JimBuck I dont think so,declaring a array will store the string the string either on stack or in data segment. – Vagish Feb 12 '15 at 10:02
  • @JimBuck Nope, `char []` can be modified – qrdl Feb 12 '15 at 10:02
  • @Daniel so if it is on the heap, I have to free the `'lo'` after doing this? – ray Feb 12 '15 at 10:02
  • 2
    @ray No, I said that the code would *not* lead to a memory leak, meaning you don't have to free the `'lo'`. `free` works by looking at the size of the dynamically allocated memory, not its contents (which in this case include the null character). – Daniel Kleinstein Feb 12 '15 at 10:04
  • 1
    A memory leak can only occur when you use one of the `malloc` family functions (or a non-standard function which calls those functions such as `strdup`); and then don't call `free`. – M.M Feb 12 '15 at 10:09
  • Hmm, I've never used `char []` in 20+ years of programming in C, but I had always though it was equivalent to `char *`. I guess not :) http://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c – Jim Buck Feb 12 '15 at 19:28

3 Answers3

2

Your snippet has 2 memory areas to consider:

The area for the string literal "hello", composed of 6 bytes ('h', 'e', 'l', 'l', 'o', '\0') and the area for the variable str, also composed of 6 bytes (copies of the string literal).

Neither of these memory areas will ever move around, change size or be altered in any way whatsoever, so there is no chance for a memory leak.

You can change the contents of the memory area reserved for str; you cannot change the contents of the memory area reserved for the string literal.
Setting str[2] to '\0' is perfectly ok.

pmg
  • 106,608
  • 13
  • 126
  • 198
1

There are two possibilities: either char str[] = "hello"; is inside a function, or it is outside of a function.

If it is inside a function, then the array will be allocated on the stack, so as soon as your function returns the stack will be reclaimed, so no harm done.

If it is outside of a function, then the array will be allocated in the static data segment. When you truncate it by placing a zero in the middle of it, the memory is still there, and you can later replace the zero with 'l' and you will get "Hello" back. No harm done, either.

The only scenario under which we speak of memory leaks is when we allocate memory dynamically, for example with malloc(). If you allocate memory with p = malloc( 1 ); and then forget the pointer to it (say, with p = NULL; or with another p = malloc( 1 ); without first doing free( p );) then you have introduced a tiny, tiny memory leak.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
1

No, there's no a memory leak here.

The key thing is that you've declared and initialized an array of chars. You could have done that two ways, which are effectively the same:

First way:

char str[] = {'h', 'e', 'l', 'l', 'o', '\0'};

Here compiler knows you want an array of chars, and it figures the array length and contents via by-element initialization. So, you have an array of 6 chars.

Second way:

char str[] = "hello";

Here compiler once again knows you want an array of chars. Array of chars, this is a kind of special case, can be initialized with a string literal. It's length is 5 printed characters plus one for the string terminating NUL. So you have an array of 6 chars.

Changing any certain element of this array doesn't lead to a memory leak, since you can still access any array element with str[i]. You just have to make sure, you don't get out of array bounds with i.

Try to compare to this:

int arr_int[] = {10, 20, 30, 40, 50};
arr_int[2] = 0; 

Is there a memory leak in this snippet? -- No, there isn't.

Igor S.K.
  • 999
  • 6
  • 17