Does malloc add size information to program?

H

Hongzheng Wang

Hi, everyone
I have a problem about malloc/free function.
Does malloc add size information to program? And, when free function is
called, how this function get the size information?
That is, if I request a memory block of size 10, where the size
information 10 is stored?
To be clear, I have such codes below:

int *p = (int *) malloc(10*sizeof(int));
/* ...... */
p[5] = '\0';
/* ...... */
free p;

Will these codes cause memory leak?

I'm not clear about this problem.
Thank you for your help. ^_^
 
R

Richard Heathfield

Hongzheng said:
Hi, everyone
I have a problem about malloc/free function.
Does malloc add size information to program?

No, malloc provides no information to your program about the size of the
memory block you wish to allocate. In fact, the situation is precisely
reversed. You tell malloc.
And, when free function is
called, how this function get the size information?

There is no standard way to retrieve this information, which need not even
be stored at all.
That is, if I request a memory block of size 10, where the size
information 10 is stored?

In your request. In other words, when you request those 10 bytes, you know
you are requesting 10 bytes. If you need this information later, then Don't
Forget.
To be clear, I have such codes below:

int *p = (int *) malloc(10*sizeof(int));

Better: int *p = malloc(10 * sizeof *p);
/* ...... */
p[5] = '\0';

Better:

if(p != NULL)
{
p[5] = '\0';
}
/* ...... */
free p;

Better:

free(p);
Will these codes cause memory leak?

No.
 
S

Sheldon Simms

Does malloc add size information to program?

Yes, but it cannot be accessed from your C program.
And, when free function is called, how this function get the size
information? That is, if I request a memory block of size 10, where the
size information 10 is stored?

It depends on the implementation of malloc() and free()
To be clear, I have such codes below:

int *p = malloc(10 * sizeof *p); /* ...... */
p[5] = 0;
/* ...... */
free(p);

Will these codes cause memory leak?

No.
 
M

Malcolm

Hongzheng Wang said:
Does malloc add size information to program? And, when free function > is
called, how this function get the size information?You can implement malloc() yourself. Let's call the functions mymalloc() and
my free().
In our file mymalloc.c we have a huge static array from which we allocate
the memory.
The trick is that we store information - usually the size of the block
allocated, in the space _before_ the pointer we return.
myfree() then looks at this information, and uses it to consolidate the
blocks when they are freed, making them available for further allocation.

The malloc() on your system may have a rather more sophisticated scheme than
this, however, there are lots of tricks you can use to speed up the
allocation of small blocks.
To be clear, I have such codes below:

int *p = (int *) malloc(10*sizeof(int));
/* ...... */
p[5] = '\0';
/* ...... */
free p;

Will these codes cause memory leak?
No, you allocate p, then free it. p[5] is a legal address (as long as
malloc() doesn't return NULL because it is out of memory). You lose the
contents of p as soon as you free it, of course, but the computer won't leak
memory.
 
D

Derk Gwen

# Hi, everyone
# I have a problem about malloc/free function.
# Does malloc add size information to program? And, when free function is
# called, how this function get the size information?

Whether malloc records the size somewhere is implementation dependent.
For example, malloc might always allocate a new block at the end
of a memory segment it extends, and free might be ignored. With enough
memory, this would work well and nowhere is block size is stored
or retrievable from malloc.

If you give free a pointer previously returned from malloc, you may no
longer safely access any part of that block. What happens to the block
is implementation dependent, but does not depend on the block contents.
Malloc and free somehow cooperate behind the interface to determine
what is allocated and freed.

(Unlike Pascal dispose, you do not have to give the size of a heap array.)
 
T

Thomas Matthews

Richard said:
Hongzheng Wang wrote: [snip]
That is, if I request a memory block of size 10, where the size
information 10 is stored?


In your request. In other words, when you request those 10 bytes, you know
you are requesting 10 bytes. If you need this information later, then Don't
Forget.

As a reminder to the OP, malloc is allowed to allocate more memory than
you requested. For example, if you requested 10 bytes, but memory is
apportioned into 16-byte chunks, malloc can allocate 16 bytes (one
chunk).

The only trusted information you can get from malloc is whether it
succeeded or not. You cannot be certain that it allocate exactly the
amount requested. You cannot guarantee that the same program executed
the same way will return the same pointer (address) by malloc. If
malloc succeeds, you can safely say that you have an area of at least
the amount of space you requested.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,104
Messages
2,570,643
Members
47,247
Latest member
youngcoin

Latest Threads

Top