Skip to main content

C Programming Interview Questions With Answers - Part VII.


43. What Is Static Memory & Dynamic Memory Allocation?

Answer: Compiler allocates memory space for a declared variable. By using the address of operator, the reserved address is obtained and this address is assigned to a pointer variable. This way of assigning pointer value to a pointer variable at compilation time is known as static memory allocation.

A dynamic memory allocation uses functions such as malloc() or calloc() to get memory dynamically. If these functions are used to get memory dynamically and the values returned by these function are assigned to pointer variables, such a way of allocating memory at run time is known as dynamic memory allocation.


44. What Is Dynamic Memory Allocation? Explain The Various Memory Allocation Function With Its Task.

Answer: The mechanism of allocating required amount of memory at run time is called dynamic allocation of memory. Sometimes it is required to allocate memory at runtime. When we declare array in any program, the compiler allocates memory to hold the array.

Now suppose the numbers of items are larger than the defined size then it is not possible to hold all the elements and if we define an array large enough and data to be stored is less, in that case the allocated memory is wasted leading to a need for dynamic memory allocation. In this mechanism, we use a pointer variable to which memory is allocated at run time.


45. What Are The Three Dynamic Memory Allocation Functions?

Answer: Three dynamic memory allocation functions are: malloc, calloc and free.


46. Explain The Various Memory Allocation Function With Its Task In C Programming.

Answer: The various memory allocation functions are described below:
  • malloc(): It is a memory allocation function that allocates requested size of bytes and returns a pointer to the first byte of the allocated space. The malloc function returns a pointer of type void so we can assign it to any type of pointer. It takes the following form:
ptr = (cast type *) malloc(byte-size);
where ptr is a pointer of type cast-type.

For example, the statement

x = (int *) malloc(10 *sizeof(int)) means that a memory space equivalent to 10 times the size of an int byte is reserved and the address of the first byte of memory allocated is assigned to the pointer x of int type.

The malloc function can also allocate space for complex data types such as structures. For example:
ptr= (struct student*) malloc(sizeof (struct student));
where ptr is a pointer of type struct student.
  • calloc(): It is another memory allocation function that allocates space for an array of elements, initializes them to zero and then returns a pointer to the memory. This function is normally used for requesting memory space at run time. While malloc allocates a single block of storage space, calloc allocates multiple block of storage, each of the same size, and then sets all bytes to zero. It takes the following form:
ptr = (cast type *) calloc(n,element-size);

This statement allocates contiguous space for n blocks, each of size element-size bytes.
  • realloc(): realloc is a memory allocation function that modifies the size of previously allocated space. Sometime it may happen that the allocated memory space is larger than what is required or it is less than what is required. In both cases, we can change the memory size already allocated with the help of the realloc function known as reallocation of memory. For example, if the original allocation is done by statement
ptr = malloc(size);

then reallocation is done by the statement

ptr = realloc(ptr,newsize); which will allocate a new memory space of size newsize to the pointer variable ptr and returns a pointer to the first byte of the new memory block.


47. What Are The Differences Between Malloc() & Calloc()?

Answer: Differences between malloc() and calloc() are:
  • The malloc() function is used to allocate memory space. It reserves a memory space of specified size and gives the starting address to pointer variable whereas calloc() function is used to allocate multiple blocks of memory. This has two arguments. The calloc() function is generally used for allocating the memory space for arrays and structures.
  • Malloc takes one argument Malloc(a);where a represents number of bytes whereas Calloc takes two arguments Calloc(b,c) where b is the no of object and c represents size of object.
  • Calloc initializes the contains of block of memory to zeros whereas memory allocated using malloc contains garbage values.
  • Memory allocated using Malloc malloc function is contiguous whereas memory allocated using calloc is not contiguous.


48. What Is The Purpose Of Realloc?

Answer: It increases or decreases the size of dynamically allocated array. The function realloc (ptr, n) uses two arguments. The first argument ptr is a pointer to a block of memory for which the size is to be altered.

The second argument specifies the new size. The size may be increased or decreased. If sufficient space is not available to the old region the function may create a new region.


49. What Is The Purpose Of The Free() Function?

Answer: For efficient use of memory space, the memory space that is not required should be released. The free() function achieves this.


