• Advertisement

Archived

This topic is now archived and is closed to further replies.

Fun with calloc and malloc

This topic is 5145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is calloc(elementNumber, size) the same thing as calling malloc(elementNumber * size)? I know this works for dynamic arrays in C, but is there a standard C++ equivalent for handling them?

Share this post


Link to post
Share on other sites
Advertisement
The new and delete operators. You could still use malloc or calloc, but it''d be considered bad practice.

Share this post


Link to post
Share on other sites
Oh! That's right... I've used them before. I just completely forgot about them. Heh.

Oh, and one more thing: Does free() work with calloc()? Or do I have to use realloc() with a size of 0 to free memory allocated with calloc?

[edited by - Sirveaux on January 24, 2004 5:49:24 PM]

Share this post


Link to post
Share on other sites
free works with calloc.

You can find out more by using google like so: free site:msdn.microsoft.com.

Which leads to free:

quote:

Remarks

The free function deallocates a memory block (memblock) that was previously allocated by a call to calloc, malloc, or realloc. The number of freed bytes is equivalent to the number of bytes requested when the block was allocated (or reallocated, in the case of realloc). If memblock is NULL, the pointer is ignored and free immediately returns. Attempting to free an invalid pointer (a pointer to a memory block that was not allocated by calloc, malloc, or realloc) may affect subsequent allocation requests and cause errors.

Share this post


Link to post
Share on other sites

  • Advertisement