Is pre-defining lots of objects inefficient?

I understand that using global variables is a bad idea, because that variable is taking up memory space the entire time the program is running even though it's rarely used. I'm wondering if declaring every object in my game project at once has a similar effect, if it's highly inefficient and unecessarily computer intensive (for what it is).

I get that I could use pointers and new to create objects on the fly whenever they need to appear (then delete;), but what happens if I need to pre-define parameters? Surely the new method would only work if you were generating random objects or something?

Here's what my program looks like:

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
    int main()
    {
        /// Declare Objects:

        const int MaxActors = 6;
        Actor ACT[MaxActors];
        //               Name (14 chars) , HP , AP, AT, DE, MA, SP, HI, EV, SP, LU, Job, JobName
        ACT[0].set_Stats("Character     ", 999, 99, 25, 25, 25, 25, 25, 25, 25, 25, MAR, "");
        ACT[1].set_Stats("Character     ", 999, 99, 25, 25, 25, 25, 25, 25, 25, 25, MAR, "");
        ACT[2].set_Stats("Character     ", 999, 99, 25, 25, 25, 25, 25, 25, 25, 25, MAR, "");
        ACT[3].set_Stats("Character     ", 999, 99, 25, 25, 25, 25, 25, 25, 25, 25, MAR, "");
        ACT[4].set_Stats("Character     ", 999, 99, 25, 25, 25, 25, 25, 25, 25, 25, MAR, "");
        ACT[5].set_Stats("Character     ", 999, 99, 25, 25, 25, 25, 25, 25, 25, 25, MAR, "");

        const int MaxSkills = 2;
        Skill SK[3][MaxSkills];
        //                       Name (14 chars) , L, TYP, VAL, LP , LPM, StEfct
        SK[0][0].set_Skill("Skill         ", 1, ATK, 255,   0, 999, None);
        SK[0][1].set_Skill("Skill         ", 1, ATK, 255,   0, 999, None);
        SK[1][0].set_Skill("Skill         ", 1, ATK, 255,   0, 999, None);
        SK[1][1].set_Skill("Skill         ", 1, ATK, 255,   0, 999, None);
        SK[2][0].set_Skill("Skill         ", 1, ATK, 255,   0, 999, None);
        SK[2][1].set_Skill("Skill         ", 1, ATK, 255,   0, 999, None);

        const int ItemTotal = 18;
        Items ITEM[ItemTotal+1];
        //                 Q, TYP, Name (14 chars) , Selct.
        ITEM[ 0].set_Items(0, NON, "--------------", false);
        ITEM[ 1].set_Items(0, USE, "AnItem        ", false);
        ITEM[ 2].set_Items(0, USE, "AnItem        ", false);
        ITEM[ 3].set_Items(0, USE, "AnItem        ", false);
        ITEM[ 4].set_Items(0, USE, "AnItem        ", false);
        ITEM[ 5].set_Items(0, EQU, "AnItem        ", false);
        ITEM[ 6].set_Items(0, EQU, "AnItem        ", false);
        ITEM[ 7].set_Items(0, EQU, "AnItem        ", false);
        ITEM[ 8].set_Items(0, EQU, "AnItem        ", false);
        ITEM[ 9].set_Items(0, GEM, "AnItem        ", false);
        ITEM[10].set_Items(0, GEM, "AnItem        ", false);
        ITEM[11].set_Items(0, GEM, "AnItem        ", false);
        ITEM[12].set_Items(0, GEM, "AnItem        ", false);
        ITEM[13].set_Items(0, MAT, "AnItem        ", false);
        ITEM[14].set_Items(0, MAT, "AnItem        ", false);
        ITEM[15].set_Items(0, KEY, "AnItem        ", false);
        ITEM[16].set_Items(0, KEY, "AnItem        ", false);
        ITEM[17].set_Items(0, KEY, "AnItem        ", false);
        ITEM[18].set_Items(0, KEY, "AnItem        ", false);
}


So yeah, if this is perfectly normal or whether it's a complete mess I don't know. Maybe I'm supposed to store all of this in a file then only create an object (using the new method?) with that information when it's encountered?

Thanks in advance for your help.
Last edited on
From what I can see I would learn how to use the STL, for vector classes or list classes. It looks like you have things in Object or structure form. The idea of using vector or list templates is that have a variable list length for things so the use of fixed length arrays wouldn't be needed. Reading things from a file would be fairly efficient. It would simplify the code so you don't have so many repetitive things.
Using global variables is generally considered a bad idea because those variables can be accessed and changed from anywhere in the code base, not because they are inefficient or take up space for the duration of the program's execution.

Using only the stack for memory as you're doing here is certainly unusual in programs that aren't tiny.



Topic archived. No new replies allowed.