in Technology by
How is free keyword work in C?

1 Answer

0 votes
by
When we call the memory management functions (malloc, calloc or realloc) then these functions keep extra bytes for bookkeeping. Whenever we call the free function and pass the pointer that is pointing to allocated memory, the free function gets the bookkeeping information and release the allocated memory. Anyhow if you or your program change the value of the pointer that is pointing to the allocated address, the calling of the free function gives the undefined result. ____ The allocated block ____ / \ +--------+--------------------+ | Header | Your data area ... | +--------+--------------------+ ^ | +-- Returned Address Let us see a program to understand the above concept. The behavior of the below program is not defined. #include #include int main() { char *pcBuffer = NULL; //Allocate the memory pcBuffer = malloc(sizeof(char) * 16); //make sure piBuffer is valid or not if (pcBuffer == NULL) { // allocation failed, exit from the program fprintf(stderr, "Out of memory!\n"); exit(1); } //Increment the pointer pcBuffer++; //Call free function to release the allocated memory free(pcBuffer); return 0; } OutPut: Undefined Result

Related questions

0 votes
    What is use of keyword auto C Programming?...
asked Jan 17, 2021 in Technology by JackTerrance
+1 vote
    What is the purpose of the keyword typedef in C Progamming?...
asked Nov 9, 2020 in Technology by JackTerrance
+1 vote
    What are the valid places for the keyword break to appear in C-Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
+1 vote
    What is keyword auto for in C-programming?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    Which keyword is used to define the macros in c++? a) #macro b) #define c) macro d) define...
asked Oct 19, 2022 in Education by JackTerrance
0 votes
    Most organisms cannot use the free form of (a) Oxygen (b) Nitrogen (c) Carbon dioxide (d) ... Science,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    Lotus is an example of a. Free-floating plant b. Fixed floating plant c. Submerged plants d. None...
asked May 16, 2021 in General by JackTerrance
0 votes
    How can a nucleus lose electrons ( β β -particles) even though free electrons are not present in it? Select the correct answer from above options...
asked Jan 2, 2022 in Education by JackTerrance
0 votes
    Which one of the following is not a keyword in Python language? a) pass b) eval c) assert d) nonlocal...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    Which one of the following is not a keyword in Python language? a) pass b) eval c) assert d) nonlocal...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    Which keyword is used for Inheritance in TypeScript? (1)extends (2)defines (3)follows (4)implements...
asked Aug 26, 2021 in Technology by JackTerrance
0 votes
    Why is Google AdWords Keyword Planner not displaying actual no of searches?...
asked Mar 5, 2021 in Technology by Editorial Staff
0 votes
    What are the keyword research tools available in the market?...
asked Mar 5, 2021 in Technology by Editorial Staff
0 votes
    What is the purpose of the keyword research?...
asked Mar 5, 2021 in Technology by Editorial Staff
0 votes
    If we use a keyword in domain, Will it hit with EMD filter?...
asked Mar 5, 2021 in Technology by Editorial Staff
...