| | |
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,658
Reputation:
Solved Threads: 206
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.
New members chased away this month: 5
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?
Views: 937 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






