value[0] lost in an arrary.

J

Jacob

I just wrote a function that draws a fractal by recursively calling
itself, and ran into an odd (odd from my point of view anyway) problem.
I've managed to solve (or rather, circumnavigate) the problem, but I still
don't understand why it happened and would like to.
For starters, here's the function:

void draw_series(SDL_Surface *img, float x1, float y1, float x2, float y2,
MAT_matrix<float> *pointlist, int depth, int R, int G, int B)
{
int n;
float px1, py1, px2, py2;
for(n = 0; n < pointlist->xsize - 1; n++){
px1 = x1 + (x2 - x1) * pointlist->value[n];
py1 = y1 + (y2 - y1) * pointlist->value[n];
px1 += (y2 - y1) * pointlist->value[n + pointlist->xsize];
py1 -= (x2 - x1) * pointlist->value[n + pointlist->xsize];

px2 = x1 + (x2 - x1) * pointlist->value[n + 1];
py2 = y1 + (y2 - y1) * pointlist->value[n + 1];
px2 += (y2 - y1) * pointlist->value[n + pointlist->xsize + 1];
py2 -= (x2 - x1) * pointlist->value[n + pointlist->xsize + 1];

if(depth){
draw_series(img, px1, py1, px2, py2, pointlist, depth - 1, R, G, B);
}else{
gfx_line(img, (int)px1, (int)py1, (int)px2, (int)py2, R, G, B);
}
}

}

The version shown here works fine. My problem occurred with an earlier
rendition of it, which used exactly the same code as above with one
exception. Instead of passing a pointer to the "pointlist" object, it
passed the object itself. To see what the code looked like, just replace
every occurance of "->" with ".", and remove the * in the first line.
When I did that, the value of pointlist.value[0] was set to 0. All of the
other values within it were fine. So for instance, if it contained a
series of values like (3, 1, 4, 1, 5, 9), those values would become (0, 1,
4, 1, 5, 9)
pointlist is an object of a matrix class I made. The "value" variable is a
pointer to some memory allocated using "new". It uses a template (in this
case it's a float). I don't know if there's any other information that
would pertain to the problem. Again, it was only the [0] value of the
array that would be set to 0.0. All of the other values in that array
(and all other values within that object) remained unscathed.

Does anyone know what would cause this?
 
V

Victor Bazarov

Jacob said:
I just wrote a function that draws a fractal by recursively calling
itself, and ran into an odd (odd from my point of view anyway) problem.
I've managed to solve (or rather, circumnavigate) the problem, but I still
don't understand why it happened and would like to.
For starters, here's the function:

void draw_series(SDL_Surface *img, float x1, float y1, float x2, float y2,
MAT_matrix<float> *pointlist, int depth, int R, int G, int B)
{
int n;
float px1, py1, px2, py2;
for(n = 0; n < pointlist->xsize - 1; n++){

Why "- 1"?
px1 = x1 + (x2 - x1) * pointlist->value[n];
py1 = y1 + (y2 - y1) * pointlist->value[n];
px1 += (y2 - y1) * pointlist->value[n + pointlist->xsize];

When n == pointlist->xsize - 2, i.e. at the last iteration, you're adding
a whole 'pointlist->xsize' to it. What element does it represent? Does
your "matrix" have the element [pointlist->xsize*2 - 2]?
py1 -= (x2 - x1) * pointlist->value[n + pointlist->xsize];

px2 = x1 + (x2 - x1) * pointlist->value[n + 1];
py2 = y1 + (y2 - y1) * pointlist->value[n + 1];
px2 += (y2 - y1) * pointlist->value[n + pointlist->xsize + 1];
py2 -= (x2 - x1) * pointlist->value[n + pointlist->xsize + 1];

if(depth){
draw_series(img, px1, py1, px2, py2, pointlist, depth - 1, R, G, B);
}else{
gfx_line(img, (int)px1, (int)py1, (int)px2, (int)py2, R, G, B);

What does 'gfx_line' do, anything relevant?
}
}

}

The version shown here works fine. My problem occurred with an earlier
rendition of it, which used exactly the same code as above with one
exception. Instead of passing a pointer to the "pointlist" object, it
passed the object itself. To see what the code looked like, just replace
every occurance of "->" with ".", and remove the * in the first line.

With all due respect, seeing "what the code looked like" is pointless
unless we can understand what it _did_, and it's impossible without the
definition of your MAT_matrix template.
When I did that, the value of pointlist.value[0] was set to 0. All of the
other values within it were fine. So for instance, if it contained a
series of values like (3, 1, 4, 1, 5, 9), those values would become (0, 1,
4, 1, 5, 9)
pointlist is an object of a matrix class I made. The "value" variable is a
pointer to some memory allocated using "new".

Here is more reason to see what your MAT_matrix looks like. Do you manage
the dynamic memory correctly? Passing by value uses the copy-constructor,
did you define it properly? Do you know of the "Rule of Three"? Did you
follow it?
It uses a template (in this
case it's a float). I don't know if there's any other information that
would pertain to the problem. Again, it was only the [0] value of the
array that would be set to 0.0. All of the other values in that array
(and all other values within that object) remained unscathed.

Does anyone know what would cause this?

Bad memory management for one.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top