#### Archived

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

This topic is 6279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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
You sure you writing/reading the file right?
Show some code

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

Post back with more source.

- Goblineye Entertainment
The road to success is always under construction

##### Share on other sites
Hehe
I was wondering why your using so much italics

##### Share on other sites
quote:
Original post by wise_Guy

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.
quote:
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 on other sites
quote:
Original post by Selkrank

You should write it like this:

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 on other sites
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 on other sites
quote:

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 on other sites
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 ; jstate = 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 ; jsprite = 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 on other sites
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 on other sites
Selkrank: Oh, right, oops...

  all_powerups = new object_pointer[num_powerups];for (j=0 ; jsprite = 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 on other sites
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 on other sites
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 ; jsprite = power_sprites;}

Let''s say your first function is:

int SomeFunc(int x); // for example

int (*SomeFuncPointer)(int x);

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

SomeFuncPointer = SomeFunc;

That''s what you wanted?

##### Share on other sites
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 on other sites
Why not write two function that read and writes out the data in the class Powerup or whatever, and call them inside the loop. That should work, cos I do it all the time. No problems!

------------------------------
BCB DX Library - RAD C++ Game development for BCB

##### Share on other sites
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

• ### Forum Statistics

• Total Topics
628714
• Total Posts
2984349

• 23
• 11
• 10
• 13
• 14