50. How To Use Realloc() To Dynamically Increase Size Of An Already Allocated Array?

Answer: The function realloc() can be used to change the size of the memory block reserved for a particular object. This is known as reallocation of memory.

The function realloc() moves the content of old block into the new block and the data of the old block is not lost.


51. In Which Header File Are The Memory Management Functions Defined?

Answer: Memory management functions which are used for allocating and freeing memory during execution of a program. These functions are defined in stdlib.h.


52. How Much Maximum Memory Can Be Allocated In A Single Call To Malloc() ?

Answer: 64 KB


53. What Are The Differences Between Formal Arguments & Actual Arguments Of A Function?

Answer: Formal arguments are parameters or arguments in a function declaration. The scope of such arguments is local to the function definition. Any change made to the formal arguments will not be reflected back in the main program.

Actual arguments are arguments that are passed in a function call. Such arguments are defined inside the function body.




Check Out Other Questions:

36) What is the purpose of the free() function?
37) How to use realloc() to dynamically increase size of an already allocated array?
38) What is the purpose of the sizeof() operator?
39) What is a wild pointer in C?
40) What is the purpose of the do-while loop in C?


41) What is the size of the void pointer in C?
42) What is a nested loop in C?
43) What are the operations performed on C++ lists?
44) How is searching in an array list carried out?
45) Where can an element are inserted in an array list?


...Return To C Questions INDEX.

...Return To C Programming INDEX.


... Return To HR Interview Index

Comments

Popular posts from this blog

Tell Me Something About Yourself - Interview Answers.

Try to introduce some of your most important employment-oriented skills as well as your education and accomplishments to the interviewer. Answer to this question is very important because it positions you for the rest of the interview . That's why this statement is often called the "Positioning Statement". One should take the opportunity to show his/her communication skills by speaking clearly and concisely in an organized manner. Since there is no right or wrong answer for this question hence it is important to appear friendly. YOUR ANSWERS CAN BE: 1) I am a person with strong interpersonal skills and have the ability to get along well with people . I enjoy challenges and looking for creative solutions to problems. 2) Besides the details given in my resume, I believe in character values, vision and action. I am quick in learning from mistakes. I am confident that the various tests that you have conducted will corroborate my competencies apti...

HR Interview Questions With Simple Answers - Top 30.

Here are some very important HR questions which are often asked during Interviews and I believe these answers can really help you to get through....... Click on them to view the answer: 1) Tell Me Something About Yourself ? 2) What Are Your Strengths? 3) What Are Your Weaknesses? 4) How Do You Handle Pressure / Can You Work Well Under Pressure? 5) What Are Your Short Term Goals? 6) What Are Your Long Term Goals? 7) Where Do You See After 5 Years? 8) Why Should We Hire You? 9) What Is Your Salary Expectation? 10) Why Do You Want To Leave Your Current Job? 11) Do You Prefer To Work Alone Or As A Team Player? 12) What Made You Choose Your Major / Stream? 13) Why Didn’t You Pursue A Career In Your Major / Stream? 14) Why Do You Want To Work For Us? 15) Are You Willing To Travel? 16) Are You Willing To Take Risks? 17) What Do You Know About This Company? 18) What Do You Seek From A Job? 19) How Do You Evaluate Success? 20) Dur...

Sample Cover Letter / Job Application.

To The General Manager (HR) [ Name and Designation (Bold Words)] Infosys Limited Delhi – 110001 Uttam Agrawal Gandhi Chowk, Bhartee Street [ Name Bhawanipatna, Kalahandi Contact Address Orissa – 766001 Telephone No.] Ph. no - +91-9438170446 [Sub: Application for the position of ___________ in your esteemed organization.] Respected Sir, (Always use ‘Respected’) With reference to your advertisement dated 14th may’ 08 in The Times of India for the position of ___________ . I would like to place myself as a strong contender before you. OR This has reference to your advertisement dated 14th may’ 08 in The Times of India for the position of ____________. In the said connection I take this opportunity to apply for the said position as a strong contender. /*This is the Introduction i.e the first paragraph of your application/* ( Now the Second Paragraph is the Self Introduction) Try to start the second paragraph with the below mentioned lines: - I have the p...