tags:

views:

153

answers:

5

Why is it that for user defined types when creating an array of objects every element of this array is initialized with the default constructor, but when I create an array of a built-in type that isn't the case?

And second question: Is it possible to specify default value to be used while initializing elements in the array? Something like this (not valid):

char* p = new char[size]('\0');

And another question in this topic while I'm with arrays. I suppose that when creating an array of user defined type, every element of this array will be initialized with default value. Why is this?

If arrays for built in types do not initialize their elements with their defaults, why do they do it for User Defined Types?

Is there a way to avoid/circumvent this default construction somehow? It seems like bit of a waste if I for example have created an array with size 10000, which forces 10000 default constructor calls, initializing data which I will (later on) overwrite anyway.

I think that behaviour should be consistent, so either every type of array should be initialized or none. And I think that the behaviour for built-in arrays is more appropriate.

A: 

Currently (unless you're using the new C++0x), C++ will call the constructor that takes no arguments e.g. myClass::myClass(). If you want to initialise it to something, implement a constructor like this that initialises your variables. e.g.


class myChar {
    public:
        myChar();

        char myCharVal;
};

myChar::myChar(): myCharVal('\0') {
}
Warpspace
This incurs a runtime penalty. Most classes/structs will always have different alignment rules than naked chars, even if it's not strictly necessary. For example, on x86 that class will have a width of 4 bytes rather than one.
Billy ONeal
Sorry I missed your edit. For your second question, if you don't initialise your variables in your custom class, it depends on the compiler as to what happens. Some compilers set variables to 0 if they're not specifially set by you (unless your tell them not to using compiler options such as optimisation), and in others it's the default not to do anything. Therefore, you can usually get away with it doing nothing if you set an array of 10,000 elements like that.
Warpspace
I believe (although I've never tried it myself) that you can use the #pragma pack setting to specify any padding the compiler should put in. I do know there's always a way to tightly pack your arrays if your classes are "Plain Old Data" (don't do anything fancy like virtual functions).
Warpspace
I'm using VS2010RC and it tells me that this code is invalid? What's going on?
There is nothing we can do
I'm using GCC, and there's no errors. What's the error message?
Warpspace
@Warpspace: I'm using VS2010RC as well and I get no errors. I suspect a PEBKAC is on the loose :)
Billy ONeal
What on earth is PEBKAC stands for? @Billy I'm getting msg: A new initializer cannot be specified for an array.
There is nothing we can do
@atch: **P** roblem **E** xists **B** etween **K** eyboard **A** nd **C** hair. I see no array in the above example code so I fail to see how you can be getting that message from Warpspace's code.
Billy ONeal
@Billy Thanks for explanaition. Nice one ;)
There is nothing we can do
The array would be myChar* p = new myChar[size]; I thought that was obvious.
Warpspace
A: 

Something like this (not valid):

As far as I know that is perfectly valid. Well not completely, but you can get a zero intialized character array:

#include <iostream>
#include <cstdlib>

int main(int argc, char* argv[])
{
    //The extra parenthesis on the end call the "default constructor"
    //of char, which initailizes it with zero.
    char * myCharacters = new char[100]();
    for(size_t idx = 0; idx != 100; idx++) {
        if (!myCharacters[idx])
            continue;
        std::cout << "Error at " << idx << std::endl;
        std::system("pause");
    }
    delete [] myCharacters;
    return 0;
}

This program produces no output.

And another question in this topic while I'm with arrays. I suppose that when creating an array of user defined type and knowing the fact that every elem. of this array will be initialized with default value firstly why?

Because there's no good syntactic way to specialize each element allocated with new. You can avoid this problem by using a vector instead, and calling reserve() in advance. The vector will allocate the memory but the constructors will not be called until you push_back into the vector. You should be using vectors instead of user managed arrays anyway because new'd memory handling is almost always not exception safe.

I think that behaviour should be consistent, so either every type of array should be initialized or none. And I think that the behaviour for built-in arrays is more appropriate.

Well if you can think of a good syntax for this you can write up a proposal for the standard -- not sure how far you'll get with that.

Why is it that for user defined types when creating an array of objects every element of this array is initialized with the default constructor, but when I create an array of a built-in type that isn't the case? and
And another question in this topic while I'm with arrays. I suppose that when creating an array of user defined type, every element of this array will be initialized with default value. Why is this? and
If arrays for built in types do not initialize their elements with their defaults, why do they do it for User Defined Types?
Because a user defined type is never ever valid until its constructor is called. Built in types are always valid even if a constructor has not been called.

And second question: Is it possible to specify default value to be used while initializing elements in the array? Something like this (not valid):
Answered this above.

Is there a way to avoid/circumvent this default construction somehow? It seems like bit of a waste if I for example have created an array with size 10000, which forces 10000 default constructor calls, initializing data which I will (later on) overwrite anyway.
Yes, you can use a vector as I described above.

Billy ONeal
HOW did you compile this, that your saying that it's valid?
There is nothing we can do
@atch: Sorry -- just realized that `char` does not provide a copy constructor like this. I have edited in an example that makes use of the default constructor syntax though, which initializes all the chars with zero.
Billy ONeal
And about syntax: I think syntax I've presented is quite ok.
There is nothing we can do
@atch: You have provided no syntax. If you want to be able to construct an array with values, then you need to be able to provide as many constructor argument sets as there are values in the array. We're talking about small potatoes in any case -- 99.9% of the time the allocation time will be much longer than constructor execution time.
Billy ONeal
Thanks, I was looking for exactly what you've presented.
There is nothing we can do
Can't agree with you on the last one. In my syntax char* p = new char[size](<params>); you can specify as many elems as you wish.
There is nothing we can do
@atch: You run into difficulty when a constructor requires more than one argument. Rest assured, there is a reason no such syntax is in the standard. I have edited my above answer to answer some more of your specific questions (now that I've pieced apart your question). I hope that's helpful :)
Billy ONeal
A: 

The C++ philosophy is - don't pay for something you don't need.

And I think the behviour is pretty unified. If your UDT didn't have a default constructor, nothing would be run anyway and the behaviour would be the same as for built-in types (which don't have a default constructor).

Vilx-
Almost. All UDTs -- even POD types -- have a default constructor provided by the compiler. If it's just a POD struct then the default constructor does not need to do anything, but the issue is there nonetheless.
Billy ONeal
But it wont allow me to create an array of objects if I won't provide dflt ctor. And if the philosophy is that I shouldn't pay for what I don't need why do I have to pay for invoking dflt ctor?
There is nothing we can do
"But it wont allow me to create an array of objects if I won't provide dflt ctor." <-- Yes, it will. Give me a sec and I'll edit that into my answer.
Billy ONeal
@atch: Found a better answer. You can create an array without default constructors if you use placement new. See http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10
Billy ONeal
@Billy ONeal thanks.
There is nothing we can do
A: 

Why is it that for user defined types when creating an array of objects every element of this array is initialized with the default constructor, but when I create an array of a built-in type that isn't the case?

But if the default constructor of an object does nothing then it is still not initialiized.

class X
{
     public:
         char y;
}
X* data = new X[size]; // defaut constructor called. But y is still undefined.

And second question: Is it possible to specify default value to be used while initializing elements in the array? > Something like this (not valid):

Yes:

char                data1[size]  = { 0 };
std::vector<char>   data2(size,0);
char*               data3 = new char[size];
memset(data3,0,size);

Is there a way to avoid/circumvent this default construction somehow? It seems like bit of a waste if I for example have created an array with size 10000, which forces 10000 default constructor calls, initializing data which I will (later on) overwrite anyway.

Yes. Use a std::vector.

You can reserve the space for all the elements you need without calling the constructor.

std::vector<char>    data4;
data4.reserve(size);
Martin York
A: 

That's how built-in types work in C++. In order to initialize them you have to supply an explicit initializer. If you don't, then the object will remain uninitialized. This behavior is in no way specific to arrays. Standalone objects behave in exactly the same way.

One problem here is that when you are creating an array using new[], you options for supplying an initializer (in the current version of the language) are very limited. In fact, the only initializer you can supply is the empty ()

char* p = new char[size](); 
// The array is filled with zeroes

In case of char type (or any other scalar type), the () initializer will result in zero-initialization, which is incidentally what you tried to do.

Of course, if your desired default value in not zero, you are out of luck, meaning that you have to explicitly assign the default values to the elements of the new[]-ed array afterwards.

As for disabling the default constructor call for arrays of types with user-defined default constructor... well, there's no way to achieve that with ordinary new[]. However, you can do it by implementing your own array construction process (which is what std::vector does, for one example). You first allocate raw memory for the entire array, and then manually construct the elements one-by-one in any way you see fit. Standard library provides a number of primitive intended to be used specifically for that purpose. That includes std::allocator and functions like uninitialized_copy, uninitialized_fill and so on.

AndreyT