Apply for Zend Framework Certification Training

c




< Dynamic memory allocation Practice problems and solutions on malloc() in c >



Stack Memory
 
What is Stack?
Stack is a fixed-size memory region used for:
Local variables
Function calls
Parameters
Key Features:
Works in LIFO (Last In First Out) manner
Automatically managed by the system
Very fast access
Limited size
Example (C):
void fun() {
    int x = 10; // stored in stack
}
 
Real-Life Example:
Think of a stack of plates:
You put a plate → push
You remove the top plate → pop
 
Heap Memory
What is Heap?
Heap is a large memory area used for dynamic memory allocation.
 
Key Features:
Memory is allocated at runtime
Managed manually using:
malloc()
calloc()
realloc()
free()
Slower than stack
Flexible size
Example (C):
int *ptr;
ptr = (int*) malloc(sizeof(int)); // stored in heap
 
Real-Life Example:
Think of heap like a warehouse:
You request space when needed
You must return it when done
If not returned → memory leak
Stack vs Heap (Quick Comparison)
Feature Stack Heap
Allocation Automatic Manual
Speed Fast Slower
Size Limited Large
Management System Programmer
Usage Local variables Dynamic memory
Important Concept
If you forget to free heap memory:
It causes a memory leak
Program performance decreases
Simple Summary
Stack → Temporary, fast, automatic
Heap → Flexible, large, manual
  
Tricky Interview Questions on Stack vs Heap
 
1. What happens when stack memory is full?
Answer:It causes a stack overflow.
Example:
Infinite recursion
Too many local variables
 
2. What happens when heap memory is full?
Answer:malloc() / calloc() returns NULL
Program must handle it
int *ptr = malloc(sizeof(int));
if(ptr == NULL){
    printf("Memory not allocated");
}
 
3. Which is faster: Stack or Heap? Why?
Answer:Stack is faster
Because memory allocation is simple (just moving pointer)
Heap requires complex management
 
4. Can you access heap memory without a pointer?
Answer: No
Heap memory is accessed only via pointers
 
5. Where are pointers stored?
Answer:
Pointer variables → Stack
Data they point to → Heap
int *ptr = malloc(sizeof(int));
 
6. What is a memory leak?
Answer:When heap memory is allocated but not freed
Example:
int *ptr = malloc(sizeof(int));
// forgot free(ptr);
 
7. What is dangling pointer?
Answer: Pointer pointing to freed memory
int *ptr = malloc(sizeof(int));
free(ptr);
// ptr is now dangling
 
8. What is stack overflow vs heap overflow?
Answer: Type Meaning
Stack Overflow Too much stack usage (recursion)
Heap Overflow Writing beyond allocated heap memory
 
9. Can stack memory be manually freed?
Answer: No
It is automatically managed
 
10. Why is heap memory preferred for large data?
Answer:Stack size is limited
Heap can allocate large memory dynamically
 
11. What is segmentation fault related to heap?
Answer: Occurs when:
Accessing freed memory
Accessing invalid pointer
Out-of-bounds access
 
12. What is difference between static and dynamic memory allocation?
Answer:
Static → Stack (compile time)
Dynamic → Heap (runtime)
 
13. What will be the output?
int *ptr;
{
    int x = 10;
    ptr = &x;
}
printf("%d", *ptr);
Answer: Undefined behavior
x is destroyed (stack variable)
Pointer becomes invalid
 
14. Can heap memory cause fragmentation?
Answer: Yes
Small unused blocks remain scattered
 
15. Which memory is safer?
Answer:
Stack is safer (automatic)
Heap is error-prone (manual handling)

< Dynamic memory allocation Practice problems and solutions on malloc() in c >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top