Sign in to follow this  
Ashaira

Array of pointers help

Recommended Posts

Well since you've all been so helpfull might as well ask for ur help once again.

Until yesterday i was using a fixed array of pointers for loading game objects but now i wanted to make it dynamic. I searched how to do it and found a way which should be correct however something happens and the pointers lose their data. I made sure to run step by step when they are filled with data and that is done right, during loading everything seems to be fine but when accessing the objects update function it crashed with an access violation error. I put in a break point and to my surprise the objects were empty. none of the variables had any values.

this is how i used the array of pointers before when it was working.

[code]CGameObject *GameObjects[50];
GameObjects[tmpID-1] = new CGameObject(vec3(tmpPosX,tmpPosY,tmpPosZ),vec3(tmpRotX,tmpRotY,tmpRotZ),vec3(tmpScaleX,tmpScaleY,tmpScaleZ),tmpModel,tmpTexture1,tmpTexture2,tmpTexture3,tmpTexture4,tmpShader,Resources->Models[tmpModel-1].model);[/code]

but now i used this

[code]CGameObject **GameObjects;
GameObjects = new CGameObject *[ObjectsLoaded];
GameObjects[tmpID-1] = new CGameObject(vec3(tmpPosX,tmpPosY,tmpPosZ),vec3(tmpRotX,tmpRotY,tmpRotZ),vec3(tmpScaleX,tmpScaleY,tmpScaleZ),tmpModel,tmpTexture1,tmpTexture2,tmpTexture3,tmpTexture4,tmpShader,Resources->Models[tmpModel-1].model);[/code]

i cant understand how it can load the data to the objects corectly but lose it somewhere along the line. i even removed any code that was deleting the objects just to make sure. Did i implement the pointer array wrong?

EDIT: well this is embaressing. i had the memory alocation in the same loops as the object constructing so i was overwriting the pointers...... hehe sry about the bother.

Share this post


Link to post
Share on other sites
Why don't you use std::vectors (or any other STLcontainer)? With it you easily can achieve the same and if you preallocate space the objects will most likely stored in the same way as your array.

Share this post


Link to post
Share on other sites
[quote name='doeme' timestamp='1333616773' post='4928424']
Why don't you use std::vectors (or any other STLcontainer)? With it you easily can achieve the same and if you preallocate space the objects will most likely stored in the same way as your array.
[/quote]

right and then u can go ahead and face my boss when she sees i used stl again.

Share this post


Link to post
Share on other sites
[quote name='Ashaira' timestamp='1333616975' post='4928425']
right and then u can go ahead and face my boss when she sees i used stl again.
[/quote]

I will gladly do that, because I think your boss needs some pretty good arguments for this :) But let's keep the STL-yes-or-no-discussion aside. I meant no offense to you, I just trying to keep people from reinventing the wheel.

Share this post


Link to post
Share on other sites
[quote name='Ashaira' timestamp='1333616975' post='4928425']
[quote name='doeme' timestamp='1333616773' post='4928424']
Why don't you use std::vectors (or any other STLcontainer)? With it you easily can achieve the same and if you preallocate space the objects will most likely stored in the same way as your array.
[/quote]

right and then u can go ahead and face my boss when she sees i used stl again.
[/quote]
sounds like your boss is an idiot.

Share this post


Link to post
Share on other sites
just write

[source]
std::vector<CGameObject *> GameObjects;

GameObjects.pushback(new CGameObject(vec3(tmpPosX,tmpPosY,tmpPosZ),vec3(tmpRotX,tmpRotY,tmpRotZ),vec3(tmpScaleX,tmpScaleY,tmpScaleZ),tmpModel,tmpTexture1,tmpTexture2,tmpTexture3,tmpTexture4,tmpShader,Resources->Models[tmpModel-1].model));

[/source]

And tell your boss that you just saved weeks worth of work and painful debugging.

Share this post


Link to post
Share on other sites
[quote name='doeme' timestamp='1333617433' post='4928427']
[quote name='Ashaira' timestamp='1333616975' post='4928425']
right and then u can go ahead and face my boss when she sees i used stl again.
[/quote]

I will gladly do that, because I think your boss needs some pretty good arguments for this [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] But let's keep the STL-yes-or-no-discussion aside. I meant no offense to you, I just trying to keep people from reinventing the wheel.
[/quote]

no worries. i uped ur solution anyway as it is good just not in my case currently.

EDIT. btw one funny thing i heard regarding STL. someone did a check on one of the projects and it had 12000 buffers set out of which 7000 were 4 bytes big. :))

Share this post


Link to post
Share on other sites
[quote name='Ashaira' timestamp='1333627060' post='4928448']
EDIT. btw one funny thing i heard regarding STL. someone did a check on one of the projects and it had 12000 buffers set out of which 7000 were 4 bytes big. :))
[/quote]That's either:
1. Someone not using the STL properly, and actually allocating 4 byte buffers - in which case not using the STL wouldn't help
2. Iterator debugging or secure STL overhead, in which case iterator debugging and secure STL could be disabled.

Also, it's technically not the STL (Standard Template Library), it's the Standard C++ Library. Telling you not to use the STL is like telling you not to use for loops, which is equally batshit insane.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this