Thread: calloc in C++

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    calloc in C++

    Is there a way(with new) to allocate a number of bytes which will be initialised to zero like calloc in C?
    Last edited by k_w_s_t_a_s; 05-01-2003 at 04:13 PM.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Why would you want to do that?

    The point of new is that it automatically calls the appropriate constructor, and leaves it up to the constructor to initialize data members of classes according to the class definitions.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There you go. Overload the new operator. I haven't tried it myself, but then, I'm not the one who wants to do it.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    as far as im aware the standard decrees that new default constructs primitives so for byte read char and it will already be zeroed. i.e.

    myzeroedmem = new char[ SIZE_IN_BYTES_WANTED ];
    // do stuff
    delete [] myzeroedmem;

    will do what you want.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Originally posted by Stoned_Coder
    as far as im aware the standard decrees that new default constructs primitives so for byte read char and it will already be zeroed. i.e.

    myzeroedmem = new char[ SIZE_IN_BYTES_WANTED ];
    // do stuff
    delete [] myzeroedmem;

    will do what you want.
    Nope, though Debug mode in VC++ does I think. The correct way to do this in C++ is to just use calloc. Wrapping it in a buffer class is not a bad idea though. Andrei Alexandrescu's typed buffer goes into nausiating detail on the subject.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    yes sorry you were right. Section 5.3.4.15 says that primitives are constructed with an indeterminate value. Well That updates my knowledge of the standard slightly. You could always do a ZeroMemory() or memset() on the memory returned by new char[size] and that would zero it all.
    Last edited by Stoned_Coder; 05-02-2003 at 07:38 AM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    I'm really curious about this discussion. Are you talking about allocating memory but not assigning any data type to it? In my vast experience (not!) I've never heard of that. Why would you do that? What's the application?

    (Or if that's NOT it, then what are you talking about ?)

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    calloc is clear allocate. That is it allocates memory and then blindly fills it with zero's. Naturally this is a no-no for anything with a non-trivial ctor. For things like a bunch of char's, however, it can be just the thing. The runtime and OS have a lot of options that aren't available to new.

    It should be noted, that while calloc is cool and for large zeroed buffers, optimal, malloc and realloc have even more tricks for building POD(plain old data) buffers. If you bought your computer some time after 1986 malloc will probably just grab a single page from the heap, and tell the OS how many pages it may ask for later. The pages are not put into frames untill a write past the first one. This is why vector uses placement new so extensively, and how it can out-perform new[] where default ctor's have some weight to them.

    calloc is also sometimes used to try to even out performance. After the callloc as much of the memory as possible is actually in frames. In other words random access to the calloc'ed space is near-uniform, where a page-fault can easily take 100X or more. Nothing is free, however as the calloc is much more likely to swap out other pages, you will have to pay the page miss someday.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 03:29 AM
  2. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. Why use calloc()?
    By dwks in forum C Programming
    Replies: 8
    Last Post: 07-20-2005, 08:22 AM
  5. difference between calloc and malloc
    By saravanan_ts in forum C Programming
    Replies: 4
    Last Post: 07-28-2003, 06:13 AM