Learn C Language 

Learn C Programming Language Step By Step. It’s easy to learn c programming language with real-life examples and hands-on. We are trying our best to deliver quality content.

Memory Management In C

 

[adinserter name="Block 7"]

« Previous Page                                                                                                                            Next Page »

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

Sr.No. 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 uninitialized.

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 the name of any person, it can go up to a maximum of 100 characters, so you can define something as follows:

[et_pb_dmb_code_snippet code=”Y2hhciBuYW1lWzEwMF07Cg==” _builder_version=”4.0.9″]Y2hhciBuYW1lWzEwMF07Cg==[/et_pb_dmb_code_snippet]

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 of a topic. Here we need to define a pointer to character without defining how much memory is required and later, based on the requirement, we can allocate memory as shown in the below example:

[et_pb_dmb_code_snippet code=”I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKaW50IG1haW4oKSB7CgogICBjaGFyIG5hbWVbMTAwXTsKICAgY2hhciAqZGVzY3JpcHRpb247CgogICBzdHJjcHkobmFtZSwgIkFtYW4gUmFvIik7CgogICAvKiBhbGxvY2F0ZSBtZW1vcnkgZHluYW1pY2FsbHkgKi8KICAgZGVzY3JpcHRpb24gPSBtYWxsb2MoIDIwMCAqIHNpemVvZihjaGFyKSApOwoJCiAgIGlmKCBkZXNjcmlwdGlvbiA9PSBOVUxMICkgewogICAgICBmcHJpbnRmKHN0ZGVyciwgIkVycm9yIC0gdW5hYmxlIHRvIGFsbG9jYXRlIHJlcXVpcmVkIG1lbW9yeVxuIik7CiAgIH0gZWxzZSB7CiAgICAgIHN0cmNweSggZGVzY3JpcHRpb24sICJBbWFuIHJhbyBpcyBhIERQUyBzdHVkZW50IGluIGNsYXNzIDEwdGgiKTsKICAgfQogICAKICAgcHJpbnRmKCJOYW1lID0gJXNcbiIsIG5hbWUgKTsKICAgcHJpbnRmKCJEZXNjcmlwdGlvbjogJXNcbiIsIGRlc2NyaXB0aW9uICk7Cn0=” _builder_version=”4.0.9″]I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKaW50IG1haW4oKSB7CgogICBjaGFyIG5hbWVbMTAwXTsKICAgY2hhciAqZGVzY3JpcHRpb247CgogICBzdHJjcHkobmFtZSwgIkFtYW4gUmFvIik7CgogICAvKiBhbGxvY2F0ZSBtZW1vcnkgZHluYW1pY2FsbHkgKi8KICAgZGVzY3JpcHRpb24gPSBtYWxsb2MoIDIwMCAqIHNpemVvZihjaGFyKSApOwoJCiAgIGlmKCBkZXNjcmlwdGlvbiA9PSBOVUxMICkgewogICAgICBmcHJpbnRmKHN0ZGVyciwgIkVycm9yIC0gdW5hYmxlIHRvIGFsbG9jYXRlIHJlcXVpcmVkIG1lbW9yeVxuIik7CiAgIH0gZWxzZSB7CiAgICAgIHN0cmNweSggZGVzY3JpcHRpb24sICJBbWFuIHJhbyBpcyBhIERQUyBzdHVkZW50IGluIGNsYXNzIDEwdGgiKTsKICAgfQogICAKICAgcHJpbnRmKCJOYW1lID0gJXNcbiIsIG5hbWUgKTsKICAgcHJpbnRmKCJEZXNjcmlwdGlvbjogJXNcbiIsIGRlc2NyaXB0aW9uICk7Cn0=[/et_pb_dmb_code_snippet]

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

[et_pb_dmb_code_snippet code=”TmFtZSA9IEFtYW4gUmFvCkRlc2NyaXB0aW9uOiBBbWFuIFJhbyBpcyBhIERQUyBzdHVkZW50IGluIGNsYXNzIDEwdGg=” _builder_version=”4.0.9″]TmFtZSA9IEFtYW4gUmFvCkRlc2NyaXB0aW9uOiBBbWFuIFJhbyBpcyBhIERQUyBzdHVkZW50IGluIGNsYXNzIDEwdGg=[/et_pb_dmb_code_snippet]

The same program can be written using calloc(); the only thing is you need to replace malloc with calloc as follows:

