this is pench.i have finished my psot gradution(MCA) in S.K.University.i am new to this group.plz help me if post any queris in forum.
1. what is the difference between New and Malloc or Calloc in C
new actually calls malloc internally, wat it does it first allocate required memory thn calls constructor on that chunk (as told in last reply).
most prominenet difference are u need no typecasting after allocationg memory from new and, when new fails it throwa an exception bad_alloc. which malloc and calloc doesnot.
next calloc is contiguous allocation, it tryies to allocate contiguous memory, so somtimes it is faster but it is more likely to fail if u try to allocate big memeoy. also calloc intialize all bits to 0 as told in privious post.
> 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
18.4.1 Storage allocation and deallocation [lib.new.delete]
188.8.131.52 Single-object forms [lib.new.delete.single] void* operator new(std::size_t size) throw(std::bad_alloc);
1 Effects: The allocation function (184.108.40.206) 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.
4 Default 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 current new_handler (220.127.116.11). 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.