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; }
NO, you may not do that. You use free() only with memory that's been dynamically allocated with malloc( ), calloc(), or realloc( ).
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.
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.
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."'
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With