hai freinds,


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.

queris:

1. what is the difference between New and Malloc or Calloc in C

Recommended Answers

All 5 Replies

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.

hai freinds,


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.

queris:

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

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]
void* operator new(std::size_t size) throw(std::bad_alloc);
1 Effects: 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.

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

>You don't need to use sizeof in case of new
>You can initialise while allocating memory through new.

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.