Proper declaration of dynamic arrays

Hello,

I started to practice some C++. I use to program in C and a little C++. Anyway, I am writing code that creates a dynamic array. I would like to be able to do something like

galaxyobject[object] -> uniqueid = in the class but I do not think I have it setup right.

Question.
1. Is the code correct?
2. Why can't I use the above line without a compile error or segment fault?


Any comments or help would be appreciated.

Vivienne

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
int main()
{
    cout << "ProteusCore Server" << endl;

    // create a galaxy system with a certain amount of objects
    galaxy galaxysystem;
    galaxysystem.IntializeGalaxy(100);

    galaxysystem.AddObject();

The header in galaxy.h includes

// set a close
class galaxy
{
    private:
    int galaxyobjectstotal;
    galaxyobject* galaxyobjectsptr;

    public:
    galaxy();
    virtual ~galaxy();

    // initlialization members
    int IntializeGalaxy();
    int IntializeGalaxy(int totalobjects);
    int ShutdownGalaxy();

    // object manipulation members
    int AddObject();

    };


galaxy::galaxy()
{
    //ctor
}int main()
{
    cout << "ProteusCore Server" << endl;

    // create a galaxy system with a certain amount of objects
    galaxy galaxysystem;
    galaxysystem.IntializeGalaxy(100);

    galaxysystem.AddObject();

The galaxy.cpp code


galaxy::~galaxy()
{
    //dtor
}

// set initialize initialization to null
int galaxy::IntializeGalaxy()
{
    galaxyobjectstotal=0;
    galaxyobjectsptr=0;

    return true;
}

// resize galaxy to the number of items
int galaxy::IntializeGalaxy(int totalobjects)
{
    galaxyobjectstotal= totalobjects;

    // initializes galaxy objects
    cout << "\r\nInitializating malloc galaxy objects...";

    //galaxyobjectstotal=totalobjects;
    galaxyobjectsptr = new galaxyobject[ galaxyobjectstotal];

    // set to null
    cout << "\r\nSet up initial values to galaxy objects as null...";

    for(int i=0; i<= galaxyobjectstotal; i++)
    {
        // null everything
        galaxyobjectsptr[i].uniqueid=NULL;
        galaxyobjectsptr[i].galaxyid=NULL;
        galaxyobjectsptr[i].type=NULL;
        galaxyobjectsptr[i].created=NULL;
        galaxyobjectsptr[i].lifetime=NULL;
        galaxyobjectsptr[i].locx=NULL;
        galaxyobjectsptr[i].locy=NULL;
        galaxyobjectsptr[i].locz=NULL;
        galaxyobjectsptr[i].rotx=NULL;
        galaxyobjectsptr[i].roty=NULL;
        galaxyobjectsptr[i].rotz=NULL;
        galaxyobjectsptr[i].vel=NULL;
    }

    return true;
}


PS. I made the code available to see on sourceforge as Proteus 3d Game Engine. It is something I would like to work on.
Last edited on
I see no question mark... therefore I see no question, that I could try to answer.

What exactly do you need help with?
Is the code not working?
Do you want to learn about dynamic memory allocation in C++?

Also, please put your code in code tags. You may edit your previous post to do this.

[code]const int i=0; // example code [/code]
Fixed like you recommended. I'm just not sure if the code is correct.
Last edited on
I can see a small mistake.

73
74
75
76
77
78
79
    //galaxyobjectstotal=totalobjects;
    galaxyobjectsptr = new galaxyobject[ galaxyobjectstotal];

    // set to null
    cout << "\r\nSet up initial values to galaxy objects as null...";

    for(int i=0; i<= galaxyobjectstotal; i++)


I quoted more code than the mistake itself (which is underlined) in order to provide context.

As you know, in C and C++ counting starts from 0, not 1.

This means that an array A that has 3 elements will provide A[0], A[1] and A[2].
It is important to notice that A[3] does not exist.

Trying to access A[3], A[4] or A[627] means that you're going out of bounds. This is undefined behavior, which in turn means that any of these things can happen randomly:
a) it works and you get 0
b) it works and you get a garbage value (e.g. -68476)
c) it crashes with a segmentation fault

So you are allocating an array of length galaxyobjectstotal.
This means that the the index i of the for() loop can go as high as (galaxyobjectstotal - 1) and no higher.

1
2
3
for(int i=0; i < galaxyobjectstotal; i++) // same as

for(int i=0; i <= galaxyobjectstotal - 1; i++) // but the above is preferred by most people 


Anyway, I am writing code that creates a dynamic array. I would like to be able to do something like

galaxyobject[object] -> uniqueid = in the class but I do not think I have it setup right.


From what I see, you already figured out that the correct form is: galaxyobject[object].uniqueid

Also don't forget to put a delete[] galaxyobjectsptr; somewhere, perhaps in the destructor.
Last edited on
Topic archived. No new replies allowed.