n00b programming question

Started by
6 comments, last by CrackMonkeyT 20 years, 11 months ago
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
Advertisement
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;
Better yet, don''t use pointers, just declare the objects as as Vector3d instead of Vector3d*
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 timesthis->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;  


our new version has many new and good features. sadly, the good ones are not new and the new ones are not good
If he was doing it in C, there would be no such thing as "this".
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

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
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

This topic is closed to new replies.

Advertisement