can anyone please tell me the function of calloc() and malloc() and the difference between them ...

what does (char*)malloc(n) mean ??

Recommended Answers

All 7 Replies

1. calloc, malloc, realloc et al: http://irc.essex.ac.uk/www.iota-six.co.uk/c/f7_dynamic_memory_allocation.asp (or lots of another tutorials on this topic).

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).

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.

From the C Standard:

void *calloc(size_t nmemb, size_t size);

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.
...

void *malloc(size_t size);

The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.

None the less look at the link mentioned above.

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.

calloc is essentially this:

void *calloc ( size_t n, size_t size )
{
  void *base = malloc ( n * size );

  if ( base != NULL )
    memset ( base, 0, n * size );

  return base;
}

The difference is that the bytes are set to zero with calloc and left as-is with malloc.

give some example
and explain each word.
about malloc and calloc?

commented: Resurrecting a 1.5 year old thread rather than followeing the links provided. Shame! -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.