Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Monday, March 9, 2020

Memory allocation in C

Memory allocation: The process of allocating memory spaces in the RAM is called memory allocation. There are two basic types of memory allocation

Memory Allocation | welcome2protec.com

C language provides many functions that come in header files to deal with allocation and management of memories. In this tutorial you will find brief information about managing memory in your program using some functions and their respective header files.

All the variables used in your program gets memory location along with the program itself in the RAM. When a variable gets assigned in a memory in one program, that memory location cannot be used by another variable or another program. So, C language gives us a technique of allocating memory to different variables and program.


Memory allocation:

The process of allocating memory spaces in the RAM is called memory allocation. The allocation is done either before or at the time of program execution. This eventually allocates memory for the variables declared by a programmer like you and me.

There are two basic types of memory allocation:

1) Static Memory Allocation (SMA):

When a variable is declared compiler automatically allocates memory for it in the RAM. The compiler knows about the amount of memory that would be taken by these variables at compile time only. But please note, the actual memory allocation happens during the program startup not during compile time which is a common misconception.

Since the compiler knows about the amount of memory to be allocated for these variables at compile time therefore, it is called compile time memory allocation or static memory allocation. When memory allocation is done it stays the same throughout the entire run of your program. Neither any changes will be there in the amount of memory nor any change in the location of memory.

All the automatic variables (variables local to a function) and information about function are get memory on the stack area. You can allocate the memory on the stack by simply writing like below:

int myint = 6; 
char mychar;

Note: The memory occupied by Global variables, Static variables of functions, Static variables of file scope. These all variables are stored in data segment and cannot be freed until the program termination.


2) Dynamic Memory Allocation (DMA):

The process of allocating memory at run time is called dynamic memory allocation or run time memory allocation. It has the facilities to grow and shrink the amount of memory. In addition we can also release and free the memory when not required or used.

In C language, dynamic memory allocation is possible by four standard library functions of stdlib.h header file. These function are:

  • malloc()
  • calloc()
  • realloc()
  • free()

malloc() - Stands for Memory Allocation:

malloc() function is used to allocate memory of the specified number of bytes at run time (while the program is getting executed). And it returns a pointer of void type which can be type casted in any type.

malloc() function does not initialize the memory allocated at run time. It contains garbage value.

Syntax of malloc():

Syntax:
  ptr = (cast type*) malloc(number * sizeof(dataType));

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

The above statement allocates 40 bytes of memory. It's because the size of int will be 4 or 2 bytes depending on the compiler, and it is typecated to int type since, ptr is a pointer variable of int type which hold address returned by malloc(). And we know that, malloc() returns address of unknown type (viod type).


calloc() - Stands for Contiguous Allocation:

Both calloc() and malloc() functions are similar. Only difference is that, malloc() function allocates memory and leave it uninitialized. Whereas, calloc() function allocates memory and initializes all block to zero.

Syntax of calloc():

Syntax:
  ptr = (castType*) calloc(number, sizeof(dataType));
  
Example:
  float *ptr;
  ptr = (float*) calloc(10, sizeof(float));

Description:

ptr: is a pointer variable of float type(It could be any type for example: int, char, double etc.) which is used to hold address returned by malloc() or calloc() functions.

(float*): It is type casted to float type since ptr is pointer of float type and we knoe that, malloc() and malloc() returns address of unknown type (void type).

number: The number represents number of required block.

sizeof(dataType): The sizeof() operator is used to get the size of a variable or datatype.

calloc() function is generally used for allocating memory for derived data type such as arrays, structures etc. See the below example.

Let's understand with a simple example at below:

struct employee{
  char *name;
  float salary;
};
    
  typedef struct employee emp;
  emp *e1
  e1 = (*emp) calloc(20, sizeof(emp));

realloc() - stands for Reallocation:

realloc() function is used to resize the allocated memory using calloc() or malloc() functions without losing old data.

For Example: Suppose you have already allocated 40 bytes of memory later on you need more 80 bytes of memory spaces so in this situation you can use realloc() function to reallocate more memory spaces.

Syntax of realloc():

Syntax:
  ptr = realloc(pointer_name, number * sizeof(dataType));
  
Example:
  float *ptr;
  ptr = (float*) realloc(ptr, 20 * sizeof(float));

free():

free() function is used to free(Also can say de-allocate) the allocated memory by malloc() or calloc() functions and return it to heap so that it can be used for other purpose.

For Example:

free(ptr);

The above statement free the space allocated in the memory pointed by ptr.

--

--



Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us