new is C++
malloc and calloc are C
calloc is the same as malloc, except for it clears memory to all bits zero.
new causes constructors to be called, malloc does not.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
> next calloc is contiguous allocation, it tryies to allocate contiguous memory, so somtimes it is faster
malloc is also contiguous as well, and calloc is usually slower, not faster (it does more work)
calloc is nothing more than a wrapper around malloc + memset
http://c-faq.com/malloc/calloc.html
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
new actually calls malloc internally
Maybe.18.4.1 Storage allocation and deallocation [lib.new.delete]
18.4.1.1 Single-object forms [lib.new.delete.single][INDENT] void* operator new(std::size_t size) throw(std::bad_alloc); [/INDENT]1Effects: The allocation function (3.7.3.1) called by a new-expression (5.3.4) to allocate size bytes of storage suitably aligned to represent any object of that size.
2 Replaceable: a C++ program may define a function with this function signature that displaces the default version defined by the C++ Standard library.
3 Required behavior: Return a nonnull pointer to suitably aligned storage (3.7.3), or else throw a bad_alloc exception. This requirement is binding on a replacement version of this function.
4Default behavior:Executes a loop: Within the loop, the function first attempts to allocate the requested storage. Whether the attempt involves a call to the Standard C library function malloc is unspecified.
Returns a pointer to the allocated storage if the attempt is successful. Otherwise, if the last argument to set_new_handler() was a null pointer, throw bad_alloc .
Otherwise, the function calls the currentnew_handler (18.4.2.2). If the called function returns, the loop repeats.
The loop terminates when an attempt to allocate the requested storage is successful or when a called new_handler function does not return.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314