MEMORY

In C programming, memory management is a critical aspect of the language, and programmers have direct control over memory allocation and deallocation. Here are some key concepts related to memory in C:

Memory Segments:

The memory in a C program is typically divided into different segments:

Text Segment: Contains the program's executable code.

Data Segment: Includes global and static variables.

Heap: Dynamically allocated memory during the program's runtime.

Stack: Stores local variables and function call information.

Variables and Pointers:

Variables in C are allocated memory based on their data types. Pointers, which store memory addresses, are used to access and manipulate memory.

 

int number = 42;  // Variable 'number' allocated in the data segment

int *ptr = &number;  // Pointer 'ptr' stores the address of 'number'

Dynamic Memory Allocation:

  • C provides functions like malloc(), calloc(), realloc(), and free() for dynamic memory allocation and deallocation. The memory allocated on the heap allows for flexibility in managing storage during runtime.

 

#include<stdio.h>

void main()

{

  //if we are using “principle” variable for printing

  int principle;

  printf(“%d”,principle);

  //principle->value

  //&principle->location of variable “principle” in memory

  //when we do scanf with &principle whatever value we input will get set to location of principle variable

  scanf(“%d”,&principle);             //input 5000

  printf(“%d”,principle);               //output 5000

}