LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2008, 09:10 AM   #1
fssengg
Member
 
Registered: Feb 2005
Location: Kerala(God's own country)
Distribution: Redhat-8
Posts: 35

Rep: Reputation: 15
Lightbulb why free() need not take size as argument?


Hi All

I have a question
Why allocating memory using malloc() we have to pass the size.
But while freeing the same memory we don't need to pass the size.How the os know how much memory to be freed?

What will happen in a situation like this?

Code:
p = malloc(100)
p ++;
free (p);
In this case how much memory will be freed?(full 100 bytes or 99 bytes?)


Thanks in advance for you help
 
Old 05-25-2008, 10:14 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by fssengg View Post
Why allocating memory using malloc() we have to pass the size.
But while freeing the same memory we don't need to pass the size.How the os know how much memory to be freed?
The OS (kernel) keeps track of all the malloc()-ed chunks of memory and their sizes. Often in fixed-length chunks (called "memory pages", 4096 bytes is a common page size).

Quote:
Originally Posted by fssengg View Post
What will happen in a situation like this?
Code:
p = malloc(100)
p ++;
free (p);
In this case how much memory will be freed?(full 100 bytes or 99 bytes?)
Interesting question. I found the answer here.

Quote:
[citing http://www.cis.upenn.edu/~milom/cse2...s/MemMgmt.pdf]

What if wrong address is freed?
p = malloc(x);
p++;
free(p);
  • Size info will be wrong!
  • Program will likely (later) fail in mysterious ways

Last edited by Hko; 05-25-2008 at 10:16 AM.
 
Old 05-25-2008, 11:56 AM   #3
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Two interesting questions.

First of all, malloc and free are handled by the C library, not the kernel (or OS, taken to be synonymous with kernel in this context).

What happens is that when you call malloc(n), the malloc code finds some free space; if it's short on free space, it requests some space from the OS. Malloc then creates a node for a linked list, the size of which is n plus room for overhead. The overhead stores the two pointers for the linked list, plus the size.

When you call free, you pass a pointer that points 12 bytes into a big node. Free can subtract 12 from your pointer and get the address of the node, from which it can read the size.

So that's why you don't need to tell the size to free: it already knows.

Parent poster talked about pages. They *do* enter the picture: when malloc talks to the kernel, if the kernel hands out memory to your program, it will hand it out in chunks of a fixed size. These chunks are called pages, as parent said, and typically have a size of 4K bytes. How your program manages that memory is up to your program; malloc does it on your behalf, in the case of C, but there's nothing stopping you from talking to the kernel directly.

What happens when you call free(1 + malloc(n))? Most likely, your program will crash; either free will try to read a bad pointer, or it will check that the pointer is bad, spew out an error message and die.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
free -m command displays less RAM size babu198649 Linux - Newbie 6 05-08-2008 12:51 PM
message sending failed : Error[22 ] invalid argument .....but each and every argument rakeshranjanjha Linux - Software 2 01-07-2008 11:22 PM
Why i am having free(): invalid next size (normal) error. here is code shrik Linux - Software 0 07-23-2007 04:26 AM
Wrong Size of free disk space ALInux Linux - General 1 04-03-2006 04:22 AM
Total partition size - User partition size is not equals to Free partition size navaneethanj Linux - General 5 06-14-2004 12:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration