-1

I use calloc, I've read that, calloc internally calls new handler, so what should I go for?

Should I use new operator(which allocate fast) or calloc(which can allocate and initialize memory as well)?

trincot
  • 317,000
  • 35
  • 244
  • 286
Santhosh
  • 73
  • 1
  • 5
  • 14
  • 3
    [malloc vs new](http://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-vs-new). Use memset if you want to 0 initialize the memory as calloc does. – user1052842 Feb 20 '14 at 11:41
  • calloc (and malloc and realloc) are not allowed to call operator new in C++ – PlasmaHH Feb 20 '14 at 11:42
  • "if you need fast allocation, use `new`; if you need initialized memory, use `calloc`". there is no absolute "optimum" without context. – umläute Feb 20 '14 at 11:45
  • 2
    `more optimal` doesn't make sense, either something is `optimal` - or not. – Frerich Raabe Feb 20 '14 at 11:50
  • `operator new` is C++ whereas `calloc` is not. Insofar, there's not much to reason which one is more optimal, since only one of them is _correct_. – Damon Feb 20 '14 at 12:09
  • @Damon Er, yes, calloc (and even `std::calloc`) is part of C++. Ideally, in cases where they are equivalent, you would just call new and the compiler would optimize that to a call to calloc when appropriate (calloc can be faster than malloc+memset because it sometimes knows the memory is already zero and skips the memset), but in practice that's not the case yet. – Marc Glisse Feb 20 '14 at 12:14
  • @user1052842: But only memset PODs. – Sebastian Mach Feb 20 '14 at 12:23
  • @MarcGlisse: It is **not** part of C++. It is _mentioned_ in C++ as part of the C standard library (together with some requirements which although they make sense from the PoV of C++ are, frankly, ridiculous, first because the C language doesn't have to care what C++ has to say about its standard library, and second because C++ requires that e.g. `malloc` does not call `operator new` to request memory -- which it certainly doesn't do since there is no such thing as `operator new` in C). – Damon Feb 20 '14 at 12:29
  • @Damon Just because `calloc` is also part of C doesn't mean it isn't part of C++ as well. It is a function that is required to be provided by the implementation, like `std::sort`. – Marc Glisse Feb 20 '14 at 12:44
  • hey @Damon,you please look in to this page http://msdn.microsoft.com/en-us/library/3f8w183e.aspx ,in Remarks item,microsoft people only given such kind of statement, and thanks fa responding. – Santhosh Feb 20 '14 at 12:47

4 Answers4

2

The question cannot really be answered, because it's based on the incorrect assumption that new merely allocates memory but doesn't initialize it. The contrary is the fact:

new not only allocates memory (unless you use "placement new"), it also calls some objects constructor. I.e. new does much more than calloc.

In C++, if you feel that you need to allocate some memory for e.g. some variable-sized array, consider using the standard containers first, e.g. prefer

std::vector<char> buf( 1024 );

over

char *buf = new char[1024];
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
1

calloc isn't really comparable to new; it's closer to operator new (they are not the same). calloc will zero out the memory allocated whereas operator new and malloc won't. new constructs an object in the storage location but calloc doesn't.

// Using calloc:
my_object* p = static_cast<my_object*>(std::calloc(1, sizeof(my_object)));
::new(static_cast<void*>(p)) my_object(10);

// Using operator new:
my_object* p = static_cast<my_object*>(::operator new(sizeof(my_object)));
::new(static_cast<void*>(p)) my_object(10);

// using new:
my_object* p = new my_object(10);
Simple
  • 13,992
  • 2
  • 47
  • 47
1

Well, everyone's told you about new not initialising memory etc, but they forgot about value-initialization syntax:

char *buffer = new char[size]();

So I would argue that you always use new. If you want to initialise the memory, use the above syntax. Otherwise, drop the parentheses.

paddy
  • 60,864
  • 6
  • 61
  • 103
0

Should I use new operator(which allocate fast) or calloc(which can allocate and initialize memory as well)?

In C++ you should never use *alloc memory function (malloc, calloc, free, etc). They lead you to create code that is unsafe for C++ (for C it is fine).

You should also use most specialized (higher level) code constructs whenever available.

That means:

  • prefer new/new[]/delete/delete[] over malloc/calloc/free
  • prefer smart pointers over raw pointers
  • prefer abstractions over raw pointer data (i.e. use std::vector/std::array/etc instead of new[] and delete[], use std::unique_ptr<T>/std::shared_ptr<T> instead of T*, etc.
utnapistim
  • 26,809
  • 3
  • 46
  • 82