
-----------------------------------
apython1992
Tue Jun 28, 2011 12:40 pm

Pointer Location Changing
-----------------------------------
Hey guys.  So a while back I started writing a raytracer in Python, which was a lot of fun, but after wiping out my hard drive and forgetting to back it up, I lost it for good, and so here I am starting it again, this time in C++.  My knowledge in C++ is extremely limited, and I'm mainly doing this to pick up some knowledge on pointers and all that jazz.  Anyway, I've started laying out the groundwork for the raytracer, and I'm doing some primitive testing right now to see if my primary rays intersect a plane I set into the scene.  I've been treating almost everything with pointers, because it involves passing a lot of objects around.  I have a class for primitive objects, like planes, spheres, and rays, and I figured it would be easiest to represent a plane by its normal vector with an origin.  So, to get a "back wall" plane for example, i would create a plane with a normal vector of 

#include 
#include 
#include "plane.h"
#include "sphere.h"
#include "ray.h"

/*
Dimensions are:
x - increasing from left to right
y - increasing from down to up
z - increasing going into the screen
*/

#define SCREEN_X    1280
#define SCREEN_Y    800
#define DEPTH       16

const int SCENE_POS

Without me seeing much of any kind of C++ code, I have no idea what kinds of coding practices are good/not so good, so I'd appreciate some feedback there as well. Thanks!

-----------------------------------
DemonWasp
Tue Jun 28, 2011 9:02 pm

RE:Pointer Location Changing
-----------------------------------
I'm using g++ 4.4.5.

I compiled with "-Wall -Wextra -Werror -pedantic", with varying optimization levels. Using different levels of optimization often points out subtle bugs, because optimization really makes use of the fact that C++ is a ridiculously underdefined language, so anything you're doing that's even slightly undefined comes to focus very quickly.

I didn't get any warnings until I turned on optimization (-02), which I find to be curious. The one warning I did get was that in raytrace(Ray*), you potentially use intersect before it's initialized (if there are no planes)...not your problem, but good to fix.

Output with varying levels of optimization:

-O0, or no optimization:
on the stack (Ray, Plane, Sphere), take their addresses, and store them as pointers. This is a big no-no, as they will then out-live the stack frame they were allocated in, and subsequent stack activity will stomp all over them and their precious data. If your objects are going to outlive the method, then they should be allocated on the heap, with new (or malloc, etc).

I'm pretty sure this is your problem (though, I would have expected g++ to warn me about that...). This neatly explains why our results vary, and why my results vary depending on optimization level: the stack frame is of a different size, which means that later stack activity overwrites it in different ways (or, doesn't actually overwrite it, whichever).

-----------------------------------
apython1992
Tue Jun 28, 2011 10:15 pm

RE:Pointer Location Changing
-----------------------------------
My gosh, you're totally right.  I'm well aware that declaring pointers on the stack is a big no-no because they will just get popped off anyway, but for some reason here I was just treating them as temporary pointers for permanent objects to contain...I forgot to note that the internal pointers would of course need to hang around afterwards. Doh! Thanks for the help Demonwasp, I realize this one was a bit more of a pain what with the several source files and all. I can't fix it right now to guarantee that this will solve the problem, but I'll let you know how this goes once I can change it tomorrow. Thank you!

-----------------------------------
apython1992
Fri Jul 01, 2011 9:45 pm

RE:Pointer Location Changing
-----------------------------------
I got around to addressing the problem.  I allocated the pointers from the heap instead, and as expected it works like a charm. Thanks, DemonWasp!

-----------------------------------
DemonWasp
Sat Jul 02, 2011 1:20 am

RE:Pointer Location Changing
-----------------------------------
No problem. I find it weird that g++ refused to warn about this -- it knows the objects are being allocated on the stack, and it knows that you're sending their addresses out of the frame, so...where's the warning? I'm pretty sure I've seen compilers warn about this before...

-----------------------------------
apython1992
Sat Jul 02, 2011 10:37 am

RE:Pointer Location Changing
-----------------------------------
I find that really bizarre too, I'm sure I've been warned of this at other places in the project.  Anyway, at least it's fixed now.
