0

I am trying to find the issue with a bit of C code that I have. The debugger says that the error occurs when I try to free the mem from a pointer:

int main(int argc, char *argv[]){   
    char *desc = malloc(30 * sizeof(char));
    if(desc == NULL)                    
    {
        fprintf(stderr, "Error - cannot allocate memory\n");
    }
    desc = "Debug this program.";
    printf("Description: %s\n", desc);
    free(desc);//break point here
    int cpid = fork();
    ....
MaddieSun
  • 101
  • 8
  • 1
    You are trying to free address of a literal string, that is illegal. – LPs Sep 08 '16 at 13:56
  • 2
    Moreover, assigning an allocated pointer from a string literal is almost always wrong. If you want to copy the literal into `desc`, use `strcpy`. – fmt Sep 08 '16 at 13:57
  • Note that `sizeof(char)` is always 1. It's the unit of measure for object size. – John Bollinger Sep 08 '16 at 14:05
  • @Lundin looking through the likely duplicates, the answer that seems to be on it's way to accepted here does not look like any of the answers on the other "duplicates". – dckuehn Sep 08 '16 at 14:53
  • @dckuehn Feel free to suggest a better duplicate. ([This one?](http://stackoverflow.com/questions/3131319/how-to-correctly-assign-a-new-string-value)). The problem is that the OP doesn't understand how pointers and arrays work, nor how strings work. `strcpy` will solve the problem. – Lundin Sep 08 '16 at 15:05
  • OP's misunderstanding of c doesn't necessarily imply the question is a duplicate though. The question you referenced doesn't reference `malloc()` or `free()` which is what OP thought the problem was. I think this question is good for users who do are learning c and do the same thing. Unless you find a duplicate that specifially mentions those two functions, I don't feel this is a duplicate. @Lundin – dckuehn Sep 08 '16 at 15:31
  • @dckuehn Then pick anything from [this one](http://stackoverflow.com/search?q=[c]+how+does+malloc+work%3F). This is really basic stuff. – Lundin Sep 09 '16 at 06:14

4 Answers4

6

You reassigned desc and then freed a pointer to a string literal, that's illegal and it causes the segmentation fault.

You apparently failed to understand what malloc() is for, malloc() requests memory from the OS and returns a pointer to valid memory that you can use in your program.

After you malloc() you can use the memory, but not all pointers need to me malloc()ed. For example, you can have pointers to string literals and they are useful too.

But you can't pass anything to free() except if it was returned by malloc()/calloc()/realloc(). A pointer to a string literal or holding the address of a variable is not such a pointer, and passing it to free() is undefined behavior.

Only use malloc() if you know that you MUST, for example to allocate a lot of memory that would overflow the stack or to allocate an unknown ammount of memory that you can calculate at runtime. Otherwise, don't.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • Thank you! That really clears it up and explains things. I am in an OS course and it is really confusing to me in all honesty. – MaddieSun Sep 08 '16 at 14:04
3

At first you allocated dynamically memory and its address assigned to the pointer desc

char *desc = malloc(30 * sizeof(char));

Then you reassigned the pointer with the address of the first character of the string literal "Debug this program."

desc = "Debug this program.";

Thus the address of the allocated memory was lost.

Then you are trying to free the memory occupied by the string literal

free(desc);//break point here

However string literals have static storage duration and can not be freed by using standard function free.

Instead of this assignment statement

desc = "Debug this program.";

you should use standard C function strcpy as for example

strcpy( desc, "Debug this program." );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

You are trying to free address of a literal string, that is illegal.

To init an mallocated string with your literal string you can use, e.g. strcpy:

strcpy(desc, "Debug this program.");

You should also exit/terminate your program in case of malloc fails.

LPs
  • 16,045
  • 8
  • 30
  • 61
-1

Simply said (and read the other solutions), that memory is not yours to delete.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • This is a bad answer that does not try to answer the question. This would be better as a comment. Even then, `malloc()` and `free()` are supposed to be used in C, saying "that memory is not yours to delete" is easier to say than understand. – dckuehn Sep 08 '16 at 18:55