Advertisement

Madness?

Started by October 04, 2000 04:50 AM
13 comments, last by wise_Guy 24 years, 3 months ago
I think I will go insane soon... I just converted my Level initialisation function to read from a file. That's fine. All my values for all the tiles, the baddies, the powerups, etc all work fine. So why am I going crazy? My functions no longer work. Strange as it seems, where before I could write powerup[_i_]->action(UPDATE); This no longer works, instead it crashes the program. I have changed nothing except I have read the data from a file using fread(powerup[_i_], sizeof(powerup), 1, file); Has anyone else experienced this, and knows what to do? But my problems don't stop there! I think I have found a bug in MSVC. I have two nested if statements, but if I try to put any code inside them the program crashes. Why? I don't know!
        
if (!base_layer[y*width + x])
{
   if (!fringe_layer[y*width + x])
   {

   int x; //any code here crashes unless I comment it out


   }
}
        
My MSVC has been acting wierdly lately (ie. demanding that I include certain LIB files but then complaining when I do include them), so it could just be me, but this is driving me nuts. I have scoured my code and can't find a reason for these peculiarities. Anyone able to enlighten me? Thanks for your responses in advance! EDIT: just tried to fix up the formatting with my [_i_]'s being transformed into italics. Edited by - wise_Guy on 10/4/00 5:22:03 AM
About your two nested loops, you sure your typing it right?
Your missing a couple of ]''s and )''s in there
Try writing it: (!(fring_layer...))
Instead of how you write it, it sometimes only works that way
About the file stuff, your probably not writing/reading the file right, your functions try to read the data you loaded from the file which is corrupted.
You sure you writing/reading the file right?
Show some code
And about:
fread(powerup, sizeof(powerup), 1, file);

Your reading 1 item of the powerup array, but why is the size as the whole powerup array?
Why not like this:

fread(&(powerup), sizeof(powerup), 1, file);<br><br>Post back with more source.<br><br> </i> <br><br>- <a href="http://www.goblineye.com">Goblineye Entertainment</a><br><i>The road to success is always under construction</i>
Goblineye EntertainmentThe road to success is always under construction
Advertisement
Hehe
I was wondering why your using so much italics
Goblineye EntertainmentThe road to success is always under construction
quote: Original post by wise_Guy

fread(powerup[_i_], sizeof(powerup), 1, file);


You should write it like this:

    fread(powerup[_i_], sizeof(powerup[0]), 1, file);[/source]Because sizeof(powerup) returns the size of the pointer, not the size of the first element.<BLOCKQUOTE><font size=1>quote:<hr height=1 noshade>But my problems don''t stop there! I think I have found a bug in MSVC. I have two nested if statements, but if I try to put any code inside them the program crashes. Why? I don''t know![source]        if (!base_layer[y*width + x]){   if (!fringe_layer[y*width + x])   {   int x; //any code here crashes unless I comment it out   }}    


Are you sure you are not overwriting memory when you access the base_layer and fringe_layer?

-Jussi
quote: Original post by Selkrank

You should write it like this:

fread(powerup[_i_], sizeof(powerup[0]), 1, file);

Because sizeof(powerup) returns the size of the pointer, not the size of the first element.

But my problems don't stop there! I think I have found a bug in MSVC. I have two nested if statements, but if I try to put any code inside them the program crashes. Why? I don't know!

-Jussi


Isn't that what I said?

Edited by - Tornado on October 4, 2000 12:24:02 PM
Goblineye EntertainmentThe road to success is always under construction
Don''t know about your second problem, but the first one sure sounds like you''re saving and reading function pointers. It''s fine to do that so you can just save structs instead of individual elements, but you''ve got to make sure action points to a valid function before you call it.
Advertisement
quote: Original post by Tornado

Isn''t that what I said?


Yes, but if you look at the times the messages were posted, you''ll see that we were typing our answers at the same time.

-Jussi
Okay, well I guess I am the winner for the coveted "stupidest question" award. The two nested loops did point to invalid data because the pointers were uninitialised.

[begin excuse]
I was thrown off the scent because of the compilers optimization features. Because there was nothing inside the if''s the compiler didn''t even add them to the binary, so nothing went wrong. When I uncommented everything out, the code crashed. What was I supposed to think *wise_guy puts on best newby smile he can muster*
[end excuse]

quote: posted by Ampere

Don''t know about your second problem, but the first one sure sounds like you''re saving and reading function pointers. It''s fine to do that so you can just save structs instead of individual elements, but you''ve got to make sure action points to a valid function before you call it.


Okay the function in question is inherited from the parent class. It *was* a virtual function but it gets overridden in the definition of class powerup : public object

I''m not sure how to assign a pointer to this function (or even if I need to). I know how to assign pointers to functions normally, but here i don''t see how I would do it.

Anyway, as for some code:

This works:

    all_powerups = new object_pointer[num_powerups];for (j=0 ; j<num_powerups ; j++){   all_powerups[j] = new powerup;   all_powerups[j]->state = 0;   all_powerups[j]->sprite = power_sprites;   all_powerups[j]->shot_counter = 0;}[/source]But this doesn''t[source]all_powerups = new object_pointer[num_powerups];for (j=0 ; j<num_powerups ; j++){   all_powerups[j] = new powerup;   fread(all_powerups[j], sizeof(powerup), 1, file);   all_powerups[j]->sprite = power_sprites;}    


I know that the level file is saved correctly because all the member variables are restored to their correct value when i fread() them. Therefore I have little clue why things aren''t working.

Thanks for all the help so far guys!

wise_guy

p.s. why is the formatting of this topic slightly screwed?


The formatting is screwed ''coz selkrank typed a big-long-line-with-no-carrage-returns in a source block. (it''s about halfway down).

It happens though because the messageboard software can''t handle mutiple source blocks in a single msg.

It finds the first source tag, and the last /source tag and everything between them is stuck in the code window thing.

I just stick the largest bit of code in the source block and don''t bother using source blocks with the rest.



Waassaap!!
Waassaap!!
Selkrank: Oh, right, oops...
wise_Guy: I think your not reading the file right, try this:

    all_powerups = new object_pointer[num_powerups];for (j=0 ; j<num_powerups ; j++){all_powerups[j] = new powerup;// I think your fread() here is wrongfread(all_powerups[j], sizeof(powerup), 1, file);all_powerups[j]->sprite = power_sprites;}    


What''s all_powerups? A double pointer? A single pointer?
If It''s a double pointer you need to allocate it like this:

all_powerups = new object_pointer*[num_powerups];

What is it exactly?
Sorry if I missed something in your post, I feel a little fuzzy right now
Goblineye EntertainmentThe road to success is always under construction

This topic is closed to new replies.

Advertisement