malloc and free

  • Thread starter Joris Adriaenssens
  • Start date
S

Stephen Sprunk

Joris Adriaenssens said:
Is it the operating-system that is responsible to remember the size of
that piece of memory ?
In that case, when I build a large linked-list by allocating memory
for each node, I get a lot of overhead in the operating system. Is
this right ? or is this implementation-specific or operating-system
specific ? (FAQ q7.26 says that the implementation 'remembers' the
size. Does this mean that every implementation may do it in his own
way ? )

That's implementation-defined, though there are only a few obvious and
efficient ways to meet the requirements.
If the computer has to remember for each single allocation the size of
the allocated block, is it better than, to allocate a bigger piece of
memory and keep track of the use of it within the program, when I try
to create a linked-list or a binary tree or something like that ?

That will depend on whether your code is more or less efficient than the
malloc/free provided by your implementation. As a rule of thumb, use what
the standard library provides unless you determine (through profiling) that
it does not meet your needs. malloc is one of the most highly-tuned
functions in existence, so odds are you'll only be able to improve on it in
corner cases if at all. More importantly, your replacement is substantially
more likely to have bugs, and the risk often outweighs the potential
benefits unless you're very experienced at writing memory management code.

S
 
K

Keith Thompson

RCollins said:
Can someone give me the section of the standard that specifically says
an (implicit) type conversion should not be called a "cast" ?

Sure, it's in the same section that says an (implicit) type conversion
should not be called a "luxury yacht".

The standard says what a "cast" is. It doesn't enumerate the infinite
number of things that a "cast" isn't.

C99 6.5.4p4 says:

Preceding an expression by a parenthesized type name converts the
value of the expression to the named type. This construction is
called a _cast_. A cast that specifies no conversion has no
effect on the type or value of an expression.

A cast is a construction in C source. It is not anything else.

(I think the last sentence refers to things like "(int)42", where the
specified type is the same as the top of the operand.)
 
M

Mabden

Keith Thompson said:
Sure, it's in the same section that says an (implicit) type conversion
should not be called a "luxury yacht".

When speaking to Mr. Humphreys you must use the term "Dog Kennel".
Otherwise his puts a bucket over his head.
 
P

Pete Glasscock

Keith Thompson said:
Sure, it's in the same section that says an (implicit) type conversion
should not be called a "luxury yacht".

.... unless you pronouce it "throatwobbler mangrove".
 

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

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top