variable problems

Started by
15 comments, last by Trienco 12 years, 9 months ago
ok I found the problem line of code.

fscanf(input_file, "%d", &map_height); //line 29

The problem is that I don't know whats wrong with it.

If I comment it out the program runs but if I put it back in it stops working?

what do I do now?

Advertisement
Hello!

Have you checked if the file is opened correctly ? (i.e if input_file is not null). If it is, maybe the path you are giving the function is incorrect, thus it cannot find the file. Hope it helps.



You should use error checking, see if the file is opened, see if a read succeeds. You also need to be careful to close the file when you're done. Using the standard C++ file objects make this a little easier. You should signal the calling function if the load failed.

Use of an uninitialised variable should trigger a warning. I recommend you increase your compiler warning level until it does (better still, just set the highest warning leve), then fix all the warnings your code generates. Finally, tell the compiler to treat warnings as errors from now on.
Hey well i fixed my problem. :lol:

It was just an issue which I was modifying memory I was not supposed to. I had to change the following:

terrain1.resize(map_height*map_width);

to

terrain1.resize(map_height*map_width+3);

now I'm trying to work on my collision detection system (which is not going to well)

anyway I thank you guys for your help.


Hey well i fixed my problem. :lol:

It was just an issue which I was modifying memory I was not supposed to. I had to change the following:

terrain1.resize(map_height*map_width);

to

terrain1.resize(map_height*map_width+3);


This is not a correct fix for the problem.

The logical error is that you have 'i++' in your loop before you use 'i' to index. This means that you put the first set of data into element 1, the next into element 2, etc., and skip element 0 completely.

You have map_width * map_height many places to work with, so that should be the storage size you want. I have no idea how you came up with the +3.
Well you see the +3 was because +1 did not work.

Well you see the +3 was because +1 did not work.


His point is that you didn't fix the bug. Your "fix" seems to be based on randomly making arbitrary changes until you don't _see_ the bug anymore. There should be neither a +1 or a +3 and how does preventing a segfault in a hacky way change anything about the actual bug where your code is _still_ reading/writing to the wrong location? You have essentially just made an easy to debug error (hard crash) into a hard to debug error (eventually you will notice at a completely different point in your program that some values aren't what they should be without any clue where it actually happens).

Sorry, but you can't fix a bug without actually understanding where it happens, why it happens and how to prevent it. A segfault or access violation isn't the bug, it's the consequence of a bug.

Is your loadMap still looking like this?

int i;
for(int y=0;y<map_height;y++){
for(int x=0;x<map_width;x++){
i++;
terrain1.x = x;
terrain1.y = y;
fscanf(input_file, "%d", &terrain1.type);
}
}

f@dzhttp://festini.device-zero.de

This topic is closed to new replies.

Advertisement