Search Placementyogi.com
 

Memory Allocation Management Objective Questions

  :: Topics ::

1

What functions can be used to allocate new memory in c? (select more than one)

A.

malloc()

B.

new()

C.

realloc()

D.

calloc()

See Answer & Explanation Lets Discuss
Correct answer is : A,C,D
Explanation

malloc, calloc and realloc can be used to allocate new memory. See below for  detail usage and explanation

 

malloc() : allocates the specified number of bytes. malloc returns a void pointer (void *) 

 

ex:  Below statement allocates 10*4=40 bytes of memory at run time on heap.        

int *ptr = (int *)malloc(10 * sizeof (int32_t));

       

calloc() : allocates the specified number of memory blocks of given size and initializes them to zero.

 

ex:  Below statement allocates 10*4=40 bytes of memory at run time on heap and initializes to zero.

int *ptr = (int*)calloc(10, sizeof(int32_t));

 

realloc() : increases,decrease and allocates the size of the specified block of memory.        

int *new_ptr=(int*)realloc(ptr,4);

ptr is the pointer to a memory block previously allocated with malloc, calloc or realloc. if ptr is null then new  block of memory is allocated.

4 is the new  size for the memory block, in bytes.If it is 0 and ptr points to an existing block of memory, the memory block pointed by ptr is deallocated and a NULL pointer is returned

 

free() : releases the specified block of memory back to the system.

free(ptr);
 
 
2

What function should be used to free the memory allocated by malloc() or calloc() ?

A.

dealloc()

B.

free()

C.

malloc()

D.

realloc()

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

free() : releases the specified block of memory back to the system.

free(ptr);
 
 
3

Which header file should be included in c program to use malloc(), calloc(), realloc() and free()?

A.

#include<stdio.h>

B.

#include<stdlib.h>

C.

#include<ctype.h>

D.

#include<assert.h>

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

Do you have any question? ask here

 
4

What is the output of following c program?

#include<stdio.h>
#include<stdlib.h>
int main()
{
  char *p;
  char *get();

  p = get();

  printf("%s",p);

  return 0;

}

char* get()
{
  char str[20]={'a','b'};

  return str;

}
A.

ab

B.

Some garbage value

C.

error: returning local variable address

D.

none of above

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

Some garbage value because str is an auto array, allocated on stack and will die when control goes back to main(). 

 

Note: all local variable in a function are destroyed when control goes out of scope. 

 

if you want to persist the value when control goes out, then declare it as static.

 

Note : static variable values are retained between functions call. 

 
5

What is the output of below c program?

#include<stdio.h>
#include<stdlib.h>
int main()
{
  char *p;
  char *get();

  p = get();

  printf("%s",p);


  return 0;

}

char* get()
{
  char *str=malloc(20*sizeof(char));

  str = "AB";

  return str;

}
A.

AB

B.

Some garbage value

C.

error: returing local variable address

D.

None of above

See Answer & Explanation Lets Discuss
Correct answer is : A
Explanation

In function get() malloc allocates memory on heap. So it remains in memory.

 

NOTE: Dynamically allocated memory(created by malloc or calloc) remains in memory until explicitly deleted by free.if not deleted it causes memory leak.

 
 

Banner to LogicGuns.com

Hide Page Information

C Memory Allocation Objective Questions and Answers

Here you can find C Memory Management or Allocation interview questions with answers and explanation.

C Memory Management or Allocation Multiple Choice Questions and Answers

Why C Memory Allocation / Management ?

In this section you can learn and practice C Memory Management or Allocation (Questions with Answers) to improve your skills in order to face the interview, competitive examination and various entrance test of TCS, Infosys, Microsoft, Google, Yahoo with full confidence.

Where can I get C Memory Management or Allocation Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved C Memory Management or Allocation questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand. All students, freshers can download C Memory Management or Allocation quiz questions with answers as PDF files and eBooks.

Where can I get C Memory Management or Allocation Interview Questions and Answers (objective type, multiple choice)?

Here you can find objective type C Memory Management or Allocation questions and answers for interview and entrance examination. Multiple choice and true or false type questions are also provided.

How to solve C Memory Management or Allocation problems?

You can easily solve all kind of questions based on C Memory Management or Allocation by practising the exercises (including shortcut methods to solve problems) given below.