473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Clarification in free

72
Hi All,

Assume we have allocated 100 bytes of memory using malloc,
We will get a pointer to the allocated memory, to free up this memory we will pass this pointer to free function. My question is how free will come to know that it needs to free 100 bytes of memory? where it gets this information?

Thanks,
Manjunath
Nov 23 '09 #1
2 1335
Banfa
9,065 Expert Mod 8TB
That is implementation defined, which means that every implementation is free to store that data, and any other data it requires to function anyway it chooses. 2 common methods are
  1. malloc actually allocates more than the 100 requested bytes. It allocates a block header plus the 100 bytes, the block header is fixed size and located in memory before the returned block, it contains the block size among other things. because the header is a fixed size the start of it can be easily calculated from the block pointer.
  2. The malloc routines keep a table or list of all allocated memory, when you malloc it adds an entry to the table with details such as block size. When you free it looks up the blocks entry in the table to get the block size.

These are 2 common methods, however there are probably others. You should make no assumption about how malloc may store the additional memory it requires to perform its function in allocating and freeing memory.
Nov 23 '09 #2
manjuks
72
Very useful.

Thanks
Manjunath
Nov 23 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Wes | last post by:
I am trying to secure different files, mostly pdf, so only the person suppose to see the file that was designed for that individual can see it. I am using sessions to secure the actual web pages,...
3
by: Karthik D | last post by:
Hello All, Basically I would like to take suggestions from programmers about implementation of a function which processes some error messages.(fyi:this is not a homework problem) Let me explain...
14
by: Kristo | last post by:
Since there's no strtoi function in standard C, I've been searching the clc archives for the proper way to store the result of strtol to an int. My search has yielded conflicting results. About...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
2
by: ss | last post by:
I have a doubt. I have 2 webform W1, W2 with a Button control B1, B2 respectively. In From W1 on click of button B1, i redirect response to WebForm W2 along with a querystring value. Now, on...
3
by: mbc | last post by:
According to the official documentation: ------------------------------------------------------------------------- Limitations of PostgreSQL Maximum size for a database unlimited (4 TB...
8
by: VK | last post by:
I'm a bit confused about ECMAScript docs. Can I legally change it format (say from .pdf to .html or .doc) or publish it on my web-site? ECMA provides a free of charge pdf version here:...
1
by: luxmanpai | last post by:
Hello All, I have an performance issue, where a query which is executed at the client place which is retrieving 370 records is taking around 10 minutes, Where as the same query which is executed...
2
by: ravir | last post by:
Hi, I am new to this group. I am working in Perl and shellscripts. I have a clarification regarding perl grep and pattern matching. I am writing a perl script to automate the process of code...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.