calloc for C++
Hello,
I am wondering if there is way or rules regarding initialization of tables using the new operator with base type.
Ex :
in C I am used to do this :
int * myInt = (int *) calloc ( 100 sizeof (int) ); /* initialize to zero */
in C++ considering everything is object :
int * myInt = new int[100]; // does a default value for int is guaranty ?
// do I have to process the following line :
memset( myInt, 0, 100 );
Thank you.
--Sebastien.F
[502 byte] By [
Seb256] at [2007-11-11 10:29:26]

# 1 Re: calloc for C++
It is not going to have a set value, you want to use memset on simple data arrays and types but will have to use a loop over more complex classes and structs -- memset is unsafe and should be avoided on objects!
Consider not initializing the memory unless you absolutely have to for some algorithm (sum of products etc), many programmers will set all the locations to zero out of habit rather than any real need.
jonnin at 2007-11-11 20:58:46 >

# 2 Re: calloc for C++
In C++ you can't provide explicit initializers for arrays allocated by new[]. It stinks, but that's how it is. However, most implementations use magic values for uninitialized data and some implementations zero the allocated memory. It's not a very pleasing situation but there's a workaround: define class Int which does nothing more than initialize its int member to zero:
struct Int
{
int x;
Int(): x(int())) {}
};
Instead of allocating bare ints, allocate Int's. It makes me feel like gong back in time 15 years ago when I took my first C++ course and had to submit exercises of this type but sometimes these kludges are still useful.
BTW, are you sure you need to allocate those ints dynamically? If the size of the array is known at compile time, you can use stack memory instead with the obvious advantage of being able to use an initializer:
int arr[100]={0};
Danny at 2007-11-11 20:59:46 >

# 3 Re: calloc for C++
I think you should consider this kind of allocation, with parenthesis:
int * myInt = new int[100]();
It seems it initializes each element with zero.
I suppose this is specified in language standards.
Viorel at 2007-11-11 21:00:47 >

# 4 Re: calloc for C++
no, it just invokes the int ctor which will set everything to zero in debug mode on some platforms or all the time on others as danny said, its not related.
jonnin at 2007-11-11 21:01:41 >

# 5 Re: calloc for C++
Actually, Viorel is right. The () notation will invoke int's pseudo-constructor, which has the effect of default initializing an int. Default initialization in this case means zero initialization. However, some compilers don't support this syntax because it was added to C++ not long ago (relatively speaking of course). Borland C++ Builder 4.0 for instance chokes on this:
[C++ Error] Project1.cpp(3): E2243 Array allocated using 'new' may not have an initializer.
So you have to check whether your compiler likes this form.
Danny at 2007-11-11 21:02:45 >

# 6 Re: calloc for C++
Interesting,
Does a
int ** myInt2 = new int*[100]();
would initialize that table of pointers with the hundred pointers set to NULL ?
If it's only a compiler purpose I ll check the GCC / Sun forte for this.
Thank you.
Seb256 at 2007-11-11 21:03:51 >

# 7 Re: calloc for C++
ok, what is a pseudo constructor? This is new to me!
I would have said
int x;
and
int x();
both invoke the default ctor & do the exact same things. Is this because int is not really an object and the ctor is a special c++ language addition?
jonnin at 2007-11-11 21:04:44 >

# 8 Re: calloc for C++
pseudo ctor is my own term. The standard defines the concept 'pseudo dtor' for fundamental types, e.g., ~int() is the pseudo ctor of type int. Pseudo dtors are necessary in generic code, where the template code can't tell in advance what the actual type of its parameter will be.
In a similar vein, the initialization form of int() was added to C++ about a decade ago to ensure that templates can provide a generic initialization syntax in the form of
T t= T(); //whatever T stands for, t will always be initialized
So pseudo-ctor is a fancy (and perhaps misleading) name for default initialization of POD types and built-in types. Don't worry about performance -- int doesn't really have a constructor so the initialization is just syntactic sugar for =0;.
Danny at 2007-11-11 21:05:52 >

# 9 Re: calloc for C++
Interesting,
Does a
int ** myInt2 = new int*[100]();
would initialize that table of pointers with the hundred pointers set to NULL ?
If it's only a compiler purpose I ll check the GCC / Sun forte for this.
Thank you.
Yes, it initializes the 100 int* pointers to NULL. However, you should check whether your compiler accepts this syntax, as I explained above.
Danny at 2007-11-11 21:06:50 >

# 10 Re: calloc for C++
Oddly, .net (2002) supported the syntax but did not fully zero fill an integer pointer or array. Instead, the first two entries had garbage values. Likely it is fixed in later versions. Very nice trick for compilers that support it.
jonnin at 2007-11-11 21:07:57 >

# 11 Re: calloc for C++
.Net probably used the first two DWORDS as sentry bytes, enabling the implementation to detect uninitialized pointers or array lower bounds. Obviously, these two DWORDS should have been placed at offset -2 rather than 0. VS 2005 gets it right though.
Danny at 2007-11-11 21:08:50 >
