Jump to content
  • Advertisement
Sign in to follow this  
steenreem

unrelated code causes issues. memory problem I think.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have defined the color black like this: class Color { public: float r,g,b; Color (float argr, float argg, float argb) { r = argr; b = argb; g = argg; } }; Color black = Color(0.0,0.0,0.0); and somewhere else in my code I have this: "printf("black = %f %f %f\n",black.r,black.g,black.b);" but printf does not display the color black. It displays (0.0,0.17,0.0); which is some ugly green. No where in my code do I have something like "black = ". I'm pretty sure there is some collision of memory. Like 2 variables that use the same memory space. I also think that a 2dimensional array is involved. The array is = "galaxygridtile galaxygrid[10][10];" where: " class galaxygridtile { public: int race; double chance; };" Once I fill up the galaxygrid with values, the color black changes from black to ugly green. How can such collisions occur? And how can I remove them?

Share this post


Link to post
Share on other sites
Advertisement
Posting more code might help.

Also, if black is meant to be a constant, you should declare it as such:

const Color black(0.0f, 0.0f, 0.0f);

Then you won't have to worry about whether or not you have statements like "black = ...". The compiler will tell you if you do.

Share this post


Link to post
Share on other sites
Okay more code =)

This is the line causing the bug. If I disable it then no bug occurs:
fillgalaxygridforrace(1,20);
After this line is run the color black changes from real black to ugly green.

The only thing fillgalaxygridforrace really does it loop a lot of times; create a lot of local variables; and set the values of galaxygrid.
This is the function:
void fillgalaxygridforrace(int race,int maxcount) {
int count = 0;
int xcor;
int ycor;

while (count < maxcount) {
//printf("loopingwhile\n");
//xcor = getRandomCor();
//ycor = getRandomCor();
xcor = 0;
ycor = 0;
if (race==2){
xcor = 10-xcor;
}
else if (race==3){
ycor = 10-ycor;
}

///*
if (galaxygrid[xcor][ycor].race != 0){
int side = 0, id = 0, rad = 1;
int xbase = xcor, ybase = ycor;
while (((xcor<10) && (ycor<10))&&(galaxygrid[xcor][ycor].race != 0)){
totalshit+=1;
printf("totalshit = %d\n",totalshit);
int length = rad*2;
switch (side){
case 0: xcor = xbase-rad;
ycor = ybase-rad+id;
break;
case 1: xcor = xbase-rad+id;
ycor = ybase+rad;
break;
case 2: xcor = xbase+rad;
ycor = ybase+rad-id;
break;
case 3: xcor = xbase+rad-id;
ycor = ybase-rad;
break;
}
id += 1;
if (id>length){
id = 0;
side+=1;
if (side>3){
side=0;
rad+=1;
}
}
}
}
//*/
galaxygrid[xcor][ycor].race = race;
//double chance = max(0.0,1.0-sqrt((sqr(xcor/4.5-1.0) + sqr(ycor/4.5-1.0))/2.0));
//galaxygrid[xcor][ycor].chance = chance;
galaxygrid[xcor][ycor].chance = 0.5;
count = count+1;
}
}

Another note: the program also has crashes when running fillgalaxygrid.
I get "Process terminated with status -1073741819 (0 minutes, 22 seconds)"

Share this post


Link to post
Share on other sites
Well the only thing I can suggest is to use the debugger to step through the program and see when exactly the value of black changes. Also you should declare it as const if you haven't already.

Share this post


Link to post
Share on other sites
Couple of other things:

In your while loop you check for validity of xcor and ycor, so there's a good change, xcor and ycor are out of bounds after the loop. But yet, you use those values again, without checking.

I would also recommend to define a constant for the size of the grid (10 at the moment). Otherwise it'll be a pain to change the size of the grid later.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!