What is diff b/w malloc,calloc,realloc

  • Thread starter Ramasubramanian XR (AS/EAB)
  • Start date
D

Duck Dodgers

malloc returns uninitialized memory, calloc initializes the memory to
all bits zero, and realloc is way more flexible than it should be.
realloc can act like malloc or free, or it can resize memory returned
by malloc, calloc, or a previous realloc call, possibly allocating a
new block and invalidating any pointers into the memory.
 
N

not

What is diff b/w malloc,calloc,realloc
could any one explain

Pointer = malloc(Size)
Allocates a new block of memory.

Pointer = realloc(OldPointer, Size)
Resizes an existing block of memory

Pointer = calloc(NumBlocks, BlockSize) ...
Clears and allocates the given number of blocks of a given size. (most
useful for arrays)
 
R

Richard Bos

Pointer = calloc(NumBlocks, BlockSize) ...
Clears and allocates the given number of blocks of a given size. (most
useful for arrays)

Be careful, though: "clears" means "fills with 0-valued bytes", not
necessarily "fills with zero values". In particular, 0x00000000 need not
be the representation of a zero-valued floating point number or of a
null pointer.

Richard
 
D

Default User

Ramasubramanian said:
What is diff b/w malloc,calloc,realloc
could any one explain

What book are you using that doesn't cover such fundamental memory
management functions?




Brian
 
E

Emmanuel Delahaye

Ramasubramanian XR (AS/EAB) wrote on 23/03/05 :
What is diff b/w malloc,calloc,realloc
could any one explain

It's all explained in any decent C book or help file.

google : "man malloc" "man realloc" "man calloc"

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
 

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
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top