Syntax Question for Tile-Based Engine

Started by
59 comments, last by Nazrix 24 years ago
downloaded it, ran it, found a null-pointer exception. It''s accessing uninitialised memory, in the fscanf if your claims are correct. But to properly debug it, i need the whole project, so I can look at the source and watch the variables


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Advertisement
"I tried using debug. I made it so it just reads in world[0], and when it gets to that line I get an access violation. Then, it goes to where I think the fscanf function may be. It says it''s input.c. It stops at a line in there, and then windows just goes to hell. I get 2 illegal operations and I have to restart the computer each time. I watched world[0], and does not change. It''s initialized with all 3333''s and it stays at that value, but I''m not sure how good of a test it is because of that access violation."

world[0] should not change as a result of the function, since world[0] is a pointer. Only the contents of where it is pointing should change as a result.

However, if you are getting an access violation on the fscanf line it can only mean 1 of two things:
1. fp is not a valid file pointer
2. it is illegal to write to the address contained within world[0]

I assume it is the latter (since you test if fp == null), so do a quick test:

before opening the file, try
strcpy(world[0], " a test string");
if I''m right, then it should crash the same way (under debug). The reason being that it doesn''t want you writing over world[0]''s default data.

How does Andre define the world variable in the second, more complicated example? Is it still the way you are?

Dark Lord Pi
Dark Lord Pi
What I think it''s doing, is overrunning the end of the array in world[0], because fscanf also stores the zero-terminator at the end of the string.


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Lord Pi, I tried adding the line
strcpy(world[0], " a test string");

You were right. It did crash the same way. The only thing I do not understand is that I wrote over top of it other ways. Like, I would say
world[0]="1111111111111111111111111" later on in the program, but perhaps that''s different because I''m setting it equal to something rather than writing over it. Hmmm....I''ll try putting all the source on the ftp site...
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Okay. I put the source on the angelfire ftp the same way as before. The main source code file is called 9.cpp. Lord Pi, I''m not sure what you meant by Andre''s second more complicated example.

Thanks a lot for all your help
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
The reason that
world[0] = "101011111..." works in that you are allowed to
change the address that world[0] is storing, but not the data of a const char *.

Instead of char *world[21];
try char world[21][length+1]; (i''m not sure how long those strings are)

This way world[0] will still point to a char *, but it will let you write over it. (since char *world == char world[], so char *world[] == char world[][])

The disadvantage of this method is that it won''t have any valid default data (to start with). You can make a function that you call in the beginning:

void initWorld() {
strcpy(world[0], "01010101...");
strcpy(world[1], "01010101...");
/
strcpy(world[20], "02020200....");
}

Where the strings above are the initial, default strings.


Dark Lord Pi
Dark Lord Pi
quote:Original post by Nazrix
You were right. It did crash the same way. The only thing I do not understand is that I wrote over top of it other ways. Like, I would say
world[0]="1111111111111111111111111" later on in the program, but perhaps that''s different because I''m setting it equal to something rather than writing over it. Hmmm....I''ll try putting all the source on the ftp site...


You''re right, they are different.

When you do:
char *string;
string="11111";
You are actually moving the pointer to point to memory (probably read only) containing "11111"

Whed you do:
char *string;
strcpy(string, "11111");
You are writing "11111" to the location string currently points to.

You can solve this by allocating memory for the string. (with malloc, new, or statically) However, don''t use:
string="11111";
after allocating memory. Use strcpy or sprintf, instead.

Jesse Chounard

It worked!!!


Thanks to you all for your help. Sorry to have taken so much of your time.


Edited by - Nazrix on 4/20/00 1:49:03 PM
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
You can also change one tile at a time like this:

world[21][32] = "4";

BTW, you really should be doing the initial read from a file. Otherwise, how do you use another map without recompiling the code?
Yes, good idea. I''ll do that.
thanks again
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi

This topic is closed to new replies.

Advertisement