Why malloc() gives more than asked memory | Dynamic Memory Allocation

Why malloc() gives more than asked memory | Dynamic Memory Allocation

Dynamic Memory Allocation is one of the memory management processes in which we can change/modify the size of a data structure in runtime. It is not possible with Static Memory Allocation. Static memory allocation uses Stack memory to allocate the memory, while Dynamic Memory Allocation uses Heap memory to allocate the memory.

For dynamic memory allocation in C++ four functions are used, these are given below :

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

But in this article, we will only discuss the malloc() function. malloc() function is the function from stdlib library. malloc() function returns a pointer with void type and we need to type-cast it according to our requirement.

One example of malloc() is given below :

No alt text provided for this image

As you can see, we have allocated only 5*4=20 bytes but it can store more than 5 integers.

This is because the malloc function allocates a memory block of at least size bytes. The block may be larger than size bytes because of the space that's required for alignment and maintenance information.

We should also need to free (deallocate) the used space after our requirement is fulfilled so that the memory leakage issue doesn't appear.

No alt text provided for this image


Vimal Daga

World Record Holder | 2x TEDx Speaker | Philanthropist | Sr. Principal Consultant | Entrepreneur | Founder LW Informatics | Founder Hash13 pvt ltd | Founder IIEC

3y

Grt

Like
Reply
Jaya Shukla

Public Speaking Expert at PlanetSpark || FINALIST UNESCO HACKATHON || SIH WINNER 2022 || Aspiring Software Engineer ||Youtuber|| Instagrammer|| ARTH2.0 Learner|| Expertized communication skills

3y

This is amazing

Like
Reply

To view or add a comment, sign in

More articles by Adil Ansari

  • AWS Lambda Use Cases (Serverless Computing)

    Hello Everyone, long time no see..

    6 Comments
  • Creating Our Own Art Using CV2 and Numpy

    Hello All, It has been months passed since I've not written any article. But now, I am back with a fun topic of…

    4 Comments
  • Fixed Capacity Queue

    Hello all, I am back with a new article on Fixed Capacity Queue and its use cases. So let's started.

    3 Comments
  • String Template and Do While Loop in Python3

    String Template Template is a class in module 'string' that is used to make a string template or we can say a blueprint…

    3 Comments
  • Capture and Crop Video using OpenCV

    OpenCV is a powerful library available for multiple languages. In this tutorial I have used python as a programming…

    12 Comments
  • Creating Docker Image for ML Project

    In this article, we will see how we can create a custom docker image for an ML algorithm and as soon as the code gets…

    10 Comments
  • Crop and Swap images using OpenCV

    OpenCV is a computer vision library. It enables many operations to be performed on images and videos, and today we are…

    10 Comments
  • Run GUI App in Docker

    Hello Everyone, In this article, I am going to show you how you can run GUI application inside a docker container. So…

    9 Comments
  • Exploring Date Commands in Linux

    In this article, we will see various options available in the "date" command. So let's begin.

Insights from the community

Others also viewed

Explore topics