tags:

views:

185

answers:

7

Please excuse my C newbiness.

Consider the following C procedure:

int doSomething() {
    char *numbers="123456";
    ...
}

Before this procedure exits should I be releasing the 'numbers' pointer?

+10  A: 

No, you didn't malloc it. Why should you release it?

Often, the string is placed in a read only section of the executable.

Mehrdad Afshari
Not only is there no reason to release it, there's very good reason to not call `free(numbers)`.
David Thornley
+3  A: 

No, there is no need to do so, and doing so is incorrect (thanks dmckee). The character string will be in the data segment of the binary, so it can't be freed. The memory was not dynamically allocated.

"numbers" will be on the stack, so it will go away when the function returns.

WhirlWind
Not just "no need to", it is an error to do so.
dmckee
A: 

No, it points to pre-allocated memory in process data segment. Only free(3) what you have malloc(2)-ed (modulo some library functions like getaddrinfo(3)/freeaddrinfo(3).)

Nikolai N Fetissov
A: 

NO, since it was not allocated by malloc/ calloc / realloc.

It will be automatically "freed" because it is an automatic variable.

Tom
+4  A: 

In C language you don't and can't "release" pointers. Pointers are ordinary scalar variables. There's nothing you can do with them in terms of "releasing" or anything like that.

What one can be "releasing" is the memory a pointer is pointing to. But in C you only have to release memory that was explicitly allocated by malloc/calloc/realloc. Such memory is released by calling free.

Note again, that in your program you might have several (a hundred) pointers pointing to the same block of allocated memory. Eventually, you'll have to release that memory block. But regardless of how many pointers you have pointing to that block, you have to release that memory block exactly once. It is your responsibility to make sure that you release it. And it is your responsibility to make sure you released it exactly once. I'm telling you this just to illustrate the fact that what you release is the memory block, not the pointers.

In your example, your pointer points to a memory block that was never allocated by any of the aforementioned functions. This immediately means that you don't need to release anything.

AndreyT
+1  A: 

No; there's nothing to release. The string literal "123456" is an array of char (const char in C++) with static extent. The memory for it is allocated at program startup and held until the program exits. All you've done is assign the address of the string literal to numbers; you haven't actually allocated any memory.

John Bode
+1  A: 

you don't actually need to call delete or free , these operators are only used to clean up memory that have been allocated my runtime memory allocator like malloc, calloc , GlobalAlloC, HeapAlloc and so forth. When you define a pointer like in the example you are actually allocating space for array of character in the the execuatable file. so bigger the string length bigger will be the executable size thus increasing your working set.

Int3