Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "pointer being freed was not allocated" mean exactly?

Tags:

I have trouble with a program where im trying to copy a string to a structs string variable in c. After copying the string im trying to free the temporary string variable which is copied to the structs string variable. But when i try to free the string the program turns on me and says that "pointer being freed was not allocated". I don't understand exactly what is going on.

char str[]="       ";           //temporary string to copy to structs string str[3]=s;                       //putting a char s in middle strcpy(matrix[i-1][j].c, str);  //copying the string free(str);                      //freeing str that now is useless when copied 
like image 629
patriques Avatar asked Oct 30 '12 21:10

patriques


People also ask

What does it mean to free a pointer?

The function free takes a pointer as parameter and deallocates the memory region pointed to by that pointer. The memory region passed to free must be previously allocated with calloc , malloc or realloc . If the pointer is NULL , no action is taken.

What happens to a pointer when it is freed?

Yes, when you use a free(px); call, it frees the memory that was malloc'd earlier and pointed to by px. The pointer itself, however, will continue to exist and will still have the same address. It will not automatically be changed to NULL or anything else.

How do you allocate a pointer?

Dynamically allocating an array of pointers follows the same rule as arrays of any type: type *p; p = malloc(m* sizeof *p); In this case type is float * so the code is: float **p; p = malloc(m * sizeof *p);

How do you free a pointer after returning it?

To free the dynamically object you have to pass the value of x that is returned by the f function to the free function.


2 Answers

Only pointers returned by calls to malloc(), realloc() or calloc() can be passed to free() (dynamically allocated memory on the heap). From section 7.20.3.2 The free function of C99 standard:

The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.

In the posted code, str is not dynamically allocated but is allocated on the stack and is automatically released when it goes out of scope and does not need to be free()d.

like image 192
hmjd Avatar answered Oct 07 '22 22:10

hmjd


Be careful. This code shows confusion about two things:

  1. The difference between stack and heap memory
  2. The operation of strcpy

Point 1

This has already been answered in a way, but I'll expand a little:

The heap is where dynamic memory is given to your process. When you call malloc (and related functions), memory is returned on the heap. You must free this memory when you are done with it.

The stack is part of your process's running state. It is where ordinary variables are stored. When you call a function, its variables are pushed onto the stack and popped back off automatically when the function exits. Your str variable is an example of something that is on the stack.

Point 2

I'd like to know what that c member of your matrix array is. If it is a pointer, then you might be confused about what strcpy does. The function only copies bytes of a string from one part of memory to another. So the memory has to be available.

If c is a char array (with sufficient number of elements to hold the string), this is okay. But if c is a pointer, you must have allocated memory for it already if you want to use strcpy. There is an alternative function strdup which allocates enough memory for a string, copies it, and returns a pointer. You are responsible for freeing that pointer when you no longer need it.

like image 30
paddy Avatar answered Oct 07 '22 22:10

paddy