-1

In class Foo, I have a member variable of type Bar. Bar is constructed in my Foo constructor by passing in a pointer to an array like this:

template<typename> T
class Foo{
Bar myBar;
size_t mySize;
size_t top;
Foo(size):myBar(new T[size]),mySize(size),top(0){}; //creates a Bar element of certain size
}

template<typename> T
class Bar{
T* myListPtr;
Bar::Bar(T* tPtr):myListPtr(tPtr){}  //stores a pointer to a T array
}

Now to my knowledge, if T has a default constructor, C++ should be calling this on all elements in the array. However when I output the values that are being pointed to, I got an array like this(with size=8 and T type 'double')

9.3218e-306
0
0
0
0
3.81522e-270
nan
nan

How do I make sure that all the values are default initialized and not just some. This array might hold char, double, any type, but the type will always have have a default constructor. What could be causing this problem?

bigteeth
  • 5
  • 3
  • 1
    Please post an [MCVE](http://stackoverflow.com/help/mcve). – juanchopanza Oct 05 '14 at 16:41
  • Why do you think any element is copied when you copy a pointer to an array of elements? – Deduplicator Oct 05 '14 at 16:42
  • 3
    @Chris Well, no, not always. And there is no such thing as "C/C++" so please stop spreading that myth. Cheers. – Lightness Races in Orbit Oct 05 '14 at 16:42
  • http://stackoverflow.com/questions/2204176/how-to-initialise-memory-with-new-operator-in-c – happydave Oct 05 '14 at 16:42
  • @Deduplicator why wouldn't it? I've created an item on the heap that hasn't been deleted.. – bigteeth Oct 05 '14 at 16:45
  • @LightnessRacesinOrbit Sigh, no not always, but it's easiest to explain that way. And I realize that C and C++ are different languages, but since one is a subset of the other it's easiest to refer to them as such. – Krease Oct 05 '14 at 16:46
  • @bigteeth: Copying a pointer does not copy the pointee. – Deduplicator Oct 05 '14 at 16:46
  • Apologies; seems I'm causing more confusion than helping. I'll delete the misleading comment then. – Krease Oct 05 '14 at 16:48
  • 1
    @Chris: C is _not_ a subset of C++. – Lightness Races in Orbit Oct 05 '14 at 16:49
  • @happydave of course!! I did not realize that I actually had to call the default constructor on each element in an array as well, was under the impression that declaring one automatically calls the default constructor in it. Thanks, that solves the issue!! – bigteeth Oct 05 '14 at 16:50
  • @Deduplicator I do not mean to copy the pointee. As long as I have the pointer(with the address of the array), I can access the pointee. Assuming it hasn't been deleted, which in this case hasn't happened. – bigteeth Oct 05 '14 at 16:52
  • 1
    Instead of posting comments, why don't you spend some time preparing an MCVE? Otherwise you cannot expect a good answer to your question. – juanchopanza Oct 05 '14 at 16:53

1 Answers1

1

Now to my knowledge, if T has a default constructor, C++ should be calling this on all elements in the array. However when I output the values that are being pointed to, I got an array like this(with size=8 and T type 'double')

You are correct in that the default constructor will be used if one exists. However primitive types like double (and int, float, char, etc) do not have constructors, default or otherwise. Although the language lets you work with primitive types and class types with the same syntax in many cases, this is a fundamental difference.

So given a class MyClass then

MyClass myInstance;

will call its default constructor.

But given a double, then

double myDouble;

will not initialize it in any way.

As a consequence, creating an array of doubles as you do in your example will not initialize them.

Now the next logical question would be how to initialize them. This stackoverflow question provides an answer and also gives the worthwhile advice that using std::vector can help avoid some of the troubles that raw arrays often have.

Community
  • 1
  • 1
TheUndeadFish
  • 8,058
  • 1
  • 23
  • 17