Archived

This topic is now archived and is closed to further replies.

wise_Guy

Madness?

Recommended Posts

wise_Guy    122
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

Share this post


Link to post
Share on other sites
Tornado    122
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[i]), sizeof(powerup[i]), 1, file);

Post back with more source.



- Goblineye Entertainment
The road to success is always under construction

Share this post


Link to post
Share on other sites
Knarkles    271
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

Share this post


Link to post
Share on other sites
Tornado    122
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

Share this post


Link to post
Share on other sites
Ampere    122
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.

Share this post


Link to post
Share on other sites
Knarkles    271
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

Share this post


Link to post
Share on other sites
wise_Guy    122
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?


Share this post


Link to post
Share on other sites
mr_jrt    134
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!!

Share this post


Link to post
Share on other sites
Tornado    122
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 wrong
fread(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

Share this post


Link to post
Share on other sites
wise_Guy    122
Okay, all_powerups is a double pointer.

object **all_powerups;

but I have typedef 'd object_pointer to be a pointer to an object, so isn't that the same as what you suggested?

Anyway, i'm still wondering how a member function is attached to the class... it shouldn't be saved in each one should it? because all instances of powerup use the same function...

It was suggested that I need to assign a pointer to this function but is this necessery and how do I do it?




Edited by - wise_guy on October 5, 2000 9:33:44 PM

Share this post


Link to post
Share on other sites
Tornado    122
quote:

Okay, all_powerups is a double pointer.

object **all_powerups;

but I have typedef ''d object_pointer to be a pointer to an object, so isn''t that the same as what you suggested?

Anyway, i''m still wondering how a member function is attached to the class... it shouldn''t be saved in each one should it? because all instances of powerup use the same function...

It was suggested that I need to assign a pointer to this function but is this necessery and how do I do it?



As far as I know, member functions aren''t saved in each object, only member variables are (Of course).
Try if this works for you:

    
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;

}


About your second question, you want to make a function pointer?
Let''s say your first function is:

int SomeFunc(int x); // for example

Your function pointer will be:

int (*SomeFuncPointer)(int x);

Now if you want SomeFuncPointer to point at SomeFunc you simply do:

SomeFuncPointer = SomeFunc;

That''s what you wanted?

Share this post


Link to post
Share on other sites
wise_Guy    122
Okay, I know that I don''t need to do this...

(1) all_powerups = new object*[num_powerups];
or
(2) all_powerups = new object_pointer*[num_powerups];

Because I tried that and (1) gives me the same error that I
normally get (because it is the same as using a typedef), and
(2) gives a compiler error because in effect it is the same
as writing

all_powerups = new object**[num_powerups];

Seeing as all the member values get loaded correctly, I don''t
think that there are errors in the initialisation code. Could
I be writing over the VTABLE? Actually that could be it! but how
to fix it.... don''t tell me I need to save every variable
seperately.... no there has to be another way...

wise_guy

Share this post


Link to post
Share on other sites
wise_Guy    122
Okay, c++freak, so you mean write out a function to save each
variable to the file, one by one...

I suppose I can do that.......

...but I wish there was another way...

wise_guy

Share this post


Link to post
Share on other sites