simple pointer question

Q

questions?

Let's say;

char * first, *second;
first=malloc(10*sizeof(char));
second=malloc(100*sizeof(char));

what would be the difference between first and second other than the
difference in the address.
I mean, when I use free() function to free the memory, how does the
system know how large the block to free?
Where is the information about size stored?
 
M

Malcolm McLean

questions? said:
Let's say;

char * first, *second;
first=malloc(10*sizeof(char));
second=malloc(100*sizeof(char));

what would be the difference between first and second other than the
difference in the address.
I mean, when I use free() function to free the memory, how does the
system know how large the block to free?
Where is the information about size stored?
The trick is to store information in the space immediately before the
pointer. free() can then subtract a few bytes, and get all the information
it needs.

However modern systems use a variety of techniques. See my "memory games"
chapter of Basic Algorithms, which is free, to understand a few simple
allocation strategies.
 
B

Ben Bacarisse

questions? said:
char * first, *second;
first=malloc(10*sizeof(char));
second=malloc(100*sizeof(char));

what would be the difference between first and second other than the
difference in the address.
I mean, when I use free() function to free the memory, how does the
system know how large the block to free?
Where is the information about size stored?

You have asked a FAQ: http://c-faq.com/malloc/freesize.html
 
I

Ivar Rosquist

The trick is to store information in the space immediately before the
pointer. free() can then subtract a few bytes, and get all the
information it needs.

However modern systems use a variety of techniques. See my "memory
games" chapter of Basic Algorithms,

To the OP: Please, don't. McLean's text has been shown in this
group to be so riddled with conceptual errors that chances are that
whatever you read in it will contain at least one.
which is free,

It has to be: Who would pay to publish such a collection of
errors?
 
B

Ben Pfaff

questions? said:
I mean, when I use free() function to free the memory, how does the
system know how large the block to free?

This is in the FAQ.

7.26: How does free() know how many bytes to free?

A: The malloc/free implementation remembers the size of each block
as it is allocated, so it is not necessary to remind it of the
size when freeing.

7.27: So can I query the malloc package to find out how big an
allocated block is?

A: Unfortunately, there is no standard or portable way.
 
S

santosh

Thank you guys. Good to know the trick, Awesome!

What trick?

That FAQ in essence says that you (the programmer) should not depend on
the details of the implementation, unless necessary.
 
M

Malcolm McLean

santosh said:
What trick?

That FAQ in essence says that you (the programmer) should not depend on
the details of the implementation, unless necessary.
The question was "how is this magic achieved?". The FAQ doesn't answer that.
It tells you how to use malloc(), and that there is no portable way of
retrieving the size. But that's a slightly different question.
 

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
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top