The difference between these 2 array declarations

What's the difference between these declarations of arrays, because the first one works fine, the second doesn't give error but doesn't give the same result

1
2
	CUSTOMVERTEX vertices[8] = {};
	CUSTOMVERTEX *vertices = new CUSTOMVERTEX[8];


closed account (3qX21hU5)
The first array declaration is initializing a array and then initializing all it's elements to their default values. So in your case it would initialize them to whatever values they have by CUSTOMVERTEX's default constructor.

The second array declaration is initializing a array and leaving all it's elements uninitialized. So each element is holding a random value and will be undefined behavior.

For example run this program and study it a bit and see if that can help you figure out why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
    // Will initialize all the elements to 0 since that is the default for integers
    int arrayOne[8] = {};

    // Will create the array with 8 elements but each element will be uninitialzed
    // and will hold some random value or maybe even 0 depending on the
    // implementation. This is undefined behavoir.
    int* arrayTwo = new int[8];

    std::cout << "Array One \n\n";
    for (int i = 0; i != 8; ++i)
        std::cout << arrayOne[i] << std::endl;

    std::cout << "\nArray Two \n\n";
    for (int i = 0; i != 8; ++i)
        std::cout << arrayTwo[i] << std::endl;

    delete[] arrayTwo;
}

Last edited on
The other important difference is that the second declaration dynamically allocates the memory for the array on the heap, and leaves the developer responsible for writing code to free the memory once it's no longer required.

The first declaration reserves memory in a way that depends on the context - it could be global, or it could be local. The lifetime of that memory is determined by the context in which it is declared.
well, I later on I define values for all elements like for ex
1
2
3
4
for (int i = 0; i != 8; ++i)
{
     myArray[i] = 5;
}


so I don't really understand the difference in the behaviour later on
As I understand in both cases "vertices" is basically a pointer so aside from the memory allocation differences both cases should be interchangeable thus can be used in the same way and produce the same results ?
What is the exact problem you are having with the second definition (and not the first)?

The fact is that "array" is one of those words that is horribly overloaded (just like "window" and "class").

In C and C++, an "array" has a specific meaning by language construct. int a[10]; is an array (according to the language). That means that the compiler knows stuff about it.

Your second item int* a; is not an array according to the C++ language. It is a pointer to stuff.

Read more about the differences here:
http://www.cplusplus.com/forum/beginner/119149/#msg649227


Now, the only places where it should make a difference, to you, the programmer, are

     - when you try to get the number of elements in the array from the compiler

     - memory management and scope

Let us know.
Topic archived. No new replies allowed.