Archived

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

Having a strange problem with arrays

This topic is 5279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm having a problem with a prog i'm writin. I created a class Model, having several arrays. One function, AddVerticeToArray, has to fill one array by getting info in another one, depending on a binary tree. The array to be filled is strangely empty, expect for the three first cells. ( i'm storing points coordinates, X, Y, Z, ie here only the coordinates of the first point are correct. To debug, i added a printf verifying in AddVerticeToArray that the data is copied. Here is the strangeness: when i test the data in the function, it is here, when i test it after, it is gone. I thought that maybe the memory was unallocated, but in that case, why would i get zero's ? here is the part of the code i'm having problem with:

bool Model::StartProcess()
{

if(!(this->reducedVerticesArray = (Point*)calloc ( this->rootCluster.ReducedPointsContained , 3*sizeof(float))))printf ("Memory allocation failed\n");;
this->reducedVerticesArraySize = this->rootCluster.ReducedPointsContained;
for(int i = 0; i < this->reducedVerticesArraySize; i++)	printf("After: %f %f %f\n", this->reducedVerticesArray[i][0],this->reducedVerticesArray[i][1],this->reducedVerticesArray[i][2]);
printf("Data Processing Complete\n");
return 1;
}

bool Model::AddVerticeToArray( BNode rootCluster , Point * result, int offset)
{
if ( rootCluster.LeftFils != NULL ) //since a node have both children or no children we only test one.

{
AddVerticeToArray ( *rootCluster.LeftFils, result, offset ); //iterate the same op on the left child

AddVerticeToArray ( *rootCluster.RightFils, result, offset  );//and the right one

}
else // The node is a leaf, we copy the representative point.

{
result[offset][0] = 10 * rootCluster.Reducted[0];
result[offset][1] = 10 * rootCluster.Reducted[1];
result[offset][2] = 10 * rootCluster.Reducted[2];
printf("Before:%f %f %f\n", this->reducedVerticesArray[offset][0], this->reducedVerticesArray[offset][1], this->reducedVerticesArray[offset][2]);
offset += 1;
}
return 1;
}

here is the output of my console:

...
Before: 0.591786 0.657684 0.207727
Before: 0.601944 0.648229 0.187068
Before: 0.568864 0.707011 0.192029
Before: 0.588833 0.686143 0.180473
Before: 0.577500 0.713245 0.165083
Before: 0.587500 0.697243 0.160499
Before: 0.600000 0.680467 0.163887
Before: 0.590000 0.697911 0.168548
Before: 0.596667 0.696961 0.157327
After: 0.596667 0.696961 0.157327
After: 0.000000 0.000000 0.000000
After: 0.000000 0.000000 0.000000
After: 0.000000 0.000000 0.000000
After: 0.000000 0.000000 0.000000
After: 0.000000 0.000000 0.000000
After: 0.000000 0.000000 0.000000
After: 0.000000 0.000000 0.000000
After: 0.000000 0.000000 0.000000
After: 0.000000 0.000000 0.000000
After: 0.000000 0.000000 0.000000
After: 0.000000 0.000000 0.000000
After: 0.000000 0.000000 0.000000
After: 0.000000 0.000000 0.000000
After: 0.000000 0.000000 0.000000
...

[edited by - zifox on June 4, 2004 11:51:54 AM] [edited by - zifox on June 4, 2004 11:53:03 AM]

Share on other sites
Well mate, either I'm getting this totally wrong or you're always writing to the same offset. You are incrementing your offset ... only you're doing it at the wrong point. I mean you do
offset += 1 to modify a local variable and then return without ever using that offset. And your recursive calls to AddVerticeToArray always pass the same offset to the next level of recursion. Am I making sense? I know what I mean.

[edited by - Red Ant on June 4, 2004 12:32:07 PM]

Share on other sites
hum. what to say... i''m an ass. :D

i deserve to be hanged on the public place for that...
being stuck half an hour because i forgot to write a ''&'', i can''t believe that :D

thanks

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 14
• 9
• Forum Statistics

• Total Topics
633350
• Total Posts
3011472
• Who's Online (See full list)

There are no registered users currently online

×