C – Memory Management – GENCOR

C – Memory Management

This chapter explains dynamic memory management in C. The C programming language provides several functions for memory allocation and management. These functions can be found in the <stdlib.h> header file.

S.N. Function & Description
1 void *calloc(int num, int size);

This function allocates an array of num elements each of which size in bytes will be size.

2 void free(void *address);

This function releases a block of memory block specified by address.

3 void *malloc(int num);

This function allocates an array of num bytes and leave them initialized.

4 void *realloc(void *address, int newsize);

This function re-allocates memory extending it upto newsize.

Allocating Memory Dynamically

While programming, if you are aware of the size of an array, then it is easy and you can define it as an array. For example, to store a name of any person, it can go up to a maximum of 100 characters, so you can define something as follows −

char name[100];

But now let us consider a situation where you have no idea about the length of the text you need to store, for example, you want to store a detailed description about a topic. Here we need to define a pointer to character without defining how much memory is required and later, based on requirement, we can allocate memory as shown in the below example −

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {

   char name[100];
   char *description;

   strcpy(name, "GenCor India");

   /* allocate memory dynamically */
   description = malloc( 200 * sizeof(char) );
	
   if( description == NULL ) {
      fprintf(stderr, "Error - unable to allocate required memory\n");
   }
   else {
      strcpy( description, "GenCor India is a Software Institute in India");
   }
   
   printf("Name = %s\n", name );
   printf("Description: %s\n", description );
}

When the above code is compiled and executed, it produces the following result.

Name = GenCor India
Description: GenCor India is a Software Institute in India

Same program can be written using calloc(); only thing is you need to replace malloc with calloc as follows −

calloc(200, sizeof(char));

So you have complete control and you can pass any size value while allocating memory, unlike arrays where once the size defined, you cannot change it.

Resizing and Releasing Memory

When your program comes out, operating system automatically release all the memory allocated by your program but as a good practice when you are not in need of memory anymore then you should release that memory by calling the function free().

Alternatively, you can increase or decrease the size of an allocated memory block by calling the function realloc(). Let us check the above program once again and make use of realloc() and free() functions −

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {

   char name[100];
   char *description;

   strcpy(name, "GenCor India");

   /* allocate memory dynamically */
   description = malloc( 30 * sizeof(char) );
	
   if( description == NULL ) {
      fprintf(stderr, "Error - unable to allocate required memory\n");
   }
   else {
      strcpy( description, "GenCor India is a Software Institute in India.");
   }
	
   /* suppose you want to store bigger description */
   description = realloc( description, 100 * sizeof(char) );
	
   if( description == NULL ) {
      fprintf(stderr, "Error - unable to allocate required memory\n");
   }
   else {
      strcat( description, "It is in India");
   }
   
   printf("Name = %s\n", name );
   printf("Description: %s\n", description );

   /* release memory using free() function */
   free(description);
}

When the above code is compiled and executed, it produces the following result.

Name = GenCor India
Description: GenCor India is a Software Institute in India, It is In India.  

You can try the above example without re-allocating extra memory, and strcat() function will give an error due to lack of available memory in description.

I am happy to be here in Gencor which provided me with technical professional and skillful aspects of auto cad, thank you.

Kush Prakash - West Bengal University of Technology

I completed CCNA traning and certification preparation before appearing for ccna global examination 200-120 from gencor.

Amit Kumar - Lovely Professional University

The trainer has good knowledge of the subject.

Saurabh Kumar Suman - Shaheed Bhagat Singh State Technical Campus, Ferozepur

This institute is the best institute in web designing. Trainer sir is good in knowledge and best trained and  provided web designing knowledge to me.

Abhishek Kumar - Guru Gobind Singh Polytechnic College, Talwandi Sabo, (Bhathinda) Punjab

The trainer is good, so I think everything is good. 

Rabindra Kumar Yadav - Samalkha Group of Institutions

The Gencor classes is good for getting the knowledge. The trainer has good communication with students. I completed CAD training in Mechanical from here.

Akash Kumar - Jawaharlal Nehru Technological University, Hyderabad

I gained a lot here in the Gencor and I believe that it will help to enhance my knowledge and personalty further in my life.

Prem Kumar - BIT Sindri

Institute Completed my training as per  Syllabus provided on time. 

Akshay Kumar - The Institution of Civil Engineers, Delhi

Gencor institute is the best institute which completed my course on Civil CAD on time.

Amit Kumar - Reg No - 001/15348 - Civil CAD

The overall system of teaching is favorable and good as I expected and helped a lot in increasing my knowledge.

Sunny Kumar

I appreciate the course by the institute and efforts put in to complete it within the prescribed time limits.

Mohammad Asad Eqbal

The trainer has good knowledge of autocad, so I am happy to complete the training of AutoCAD from here.

Hemant Raj - Reg No.: 001/15307 - Civil CAD

The Trainer is nice one. He teaches well. The institute is good. I have faith on the Institute.

Vikash Kr Mandal - Reg No.: 001/15308 - Mechanical CAD

The institute is very good one and object oriented, focused on setting  up career for engineering students. The trainer is having enough knowledge to guide students.Teaching method is very much appreciated and interactive.

Indranil Mazumdar - Reg No.: 001/15312 - Civil CAD

All class I did here was very good. Faculty provide every detail about syllabus & Course. I am thankful of you.

Sumank Saurav - Reg No.: 001/15321 - Mechanical CAD

Trainer is the best guide for autocad and institute is also best for autocad

Vikash Kumar Sharma - Reg No.: 001/15322 - Mechanical CAD

This is professional institute , It has given the full knowledge of the subject, It is best best providing knowledge.

Dinu Kumar - Reg No.: 001/15328 - AutoCAD ME

This institute given me knowledge about Autocad and I appreciate that . I must recommend other to learn CAD from here.  

Abhishek Kr Singh (Reg No . 001/15332)

I am Gracy Pradhan , completed my  Linux training on RHCE from GenCor. Training is fully practical based and full syllabus coverage prescribed RED Hat Inc. . This training is surely going to provide me help in placement by college.

Gracy Pradhan - Ram Krishna Dharmarth Foundation University

I completed my Autocad Civil Training with Project and certification from GenCor InfoEdge – India, Trainer is The best in providing training .

Sonu Kumar

Best Training for telecommunication on Cisco CCNA. I got placed in 3i infotech Patna.