[et_pb_dmb_code_snippet code=”Y2FsbG9jKDIwMCwgc2l6ZW9mKGNoYXIpKTsK” _builder_version=”4.0.9″]Y2FsbG9jKDIwMCwgc2l6ZW9mKGNoYXIpKTsK[/et_pb_dmb_code_snippet]

[adinserter name=”Block 8″]

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, the operating system automatically releases 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

[et_pb_dmb_code_snippet code=”I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKaW50IG1haW4oKSB7CgogICBjaGFyIG5hbWVbMTAwXTsKICAgY2hhciAqZGVzY3JpcHRpb247CgogICBzdHJjcHkobmFtZSwgIkFtYW4gUmFvIik7CgogICAvKiBhbGxvY2F0ZSBtZW1vcnkgZHluYW1pY2FsbHkgKi8KICAgZGVzY3JpcHRpb24gPSBtYWxsb2MoIDMwICogc2l6ZW9mKGNoYXIpICk7CgkKICAgaWYoIGRlc2NyaXB0aW9uID09IE5VTEwgKSB7CiAgICAgIGZwcmludGYoc3RkZXJyLCAiRXJyb3IgLSB1bmFibGUgdG8gYWxsb2NhdGUgcmVxdWlyZWQgbWVtb3J5XG4iKTsKICAgfSBlbHNlIHsKICAgICAgc3RyY3B5KCBkZXNjcmlwdGlvbiwgIkFtYW4gcmFvIGEgRFBTIHN0dWRlbnQuIik7CiAgIH0KCQogICAvKiBzdXBwb3NlIHlvdSB3YW50IHRvIHN0b3JlIGJpZ2dlciBkZXNjcmlwdGlv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XBwb3NlIHlvdSB3YW50IHRvIHN0b3JlIGJpZ2dlciBkZXNjcmlwdGlvbiAqLwogICBkZXNjcmlwdGlvbiA9IHJlYWxsb2MoIGRlc2NyaXB0aW9uLCAxMDAgKiBzaXplb2YoY2hhcikgKTsKCQogICBpZiggZGVzY3JpcHRpb24gPT0gTlVMTCApIHsKICAgICAgZnByaW50ZihzdGRlcnIsICJFcnJvciAtIHVuYWJsZSB0byBhbGxvY2F0ZSByZXF1aXJlZCBtZW1vcnlcbiIpOwogICB9IGVsc2UgewogICAgICBzdHJjYXQoIGRlc2NyaXB0aW9uLCAiU2hlIGlzIGluIGNsYXNzIDEwdGgiKTsKICAgfQogICAKICAgcHJpbnRmKCJOYW1lID0gJXNcbiIsIG5hbWUgKTsKICAgcHJpbnRmKCJEZXNjcmlwdGlvbjogJXNcbiIsIGRlc2NyaXB0aW9uICk7CgogICAvKiByZWxlYXNlIG1lbW9yeSB1c2luZyBmcmVlKCkgZnVuY3Rpb24gKi8KICAgZnJlZShkZXNjcmlwdGlvbik7Cn0=[/et_pb_dmb_code_snippet]

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

[et_pb_dmb_code_snippet code=”TmFtZSA9IEFtYW4gUmFvCkRlc2NyaXB0aW9uOiBBbWFuIHJhbyBhIERQUyBzdHVkZW50LlNoZSBpcyBpbiBjbGFzcyAxMHRo” _builder_version=”4.0.9″]TmFtZSA9IEFtYW4gUmFvCkRlc2NyaXB0aW9uOiBBbWFuIHJhbyBhIERQUyBzdHVkZW50LlNoZSBpcyBpbiBjbGFzcyAxMHRo[/et_pb_dmb_code_snippet]

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

[adinserter name=”Block 7″]

Top Book Suggestion

The two books on C Programming Language that I Personally Recommend. I really loved reading these books. These books are listed for quality content, easy steps, and affordable price. You can get it from Amazon and Flipkart. 

 

Check The Price On Amazon:

1. C Programming Beginner’s Guide

2. Head First C: A Brain-Friendly Guide

Check The Price On Flipkart:

1. Introduction to C Programming 

2. Head First C: A Brain-Friendly Guide

« Previous Page                                                                                                                            Next Page »

[adinserter block=”6″]

Buy Premium Courses At Lowest Price. Become A Certified Developer Today!

Best Books Selected by Our Experts

Buy Premium Projects At Lowest Price

Ebooks Written by Our Experts

CoursePot.Com

Buy Premium Courses At Lowest Price. Grab Golden Opportunity To Become A Certified Developer Today!

Useful Links

[adinserter block=”6″]

Pin It on Pinterest

Share This