| | |
calloc and malloc function
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
1. calloc, malloc, realloc et al: http://irc.essex.ac.uk/www.iota-six....allocation.asp (or lots of another tutorials on this topic).
2.
No need to cast void* pointer explicitly in C because void* pointer is converted to any pointer value implicitly. You must convert void* pointer to desired pointer type explicitly in C++, however no needs in malloc call in C++ (it's the other story why).
2.
(type)expression called cast. It forces a conversion of expression value to the type; malloc() returns void* type value (pointer to void) - generic pointer value; char* denotes pointer to char type - so (char*)malloc(n) means dynamically allocate n bytes and get a pointer to this memory as if there are chars into this memory block.No need to cast void* pointer explicitly in C because void* pointer is converted to any pointer value implicitly. You must convert void* pointer to desired pointer type explicitly in C++, however no needs in malloc call in C++ (it's the other story why).
•
•
Join Date: Sep 2008
Posts: 1,625
Reputation:
Solved Threads: 205
From what I remember, essentially, they both dynamically (while the program is running) allocate memory for your program, and the main difference is that calloc sets all the memory (that it allocates to your program) to zero's while malloc does not. So after calling malloc, the memory that was set aside for your program would still have whatever 0'1 and 1's were in the bits before, but for calloc, all those bits would be set to 0's.
Last edited by BestJewSinceJC; May 27th, 2009 at 4:57 am.
From the C Standard:
None the less look at the link mentioned above.
•
•
•
•
The calloc function allocates space for an array of nmemb objects, each of whose size is size. The space is initialized to all bits zero. Note that this need not be the same as the representation of floating-point zero or a null pointer constant.C Syntax (Toggle Plain Text)
void *calloc(size_t nmemb, size_t size);
...
The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.C Syntax (Toggle Plain Text)
void *malloc(size_t size);
•
•
Join Date: Jun 2008
Posts: 9
Reputation:
Solved Threads: 0
what do you mean in the last line ? can u please explain ?
•
•
•
•
From what I remember, essentially, they both dynamically (while the program is running) allocate memory for your program, and the main difference is that calloc sets all the memory (that it allocates to your program) to zero's while malloc does not. So after calling malloc, the memory that was set aside for your program would still have whatever 0'1 and 1's were in the bits before, but for calloc, all those bits would be set to 0's.
Regards,
Rocky.
Rocky.
calloc is essentially this:
The difference is that the bytes are set to zero with calloc and left as-is with malloc.
c Syntax (Toggle Plain Text)
void *calloc ( size_t n, size_t size ) { void *base = malloc ( n * size ); if ( base != NULL ) memset ( base, 0, n * size ); return base; }
Last edited by Narue; May 27th, 2009 at 10:12 am.
I'm here to prove you wrong.
An obligatory link to http://c-faq.com/malloc/calloc.html
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- free() = delete or delete[]? (C++)
- trying to get text from file in function with malloc (C)
- Using constructor within a class array (C++)
- difference between NEW & MALLOC or CALLOC (C++)
- about struct in C (C)
- Difference between calloc and malloc (C)
- Another question regarding memory (C)
- how to increase the size of an array? (C)
Other Threads in the C Forum
- Previous Thread: clrscr is usually given after declaration of variables. why ?
- Next Thread: Send and read info from a Serial Port using C?
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks bash binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






