Initializing dynamic arrays

This is a discussion on Initializing dynamic arrays within the C++ Programming forums, part of the General Programming Boards category; How do you initialize an array which was created in run-time?
With normal arrays, I learned that you can do ...

The C function calloc (which dynamically allocates memory and sets it to zero) is an alternative in some cases. The problem with using calloc() is that it does NOT work with C++ class types that have constructors (the constructors will not be called). Similarly, memset() can be used to set values in an array, but it assumes it is initialising an array of chars.

int* i;
i = new int[j]; //j would be some value you got through user input

There are some fundamental misunderstandings in evidence in that code. The operator new returns the address of where the array is located in memory. So, 'i' is assigned an address. If this statement worked:

Code:

i = {0,};

'i' would be overwritten and no longer contain the address. The way you access the values that a pointer points to is by derefencing the pointer: *i.

In C++, you generally use vector for your dynamic array needs, and vectors initialize their values automatically to 0 (or with the default constructor). You can also initialize them to a value other than zero.

I did not know that, whereas I am aware that vectors exist (I picked it up while mindlessly surfing the net).

However, I didn't really feel comfortable using them since I'm new to c++ programming and I have no understanding of the internal structure of vector templates (they are templates, right?).

While I am quite confident that the designers of STL are far, far more proficient in c++ than I am, and that (in compiled form) vectors would probably be as fast as dynamically allocated arrays for basic types (are they?), I still feel I'm reaching a bit when I move on to STL when I'm still trying to figure out the most fundamental aspects of c/c++.

So, question: when is it the right time to start learning STL (I'm kinda past the pointers to pointers stage, and I'm trying to figure out linked lists right now)?

However, I didn't really feel comfortable using them since I'm new to c++ programming and I have no understanding of the internal structure of vector templates (they are templates, right?).

The beginning C++ book "Accelerated C++" starts right into the STL and covers vectors before talking about arrays or pointers.

So, question: when is it the right time to start learning STL (I'm kinda past the pointers to pointers stage, and I'm trying to figure out linked lists right now)?

Now. You can still understand the mechanics(simple) without understanding how they work. My 1,000 page beginning C++ book doesn't talk about the STL until the last chapter. To gain some understanding of how they work, you need to learn about pointers. To really understand them, you have to study what "iterators" are, i.e. so called "smart" pointers, and to understand them you need to know about objects, classes, and overloading operators.

As grumpy already mentioned, memset assumes chars, so that will not initialize the whole array, just the first 100 bytes. If int is 4 bytes on your platform, there will be 75 uninitialized values in that array.