Advertisement Jump to content
Sign in to follow this  

unrelated code causes issues. memory problem I think.

This topic is 3857 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
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:
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) {
//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)){
printf("totalshit = %d\n",totalshit);
int length = rad*2;
switch (side){
case 0: xcor = xbase-rad;
ycor = ybase-rad+id;
case 1: xcor = xbase-rad+id;
ycor = ybase+rad;
case 2: xcor = xbase+rad;
ycor = ybase+rad-id;
case 3: xcor = xbase+rad-id;
ycor = ybase-rad;
id += 1;
if (id>length){
id = 0;
if (side>3){
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, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!