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

DS-Static and Dynamic Memory Concept

Static memory allocation assigns a fixed amount of memory that cannot be changed. Dynamic memory allocation allows the size to change depending on the program's needs using functions like malloc(), calloc(), realloc(), and free(). The key differences are that static allocation is simpler but wastes memory, while dynamic allocation is more complex but saves memory by allowing it to grow and shrink as needed.

Uploaded by

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

DS-Static and Dynamic Memory Concept

Static memory allocation assigns a fixed amount of memory that cannot be changed. Dynamic memory allocation allows the size to change depending on the program's needs using functions like malloc(), calloc(), realloc(), and free(). The key differences are that static allocation is simpler but wastes memory, while dynamic allocation is more complex but saves memory by allowing it to grow and shrink as needed.

Uploaded by

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

Static and Dynamic memory concept

What is Static Memory Allocation?

In static memory allocation, the allocated memory is fixed. Once the memory is
allocated, it cannot be changed. The memory cannot be increased or decreased. For
example, in C language, int x; means that the variable can store an integer value. The
number of bytes depends on the computer.

There can also be arrays. E.g. int x [5]; This x is an array which can store a sequence of
data which are of the same type. It can store five integer elements. It cannot store
more than five elements. In Java, an array can be created as , int arr[] = new int[5];
The array ‘arr’ can store 5 integer values and cannot store more than that.

In static memory allocation, once the variables are allocated, they remain permanent.
After the initial allocation, the programmer cannot resize the memory. If the
programmer allocated an array that can store 10 elements, it is not possible to store
values more than that specified amount. If the programmer initially allocated an
array that can hold 10 elements, but only needed 5 elements, then there is a memory
wastage. That memory is no longer needed, but it is also not possible to reuse the
memory. Static memory allocation is fixed but the implementation is simple and easy,
and it is also fast.

What is Dynamic Memory Allocation?

Sometimes it is necessary to change the size of the memory. So memory can be


allocated dynamically. Depending on insertions and deletes of the data elements, the
memory can grow or shrink. It is known as dynamic memory allocation.

In C language, stdlib.h header file, there are four functions for dynamic memory
allocation. They are calloc(), malloc(), realloc() and free(). The function malloc()
allocates a required size of bytes and returns a void pointer, pointing the first byte of
the allocated memory. The function calloc() allocates a required size of bytes and
initialize them to zero. Then returns a void pointer to the memory. The free() function
is used to reallocate the allocated memory. And realloc() function can modify the
previously allocated memory. After allocating memory using calloc() or malloc(), the
memory size is fixed, but they can be increased or decreased using the realloc()
function.

The main advantage of dynamic memory allocation is that it saves memory. The
programmer can allocate memory or release the memory as necessary. Memory can
be reallocated during execution and can free the memory when it is not required.

Dynamic memory allocation is also efficient than static memory allocation.

One disadvantage is that implementing dynamic memory allocation is complex.

What is the Difference between Static and Dynamic Memory Allocation?

Static vs Dynamic Memory Allocation


Static memory allocation is a Dynamic memory allocation is a method of
method of allocating memory, allocating memory, and once the memory is
and once the memory is allocated, it can be changed.
allocated, it is fixed.
Modification
In static memory allocation, it is In dynamic memory allocation, the memory can be
not possible to resize after initial minimized or maximize accordingly.
allocation.
Implementation
Static memory allocation is easy Dynamic memory allocation is complex to
to implement. implement.
Speed
In static memory, allocation In dynamic memory, allocation execution is slower
execution is faster than dynamic than static memory allocation.
memory allocation.
Memory Utilization
In static memory allocation, Dynamic memory allocation allows reusing the
cannot reuse the unused memory. The programmer can allocate more
memory. memory when required . He can release the
memory when necessary.
malloc()

The name "malloc" stands for memory allocation.

The malloc() function reserves a block of memory of the specified number of bytes.
And, it returns a pointer of void which can be casted into pointers of any form.

Syntax of malloc()

ptr = (castType*) malloc(size);

Example

ptr = (float*) malloc(100 * sizeof(float));

The above statement allocates 400 bytes of memory. It's because the size of float is 4
bytes. And, the pointer ptr holds the address of the first byte in the allocated
memory.
The expression results in a NULL pointer if the memory cannot be allocated.

calloc()

The name "calloc" stands for contiguous allocation.

The malloc() function allocates memory and leaves the memory uninitialized.
Whereas, the calloc() function allocates memory and initializes all bits to zero.

Syntax of calloc()

ptr = (castType*)calloc(n, size);

Example:

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

The above statement allocates contiguous space in memory for 25 elements of type
float.

free()

Dynamically allocated memory created with either calloc() or malloc() doesn't get
freed on their own. You must explicitly use free() to release the space.
Syntax of free()

free(ptr);

This statement frees the space allocated in the memory pointed by ptr.

