Gpathy
Gpathy

Reputation: 316

release memory of the string char *str = "hello"

Is it required to release the memory allocated to string "str", in the below program ?If so, free(str) works ? In which segment, the memory is allocated to "str" ?

int main()
{
    function()
}

function()
{
    char *str="hello";
     --
     --
     return
}

Upvotes: 2

Views: 3291

Answers (4)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215387

Most of the other answers are pretty complete already, but one thing that hasn't been stressed enough is addressing use of free. The only pointers you're ever allowed to pass to free are those obtained via malloc, "as if via malloc" (some library functions are documented as returning such pointers), or via one of the other malloc-related functions (calloc, realloc, or on POSIX, posix_memalign).

It's not a matter of whether you "need to" free your string. The much more important thing is that you're not allowed to free it. At best (if your C library is very kind to you) it will detect your attempt to free a string literal and immediately abort your program. If you're unlucky, you'll silently corrupt memory, and either crash much later when it's hard to track down the root cause, or maybe overwrite important data with corrupt nonsense the next time you click "save" in your application.

Upvotes: 1

Arunmu
Arunmu

Reputation: 6901

If you disassemble executable of your code (for ex using gdb) you will be able to see that "hello" is pushed to the stack. Therefore having a scope of lifetime of the function.

Upvotes: 0

Prasoon Saurav
Prasoon Saurav

Reputation: 92874

char *str= "hello";

String literal "Hello" is stored in const region of memory(on most common implementations) and has static storage duration. Any attempt to modify the content would invoke UB i.e str[0]='c' would cause segfault on most implementations.

ISO C99 2.13.4/1 says

An ordinary string literal has type "array of n const char" and static storage duration.

'static storage duration' in 3.7.1/1:

The storage for these objects shall last for the duration of the program.


Is it required to release the memory allocated to string "str", in the below program ?

Nopes! That would be UB too because the string is not dynamically allocated.

Upvotes: 8

StasM
StasM

Reputation: 11052

String constants are not allocated on the "heap" - where the memory that you need to free() is allocated. They are allocated in another special segment, and there's no way to actually "free" this memory - i.e. make it useful for something else in the program. free(str) would either lead to a crash (segmentation fault) or if your C library is smart enough it would produce an error notifying you about memory corruption.

You should free() only values that you allocated with malloc() or return values from functions that are specifically described by the manual as returning malloc()ed result that you have to free. Leave the rest alone.

Upvotes: 3

Related Questions