How to know the length of memory space to be released for free

Source: Internet
Author: User

When the memory allocation problem occurs, both malloc and calloc must specify the size of the memory to be allocated, but free is not required, I wondered how free knows how many lengths are allocated from the pointer address?

At that time, I thought, in malloc or calloc, where should the compiler place the value of the size? When it is free, I will find the value and release the heap space of the value.

But where exactly?

A few days ago, I went online, saying that the modern compiler is to place the size value before the start of the allocation address. But how many locations are there? We can see it in the memory monitor of vs today.

The test code is as follows:

[Cpp]
# Include "stdlib. h"
# Include "stdio. h"
 
# Deprecision Num 100
 
Int main (void)
{
Int I;
Int * p = (int *) malloc (Num );
For (I = 0; I <Num; I ++)
{
* (P + I) = 1; // The value is assigned to 1 for convenience.
}
Free (p );
 
Return 0;
}
The p address is 0x00393220.
Next, open the memory monitor window for vs2008:


There is a suspicious IP address 0x00393210, because 0x64 = 100

Let's take a look:

[Cpp]
# Define Num 50
Look at the memory monitor again:


See * (0x00393210) = 0x32; that is, 0x32 = 50

The size should be put here, that is, the location before the allocated address 0x10.

It can be seen that there is a small aspect of how to allocate heap node memory size (of course, its main aspect is the generation of "fragments ~~~

So what about calloc? The answer is to replace the original size with num * size like malloc;

[Cpp]
# Include "stdlib. h"
# Include "stdio. h"
 
# Define Num 50
 
Int main (void)
{
Int I;
Int * p = (int *) calloc (Num, Num );
For (I = 0; I <Num; I ++)
{
* (P + I) = 1;
}
Free (p );
 
Return 0;
}
The p address here is 0x00395008.

0x9c4 = 2500 (here is the knowledge of large and small terminals. You can see that x86 is a small terminal: MSB high address)

This is sufficient for the value that stores the size of the memory space, because the function prototype of calloc and malloc:

[Cpp]
Void * malloc (size_t size );
Void * calloc (size_t num, size_t size );
Ps: size_t is internally defined as unsigned int by Macro.

This doesn't mean that calloc can allocate the entire memory. Of course it doesn't work. If the heap memory space you applied for is too large, the following error will occur (vs2008 ):

 

The sentence in the middle is very concise and clear. As for how much I lose, you can try it. In fact, in terms of ARM and DSP (for MCU ), the heap and stack size and start address can be manually allocated. Of course, you cannot exceed ~~~

But there is another question: is the storage size location related to the compiler? Is it CPU-related?

To Be Continued ~~~

Ps: Just a piece of Cortex-M3 in the hands of the board, you can use MDK to compile the Debug on the clear ~~~

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.