0% found this document useful (0 votes)
27 views

Memory Allocation

Memory allocation notes

Uploaded by

frankodivya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Memory Allocation

Memory allocation notes

Uploaded by

frankodivya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

POINTERS

➢ Pointer is a variable, which stores the address of another variable, i.e.,


address of the memory location.
➢ A pointer is a derived data type in C. Like any variable or constant, you must
declare a pointer before using it to store any variable address.
➢ Use: Pointers can be used to access and manipulate data stored in the
memory.

Explanation
Consider the declaration, int i = 3 ;
This declaration tells the C compiler to:
a. Reserve space in memory to hold the integer value.
b. Associate the name i with this memory location.
c. Store the value 3 at this location.

• The expression &i gives the address of the variable i.


• This address can be collected in a variable, by saying, j = &i ;
• But remember that j is not an ordinary variable like any other
integer variable!
• It is a variable that contains the address of another variable (i in this case).
• We can’t use j in a program without declaring it. And since j is a
variable that contains the address of i, it is declared as
int *j ;

Meaning of *(it is known as dereference operator )

It stands for ‘value at address’.


*j would mean, the value at the address contained in j.
Note that printing the value of *( &i ) is same as printing the value of i.

main( ) output:
{

1
int i = 3 ;
int *j ;
j = &i ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nAddress of i = %u", j ) ;
printf ( "\nAddress of j = %u", &j ) ;
printf ( "\nValue of j = %u", j ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", *( &i ) ) ;
printf ( "\nValue of i = %d", *j ) ;
}
Memory allocation in c refers to the process of reserving space in
memory for variables, arrays, or dynamically created data during the
execution of a program.

• Static Memory Allocation (Compile-time)


• Dynamic Memory Allocation (Run-time)
In static memory allocation, the memory for variables and arrays is
allocated at compile time. The size and type of the memory blocks are known
and reserved during the program compilation. Once allocated, this memory
cannot be freed or resized during the program's execution.
Examples: Global variables, local variables (within functions), and arrays with
predefined sizes.
Key Characteristics:
• Fixed size.
• Allocated when the program starts.
• Memory is automatically freed when the program exits (or when the
function returns for local variables).

• int arr[10]; // Static array of 10 integers


In the example above, the array arr is statically allocated memory for 10 integers.

Dynamic Memory Allocation


Dynamic memory allocation enables to allocate memory at run time.

• Static allocation of an array allows to reserve a fixed amount of memory


(specified as array size) at compile-time
• The size of array we have declared initially can be sometimes insufficient
and sometimes more than required.
• To solve this issue, we can allocate memory manually during run time. This
is known as dynamic memory allocation in C programming.
• Dynamic memory allocation in C is a process that allows you to
allocate and deallocate memory during the execution of a program

Dynamic memory allocation in C language is possible by 4 functions.


2
1. malloc()
2. calloc()
3. realloc()
4. free()
1. malloc() function

• malloc: Stands for "memory allocation." It is used to allocate a specified


number of bytes of memory . It returns a pointer to the first byte of the
allocated memory.
• It returns a pointer of type void which can be cast into a pointer of any
form.

Syntax:
ptr = (cast-type*) malloc(byte-size);

Example:
ptr = (int*) malloc(100 * sizeof(int));

Since the size of int is 2 bytes, this statement will allocate 200 bytes of memory.
And, the pointer ptr holds the address of the first byte in the allocated memory.
If space is insufficient, allocation fails and returns a NULL pointer.

2. calloc() function

“calloc()” (contiguous allocation) function in C is used to dynamically allocate the


specified number of blocks of memory of the specified type. It is very much
similar to malloc() but has two different points and these are:

a) It initializes each block with a default value ‘0’.


b) It has two parameters or arguments as compared to malloc().

Syntax:
ptr = (cast-type*)calloc(n, element-size);
here, n is the no. of elements and element-size is the size of each element.

Example:
ptr = (float*) calloc(25, sizeof(float));

This statement allocates contiguous space in memory for 25 elements each with
the size of the float.
3
3. realloc() function
• “realloc” or “re-allocation” method is used to dynamically change the
memory allocation of a previously allocated memory.
• If the memory previously allocated with the help of malloc() or calloc() is
insufficient, realloc() can be used to dynamically re-allocate memory.
• Re-allocation of memory maintains the already present value and new
blocks will be initialized with the default garbage value.

Syntax:
ptr = realloc(ptr, newSize);

where ptr is reallocated with new size 'newSize'. If space is insufficient, allocation
fails and returns a NULL pointer.

4. free() function
• Used to deallocate memory that was previously allocated with malloc, calloc,
or realloc. It marks the memory as available for reuse.
• The memory allocated using functions malloc() and calloc() is not de-
allocated on their own. Hence the free() method is used, whenever the
dynamic memory allocation takes place. It helps to reduce wastage of
memory by freeing it.

Syntax:
free(ptr);

Sample program to implement malloc() function


#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc
if(ptr= =NULL)
{
printf("Sorry! Unable to allocate memory");
4
exit(0);
}
printf("Enter the elements");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d", sum);
free(ptr);
return 0;
}
Output
Enter number of elements: 3
Enter the elements : 10
10
10
Sum=30
Sample program to implement calloc() function
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements:");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int)); //memory allocated using calloc
if(ptr==NULL)
{
printf("Sorry! Unable to allocate memory");
exit(0);
}
printf("Enter elements of array:");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}
Output
5
Enter elements of array: 3
Enter elements of array: 10
10
10
Sum=30

Sample program to implement realloc() function


#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n, i;
n = 5;
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL)
{
printf("Memory not allocated.\n");
exit(0);
}

for (i = 0; i < n; ++i)


ptr[i] = i + 1;
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
printf("%d, ", ptr[i]);
// Get the new size for the array
n = 10;
ptr = realloc(ptr, n * sizeof(int));
for (i = 5; i < n; ++i)
ptr[i] = i + 1;
printf("The elements of the array are: ");
for (i = 0; i < n; ++i)
printf("%d, ", ptr[i]);
free(ptr);
return 0;
}

Output:
The elements of the array are: 1, 2, 3, 4, 5,
The elements of the array are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

You might also like