Archived

This topic is now archived and is closed to further replies.

Xiachunyi

Variable global Structs

Recommended Posts

Hello, I am recently trying to make some of the variables in my game dynamic, especially the particle generators, and I seem to have run across the problem of dynamically resizing them. First of all, how would I create a changable(sp) declaration/variable that is global and can be used with another global variable.
//Use to:

//~~~~~Falldown Particles

    #define falldown_objects 100
    typedef struct
    {
        double size;
        double x_pos;
        double y_pos;
        double speed_fall;
        double red;
        double green;
        double blue;
        double fade;
        int exchange;
    }
    falldown_struct;
    falldown_struct fall[falldown_objects];
    int fall_change_sign=0;

//What I am trying to do

//~~~~~Falldown Particles

    unsigned int falldown_objects=100;
    typedef struct
    {
        double size;
        double x_pos;
        double y_pos;
        double speed_fall;
        double red;
        double green;
        double blue;
        double fade;
        int exchange;
    }
    falldown_struct;
    falldown_struct fall[falldown_objects];
    int fall_change_sign=0;
Well, something to that effect, but the compiler throws me a "variable-size type declared outside of any function" error. I plan on having an option in my "option" menu that will allow users to specify how many particles they want for each effect in case they want to see total chaos or just a few small "innuendos" of the effect itself. I want the above to be a global variable "class" since more than 5 to 6 functions in my game actually manipulate the variables themselves. Another question: If I were to say get the pathname of my application of via "GetModuleFileName", how do I resize the array of a character array before the function actually stores the string into that variable.

Share this post


Link to post
Share on other sites
I could'nt quite follow all of what you are asking, but as to dynamically allocating that is easy. If you are going to base the array size on a variable you need to do dynamic allocation like so:


// Sorry but I prefer C++ style

struct ExampleStruct
{
int memberA;
int memberB;
};

ExampleStruct *fall; // declare as pointer

unsigned int falldown_objects = 100;

// Then in code somewhere you do actual allocation

// I'll use main as an example

int main()
{
// code ...

fall = new ExampleStruct[falldown_objects];
// more code ...


// Just make sure you free the memory when you are done

delete [] fall;
}


"That's a very nice hat."
-Korben Dallas

[edited by - GHoST on October 23, 2003 10:49:25 PM] Had to fix source tags

[edited by - GHoST on October 23, 2003 10:51:27 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by GHoST
I could''nt quite follow all of what you are asking, but as to dynamically allocating that is easy. If you are going to base the array size on a variable you need to do dynamic allocation like so:


// Sorry but I prefer C++ style

struct ExampleStruct
{
int memberA;
int memberB;
};

ExampleStruct *fall; // declare as pointer

unsigned int falldown_objects = 100;

// Then in code somewhere you do actual allocation

// I''ll use main as an example

int main()
{
// code ...

fall = new ExampleStruct[falldown_objects];
// more code ...


// Just make sure you free the memory when you are done

delete [] fall;
}


"That''s a very nice hat."
-Korben Dallas

[edited by - GHoST on October 23, 2003 10:49:25 PM] Had to fix source tags

[edited by - GHoST on October 23, 2003 10:51:27 PM]


Shouldn''t that be delete fall instead of delete [] fall? I thought you would only need to use [] if you were to use something like a pointer to a pointer or am I wrong?

Share this post


Link to post
Share on other sites
nope, basically use delete [] to delete a dynamically allocated array, so...


ExampleStruct *fall = new ExampleStruct[100];
delete [] fall;

// For single object allocation, just delete

ExampleStruct *fall2 = new ExampleStruct;
delete fall2;


As to your question about a pointer to a pointer there are various ways you may have to free up memory dynamically allocated for it.
You could end up doing something like


ExampleStruct **fall3;
fall3 = new ExampleStruct*[100]; // allocates 100 pointers

for (int i = 0; i < 100; ++i)
fall3[i] = new ExampleStruct[10]; // allocates 10 to each


// So fall3 points to a multidimensional array of ExampleStructs

// You will need to delete [] each of the 100 pointers with a

// loop, then you have to delete [] fall3 itself

for (int j = 0; j < 100; ++j)
delete [] fall3[j];

delete [] fall3;


My point here is that you can''t just delete [] fall3, you would have stranded all the memory allocated to each element of fall3


"That''s a very nice hat."
-Korben Dallas

Share this post


Link to post
Share on other sites
quote:

My point here is that you can''t just delete [] fall3, you would have stranded all the memory allocated to each element of fall3



K thanks. So you have to free the memory of every array object you created before? What happens if I don''t release it correctly would that cause any problems in particular?

Share this post


Link to post
Share on other sites
If you don't free an object the memory is stranded (memory leak) since your system only has so much memory, this is bad, and can get very bad if allocation happens on a regular basis while your program is running. If you do delete instead of delete [] you MIGHT get a notice of an assertion failure when in debug (assuming MSVC++) but don't count on it. Most likely you will just fail to free all the memory, so you still have a memory leak;

if you want a quick visual example use this modified form of my previous example


int dcounter = 1;
int main()
{
struct ExampleStruct
{
~ExampleStruct(){cout << dcounter++ << endl;}
};

ExampleStruct **fall3;
fall3 = new ExampleStruct*[100]; // allocates 100 pointers

for (int a = 0; a < 100; ++a)
fall3[a] = new ExampleStruct[10]; // allocates 10 to each


for (int j = 0; j < 100; ++j)
delete /*[]*/ fall3[j]; // uncomment the []


delete [] fall3;

return 0;
}


Notice that the delete only gives you 100 destructor calls, but delete [] gives all 1000

If you are using MSVC++, compile in release mode to avoid debug assertion failures


"That's a very nice hat."
-Korben Dallas

[edited by - GHoST on October 23, 2003 12:17:23 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by GHoST
I could''nt quite follow all of what you are asking, but as to dynamically allocating that is easy. If you are going to base the array size on a variable you need to do dynamic allocation like so:
...
"That''s a very nice hat."
-Korben Dallas

[edited by - GHoST on October 23, 2003 10:49:25 PM] Had to fix source tags

[edited by - GHoST on October 23, 2003 10:51:27 PM]


Thankyou so much GHoST, I''ve been wanting to resize structures for a long time but always fail because there is something wrong.

Also, I am programming in C++, but I don''t do OOP very much, I like procedural.

Thanks again!

Share this post


Link to post
Share on other sites