954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

difference between NEW & MALLOC or CALLOC

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

pench
Newbie Poster
4 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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.

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

> 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
Team Colleague
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
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You