Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I free char* initialized using string-literals?

Tags:

Should I free char* variables when they were initialized using string literals? To me, the syntax would lead me to the assumption that they are only stack-allocated, but this example showed me, they are not.

#include <stdlib.h> #include <stdio.h>  static char* globalBuffer;  typedef struct Container {     char* buffer; } Container;  Container* Container_new(char* buffer) {     Container* container = malloc(sizeof(Container));     container->buffer    = buffer;     globalBuffer         = buffer;     return container; }  void Container_print(Container* container) {     if (container->buffer != NULL) {         printf("%s", container->buffer);         printf("\n");     }     else {         printf("Container contains a NULL-buffer.");     } }  Container* stage() {     Container* container = Container_new("Test-string.");     Container_print(container);     return container; }  int main() {     Container* container = stage();     Container_print(container);      free(container);     Container_print(container); // I know, this results in undefined behaviour      printf(globalBuffer);     printf("\n");      return 0; } 

I get the following output:

C:\Users\niklas\Desktop>gcc char_test.c  C:\Users\niklas\Desktop>a.exe Test-string. Test-string. ­6> Test-string.  C:\Users\niklas\Desktop> 

So, the char* initialized with string-literals does still exist, even it got out of scope.

So, my question, should I free such char* pointers? Would this be the correct main()?

int main() {     Container* container = stage();     Container_print(container);      free(container->buffer);    // NEW     free(container);     Container_print(container);      printf(globalBuffer);     printf("\n");      return 0; } 
like image 726
Niklas R Avatar asked Feb 29 '12 18:02

Niklas R


People also ask

Should char * Be Freed?

NO, you may not do that. You use free() only with memory that's been dynamically allocated with malloc( ), calloc(), or realloc( ).

How do you initialize a string literal?

The best way to declare a string literal in your code is to use array notation, like this: char string[] = "I am some sort of interesting string. \n"; This type of declaration is 100 percent okey-doke.

What is char in string literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string.

What are examples of string literals?

A string literal is a sequence of zero or more characters enclosed within single quotation marks. The following are examples of string literals: 'Hello, world!' 'He said, "Take it or leave it."'


1 Answers

You shall never free() memory that you did not malloc()ed.

The way the compiler implements string-literals is not your business: it's an implementation detail. You can free() a pointer to memory that you allocated using malloc(), and only those, or you are risking the life of your system.

Ideally, malloc() calls and free() calls should appear on the same "design level" (inside the same implementation file for the same module for example), and they should match perfectly: one free() for each malloc(). but that's not always possible.

(Note that some library allocates blocks of memory, returns pointers to those blocks, and instructs you to free them. in this case, you are allowed to free those pointers, but that is a bad design practice from the people who created the library.)

like image 194
Adrien Plisson Avatar answered Oct 02 '22 02:10

Adrien Plisson