Archived

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

CrackMonkeyT

n00b programming question

Recommended Posts

okay, I have a struct that looks like: struct Vector3d { float coord[3]; }; and three vars that are of this "type"; cameraPos, cameraFocus, and cameraOrient. Here is the code I use to put stuff in those vars (they are member vars): this->cameraPos->coord[X] = 0.0f; this->cameraPos->coord[Y] = 0.0f; this->cameraPos->coord[Z] = 0.0f; this->cameraFocus->coord[X] = 1.0f; this->cameraFocus->coord[Y] = 1.0f; this->cameraFocus->coord[Z] = 1.0f; this->cameraOrient->coord[X] = 0.0f; this->cameraOrient->coord[Y] = 1.0f; this->cameraOrient->coord[Z] = 0.0f; It has no problem with the first 3 (the cameraPos ones), or the cameraOrient ones, but for some reason it crashes on the cameraFocus ones (with a signal 10). I''m writing it on a BSD platform, if that matters. Here are the definitions for the vars, if it matters: Vector3d *cameraPos; //Where the camera is Vector3d *cameraFocus; //what the camera is looking at Vector3d *cameraOrient; //rotation of camera And that is copied and pasted. Thank you in advance for any help that can be offered, --Me

Share this post


Link to post
Share on other sites
Are you actually creating the objects with new? You can''t just use the pointer without initializing it or creating an object for it.

this->cameraFocus = new Vector3d;
this->cameraFocus->coord[X] = 1.0f;
this->cameraFocus->coord[Y] = 1.0f;
this->cameraFocus->coord[Z] = 1.0f;

Share this post


Link to post
Share on other sites
quote:
Original post by cgoat
Are you actually creating the objects with new? You can''t just use the pointer without initializing it or creating an object for it.

this->cameraFocus = new Vector3d;
this->cameraFocus->coord[X] = 1.0f;
this->cameraFocus->coord[Y] = 1.0f;
this->cameraFocus->coord[Z] = 1.0f;



um... jepp...
if you''re doing this in C (not C++), you might want to use malloc:

  
//doing this is faster than calling malloc 3 times

this->cameraPos = (Vector3d*)malloc(3*sizeof(Vector3d));
this->cameraFocus = cameraPos + sizeof(Vector3d);
this->cameraOrient = cameraPos + 2*sizeof(Vector3d);

this->cameraPos->coord[X] = 0.0f;
this->cameraPos->coord[Y] = 0.0f;
this->cameraPos->coord[Z] = 0.0f;

this->cameraFocus->coord[X] = 1.0f;
this->cameraFocus->coord[Y] = 1.0f;
this->cameraFocus->coord[Z] = 1.0f;

this->cameraOrient->coord[X] = 0.0f;
this->cameraOrient->coord[Y] = 1.0f;
this->cameraOrient->coord[Z] = 0.0f;


Share this post


Link to post
Share on other sites
Cool. Thanx for all your help. I put a

this->cameraPos = new Vector3d;

etc. etc. in the class constructor and that seemed to take care of the problem. Now I''m just having problems with my linked-list implimentation... Oh well, I hacked that together in a bout 30 seconds just to put something there, so I have a feeling that it needs a complete re-work as it is.

Again, thanx for all your help,
--Me

Share this post


Link to post
Share on other sites
Now that you''ve done the new, are you doing the delete?

You have to remember to always delete the memory you allocate with new when you are done with it...otherwise this is a memory leak. I would advise just using full objects instead of pointers.

Regards,
Jeff

Share this post


Link to post
Share on other sites
That''s why I''m going to be moving all of this stuff over to their own classes. That way when everything is said and done, I will delete everything that is held inside this class. In the end, Vector3d and PolyList will both be their own classes, and PolyList will be a sub-class of LinkedList. If it starts to get un-weildy, then you all may be hearing from me again.

Peace,
Thomas

Share this post


Link to post
Share on other sites