Free a NULL pointer [duplicate] - c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Does free(ptr) where ptr is NULL corrupt memory?
Is it good practice to free a NULL pointer in C?
I have a question concerning freeing a null pointer.
char *p = NULL;
free(p);
Could the free(NULL) cause a crash?
Or does it depend on the compiler?

From man page of free
void free(void *ptr);
The free() function deallocates the memory allocation pointed to by
ptr. If ptr is a NULL pointer, no operation is performed.
If you want to get confirmation from C manual itself
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.
See page 313 of this document.

Related

Does a pointer need to be nulled after it is freed? [duplicate]

This question already has answers here:
Should one really set pointers to `NULL` after freeing them?
(10 answers)
Closed 3 years ago.
int main(){
(int*) pointer = (int*) malloc(sizeof(int));
free(pointer);
}
I learned from 'Prata, Stephen. C Primer Plus (Developer's Library)' that
"When the function terminates, the pointer, being an automatic variable, disappears." so simply, i don't need to null the pointer
however, I also learned from school that the pointer becomes a dangling pointer if it doesn't get nulled after it free.
Two ideas are contradicting each other. Which one is the right one?
According to the docs:
The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime.
Therefore, you don't need to set pointer to nullptr (in C++11) or NULL (in C and before C++11) if it goes out of scope immediately after you free it because then you have no chance to dereference a dangling pointer.
However, if the pointer is still in the scope after a call to free, than the good practice would be to set it to nullptr or NULL so that the following checks would work:
C++:
if (nullptr != ptr) {...}
C:
if (NULL != ptr) {...}
C and C++:
if (!ptr) {...}
What free does is to deallocate the space that pointer holds before the execution of free. The pointer holds an integer value(the address) before and after the execution therefore the address that the pointer holds is now an invalid address that is not allocated by the program. As said, if you plan to use the pointer again in future cases, you wouldn't wanna have an invalid undefined value in it that you cannot check, having null makes the checks easier.

C pointer + free: Abort signal from abort(3) (SIGABRT) [duplicate]

This question already has answers here:
Why exactly should I not call free() on variables not allocated by malloc()?
(7 answers)
Closed 4 years ago.
I have written the following code in C:
#include <stdio.h>
#include <stdlib.h>
int main (int argc , char *argv[]) {
int * ptr = (int *)malloc(sizeof(int));
int three = 3;
ptr = &three;
free(ptr);
return EXIT_SUCCESS;
}
When I execute I get following error:
Abort signal from abort(3) (SIGABRT).
Could you help me find my mistake?
Thank you!
What you have is undefined behavior. The C11 standard states thus:
7.22.3.3 The free function
...
2 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 a memory management
function, or if the space has been deallocated by a call to free or realloc, the
behavior is undefined.
In your example the argument of free is &three which is not a pointer returned by a memory management function and therefore you have the behavior you see.
When you call malloc a pointer to memory chunk of requested size is returned (on success). This memory chunk is allocated form heap, and you can use that pointer to un-allocate it by calling free later.
Local variables are allocated memory from stack.
What you are doing here is allocating a memory chunk from heap:
int * ptr = (int *)malloc(sizeof(int));
and then overwriting then overwriting ptr with address of a local variable who's memory resides on stack.
ptr = &three;
and then attempting to free that memory:
free(ptr);
which is undefined behaviour.

Arrays of struct in c [duplicate]

This question already has answers here:
C - freeing structs
(7 answers)
Closed 5 years ago.
struct a {
int a;
int b;
};
struct a* ptr = NULL;
In main
ptr = malloc(5 * sizeof(struct a ));
//assume is 5 is from user
Assign values to ptr[0].a and ptr[0].b, similarly to all 5 block
free(ptr);
Is free (ptr) enough to free all arrays of struct? Or should i free explicitly? If so, how? Thanks.
You need to free whatever is returned by any memory management function. For allocations that are nested you need to do it reverse order of the way they are allocated. (Typical example would be creation of jagged array)
void free(void *ptr);
From 7.22.3.2p2
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 a memory management function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.
In your case free(ptr) would be enough to free the dynamically allocated memory. If it contained some pointer variable to which we assigned address of dynamically allocated memory then you would need to free then first and then this ptr variable.

Is it necessary to check whether a pointer is null before free() [duplicate]

This question already has answers here:
Is it good practice to free a NULL pointer in C? [duplicate]
(4 answers)
Closed 6 years ago.
Ì used to make sure a pointer was not null before freeing it, so I would normally destroy dynamically created structs like this:
Node *destroy_node(Node *node) {
if (node) {
free(node);
}
return NULL;
}
But CERT MEM34 suggests that since free() accepts null pointers, I could as well write
Node *destroy_node(Node *node) {
free(node);
return NULL;
}
Is that correct?
Yes, passing NULL (a null-pointer constant) to free() is perfectly valid.
Quoting C11, chapter §7.22.3.3, (emphasis mine)
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. [...]
Yes, passing NULL to free is a no-op.
Excerpt from n1570 (C11 final draft):
7.22.3.3 The free function
Synopsis
1 #include void free(void *ptr);
Description
2 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 a memory management function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.
Returns
3 The free function returns no value.

Dynamic memory allocation in C

i just experiment the things on the c language
could you answer my question regarding the program i've written
void main()
{
char *p,; // created a pointer pointing to string
p = (char *) malloc(50); // dynamically create 50 bytes.
strcpy(p, "this code is written about the dynamic allocation");
p += 20;
free(p);
}
Now could anyone tell me what is the effect of free(p) statement will the last 30 bytes will be freed of and used for the future memory allocation.? what would be the output?
You are not supposed to free any addresses but those returned by malloc(), calloc() or realloc(). And p + 20 is no such address.
http://codepad.org/FMr3dvnq shows you that such a free() is likely to fail.
The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.
Does the pointer passed to free() have to point to beginning of the memory block, or can it point to the interior? is also worth reading.
Even if you could use free() on any pointer that points to a malloc'd memory - your could would free it twice since you are calling free() on more than one memory inside that area. And double-frees are evil as they can result in security holes.
It will result in Undefined Behavior.
The free() function shall cause the space pointed to by ptr to be deallocated; that is, made available for further allocation. If ptr is a null pointer, no action shall occur. Otherwise, if the argument does not match a pointer earlier returned by the calloc(), malloc(), posix_memalign(), realloc(), strdup() function, or if the space has been deallocated by a call to free() or realloc(), the behavior is undefined.

Resources