1

I have a hard time understanding when and when not a char array is heap/stack allocated, depending on the way they are declared. For example:

void test()
{
   char *str1 = "MyString1";  // do i need to free this ??
   char str2[] = "MyString2"; //this is clearly on the stack

}

Should i call free(str1) before exiting test()??

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
JustANoob
  • 580
  • 1
  • 4
  • 19

4 Answers4

4

You only have to free what you allocated. In your example, there is nothing to free, because the compiler created a static storage for it, which can not and must not be freed.

Wether you have to free something also depends on the functions you are calling. For example, if you call strdup() then you need to free the returned pointer:

char *p = strdup("MyString");
free(p);

However, if you for example, call strcpy() then you don't call free.

char buffer[100];
strcpy(buffer, "MyString");
// Don't free, as you created a local buffer on the stack and strcpy() doesn't allocate anything.

Here you would have to free because you allocated it:

char *p = malloc(100);
strcpy(p, "MyString");
free(p);
Devolus
  • 21,661
  • 13
  • 66
  • 113
2

No, you don't have to free neither: free() is always used when you malloc() something to a dynamic dimension: the difference between the two declarations you provided, aside from one being a pointer and the other one being an array, is simply a matter of editability: the first one is an uneditable literal string, the second one is editable, but they're both allocated on the stack.

Nastor
  • 638
  • 4
  • 15
  • "Both on stack" is vague and probably misleading. There is a pointer and the data. Both pointers are on stack, but the data may not be: in the editable case, the data bytes are on stack; in the read-only case, the data bytes are in a dedicated read-only segment. – anatolyg Feb 17 '21 at 09:52
  • Yes, good correction, my bad. – Nastor Feb 17 '21 at 09:53
1

In these two declarations

char *str1 = "MyString1";
char str2[] = "MyString2";

you declared explicitly only one character array str2. This array has automatic storage duration and the memory it occupies will be freed after exiting the function where it is declared. That is after exiting the function the array will not be alive and the memory it occupies can be reused by other objects.

You have to call the function free when an array was allocated using memory allocation functions as malloc or calloc.

In the first declaration you declared a pointer to the first character of the string literal "MyString1". The string literal is internally stored as a character array defined like

char unnamed_string_literal[] = { 'M', 'y', 'S', 't', 'r', 'i', 'n', 'g', '1', '\0' };

It has static storage duration and will be alive after exiting the function.

On the other hand the pointer itself has automatic storage duration and after exiting the function the memory occupied by it can be reused.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

The string literals "MyString1" and "MyString2" are stored in the different data segments (other from stack or heap).

In the first case, you are creating a new auto variable ("str1") of type "pointer to char," and the address of the string literal "MyString1" is copied to it.

In the second case, you are creating a new auto variable ("str2") of type "9-element array of char" (size is taken from the length of the string literal), and the contents of the string literal "MyString2" are copied to it.

The auto variables are always local and are stored on the stack.

Both the variables are stored on the system stack and nothing is allocated. Therefore, calling free() makes no sense.

PS: Calling free() at the end of the program also doesn't makes sense as the operating system takes care of it.

Deepak Tatyaji Ahire
  • 4,883
  • 2
  • 13
  • 35