I am new in C programming language so can you tell me if this is correct way to do.
for example:
program points on buffer and i use that pointer
as parameter in free()
function. So, what problems can this function cause ?
I am new in C programming language so can you tell me if this is correct way to do.
for example:
program points on buffer and i use that pointer
as parameter in free()
function. So, what problems can this function cause ?
You should call free only on pointers which have been assigned memory returned by malloc
, calloc
, or realloc
.
char* ptr = malloc(10);
// use memory pointed by ptr
// e.g., strcpy(ptr,"hello");
free(ptr); // free memory pointed by ptr when you don't need it anymore
Things to keep in mind:
Never free memory twice. This can happen for example if you call free
on ptr
twice and value of ptr
wasn't changed since first call to free
. Or you have two (or more) different pointers pointing to same memory: if you call free on one, you are not allowed to call free
on other pointers now too.
When you free a pointer you are not even allowed to read its value; e.g., if (ptr)
not allowed after freeing unless you initialize ptr
to a new value
You should not dereference freed pointer
Passing null pointer to free
is fine, no operation is performed.
Think that the computer has a whole bunch of memory not (yet) used by your program. Now you need some more memory and you ask your computer to give you some more (for example, a large buffer). Once you are done with it, you want to return it to the computer.
This memory is called the heap. You ask for memory by calling malloc()
and you return it by calling free()
;
char *buffer;
buffer = malloc(512); // ask for 512 bytes of memory
if (buffer==NULL) return -1; // if no more memory available
...
free(buffer); // return the memory again
free()
function is used to deallocate memory used by one program and move it back to available memory area so that other operating system processes can use that memory location. Also free
function takes any type of pointer that points to that memory location.
For example:
int a = 10; // suppose 2 byte is allocated ie location 1000 and 1001
Now this 2 byte of memory belongs to specific problem; hence OS will not give this memory location to another process (memory is now allocated memory not available memory)
int *ptr =&a;
/*ptr is pointer variable pointing to 1000
as it is int pointer therefore ptr++ will move pointer to 1002*/
Now if we do free(ptr)
, it will check the pointer type and depending on type free function deallocate memory in this case 2 bytes starting from 1000.
Now interesting point is your data will be there until OS allocates this memory to some other process and that process overwrites it.
Also ptr
is pointing to 1000 even after free()
function but that memory location does not belong to our program hence ptr
pointer has given new name DANGLING POINTER
.
*ptr
may or may not give the same value therefore it is better to make ptr =null
.
From the man page of free()
function:
The
free()
function frees the memory space pointed to by a pointerptr
which must have been returned by a preβ vious call tomalloc()
,calloc()
orrealloc()
. Otherwise, or iffree(ptr)
has already been called before, undefined behavior occurs. Ifptr
isNULL
, no operation is performed.
You have to use the free()
function when you are allocating the memory dynamically.
If you are using that as a static variable then it may lead to unintended behavior.
char *c=malloc(100);//allocating the 100 bytes of memory for a pointer variable c.
Here after usage of that varaible you can free that allocated memory,
free(c);
If you are declared a variable like this,
char c= malloc(100);// It is illegeal. And c will have a memory in stack.
If you free this variable,
free(c);//it will lead to system crash. Because your freeing the memory which is in stack memory.