realloc()

Changes memory size that is already allocated dynamically to a variable.

Syntax:

void* realloc(pointer, new-size)

Example: realloc()

int *x;

x = (int*)malloc(50 * sizeof(int));

x = (int*)realloc(x,100); //allocated a new memory to variable x

/*C program for malloc() */


Include<stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char name[20];
char *address;

strcpy(name, "pcjabin");
address = (char*)malloc( 50 * sizeof(char) ); /* allocating memory dynamically */
strcpy( address, "vidyanagar, hubli-30");

printf("Name = %s\n", name );


printf("Address: %s\n", address );
return 0;
}
Output:
Name = pcjabin
Address: vidyanagar, hubli-30

In the above example, value for name is assigned and printed. For address, estimated
that the number of characters should be around 50. So, the size of address will be 50
* sizeof(char).
char *address → Here, declared a pointer to character for address without specifying
how much memory is required.

address = (char*)malloc( 50 * sizeof(char) ) → By writing this, assigned a memory of


'50 * sizeof(char)' bytes for address. We used (char*) to typecast the pointer
returned by malloc to character.

strcpy( address, "Lee Fort, 11-B Sans Street") → By writing this, finally assigned the
address.

Another example

#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,*p;
printf("Enter number of elements: ");
scanf("%d",&n);
p=(int*)malloc(n * sizeof(int)); //memory allocated using malloc
if(p == NULL)
{
printf("memory cannot be allocated\n");
}
else
{
printf("Enter elements of array:\n");
for(i=0;i<n;++i)
{
scanf("%d",&*(p+i));
}
printf("Elements of array are\n");
for(i=0;i<n;i++)
{
printf("%d\n",*(p+i));
}
}
return 0;
}
Output
Enter number of elements:
5
Enter elements of array:
1
2
3
4
5
Elements of array are
1
2
3
4
5
In this example, firstly, declared a pointer 'p' of type int which contains n elements.
Thus, 'p' is an integer array containing n elements. So, assigned a memory of size n *
sizeof(int) to the array which the pointer 'p' is pointing to. Thus, now have a memory
space allocated to the integer array consisting of 'n' elements. Further, ask the user
to input the values of the elements of the array and display those values.

/* C peorgam for calloc()*/


#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,*p;
printf("Enter number of elements: ");
scanf("%d",&n);
p=(int*)calloc(n, sizeof(int)); //memory allocated using malloc
if(p == NULL)
{
printf("memory cannot be allocated\n");
}
else
{
printf("Enter elements of array:\n");
for(i=0;i<n;++i)
{
scanf("%d",&*(p+i));
}
printf("Elements of array are\n");
for(i=0;i<n;i++)
{
printf("%d\n",*(p+i));
}
}
return 0;
}

Output
Enter number of elements:
5
Enter elements of array:
1
2
3
4
5
Elements of array are
1
2
3
4
5
So, this is same as the example of malloc, with a difference in the syntax of calloc.
Here (int*)calloc(n, sizeof(int)), where n is the number of elements in the integer
array (5 in this case) and sizeof(int) is the size of each of that element. So the total
size of the array is n * sizeof(int).

Calloc() initializes the allocated memory to zero value whereas malloc doesn't.
/* realloc() */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *p1;
int m1, m2;
m1 = 10;
m2 = 30;
p1 = (char*)malloc(m1);
strcpy(p1, "computer");
printf("%s\n", p1);
p1 = (char*)realloc(p1, m2);
strcat(p1, "science");
printf("%s\n", p1);
return 0;
}

Output
Computer
computerscience
In the above example, declared a pointer 'p1' which will be used to dynamically
allocate a memory space.

p1 = (char*)malloc(m1) → By writing this, assigned a memory space of 10 bytes


which the pointer 'p1' is pointing to. We used (char*) to typecast the pointer
returned by malloc() to character.

strcpy(p1, "Computer") → This assigns a string value "Computer" to the memory


which the pointer p1 is pointing to. Currently, the memory space is 10 bytes which
can easily store the string "Computer", but what if now we want that memory space
to store the string "Computerscience"? For this, need to expand the size of our
memory space which we can easily do with realloc().

/* free() */
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,*p;
printf("Enter number of elements:\n");
scanf("%d",&n);
p=(int*)malloc(n * sizeof(int));
if(p == NULL)
{
printf("memory cannot be allocated\n");
}
else
{
printf("Enter elements of array:\n");
for(i=0;i<n;++i)
{
scanf("%d",&*(p+i));
}
printf("Elements of array are\n");
for(i=0;i<n;i++)
{
printf("%d\n",*(p+i));
}
}
free(p);
return 0;
}
Output
Enter number of elements:
5
Enter elements of array:
1
2
3
4
5
Elements of array are
1
2
3
4
5
So here by writing free(p), release the memory which was dynamically allocated using
malloc

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy