🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Weird array problems

Started by
13 comments, last by Splat 24 years, 8 months ago

well, you just ran into the #1 classic C error that alot of people make. Basically, you're indexing past the end of your array. Consider this :

you told VC++ to make 100 instances of your structure. Ok, it did that.

Now look at your for loop. You count from 0 to 100, inclusive. Well, if you count from 1 to 100, you get, 100. But counting from **0**, you get 101. (try counting to 10 on your fingers starting at 1 and 0 to verify this.) So you're indexing 1 past the end of your allocated space.

To fix this, change your loop to :

for (i = 0; i < 100; i++) {
myStructs.member1 = 0;<BR> myStructs.member2 = 0;<BR>}<P>basically, just remember : when starting at 0 you always want to have the stop condition be 1 less than the number of members you have allocated.<P>– Remnant

Advertisement
*chuckle*
BWAHAHAH!
My God! Don't I feel foolish. I never was able to replicate this problem consistantly, so I always though it was the compiler. I guess another variable was allocated right at the end of my array so sometimes that last entry would go into the other variable. It's amazing: I ALWAYS though that int array[10] meant the upper bound was 10 and the lower bound was 0! All I can say is - Silly me!

I've never had anything in the form of instruction on ANY programming language, and I originally (8 years ago) became proficient in Visual Basic, where the number of entries was ALSO the upper bound!

Oh yeah, you can stop chuckling now hehe

- Splat

[This message has been edited by Splat (edited October 27, 1999).]

hehe. S'ok, almost everyone has a similar blunder to tell about. Just be careful next time before you claim that the compiler is messing up.

-- Remnant

If you've been polluted by VB, I can easily understand your confusion ... VB has the most perculiar implementation of arrays that I've ever seen.

/Niels

<b>/NJ</b>
Oh, about blunders, dig this:

In my game, I use an event system to tie the game code to the various (sound/graphics/network) engines. This works by having a base C++ eventlistener class with a virtual method that is implemented in the various subclasses that needs to know about the event (similar to the way it works in Java).

Very neat.

However, all of a sudden I started getting these very strange null pointer exceptions in my event handling - apparently the compiler failed to overload the virtual method in the sub classes. Or not.

The problem as it turned out, was a small macro I had created to avoid uninitialized data in my classes:

#define init(class) { ZeroMemory(this,sizeof(class)); }

Guess what, virtul methods in C++ are really invisible member variables, clearing the class also clears the virtual functions.. Doh!

/Niels

<b>/NJ</b>
That's really interesting! I guess C++ stores the virtual function table IN the class data. There might be interesting uses of that, like manually overloading a function in runtime.

Oh yeah. Niels: Yeah, I haven't programmed in VB for some time now - It's nice for real quick Windows application development, but C++ can be and most of the time is SO much better than everything I've ever programmed in

- Splat

[This message has been edited by Splat (edited October 28, 1999).]

Before you get too addicted to C++, check out Java - it's a much much better language. It's not quite as fast, but it IS getting there (and it IS WAY BEYOND VB)...

(I have a feeling it would be ultimate for writing game code, I.e. do the engine in C++/asm whatever, put it in a DLL, and then use Java to glue it all together)...

/Niels

<b>/NJ</b>
I had a relatively bad experience with Java. I used it for a while, but it was slow (before JIT compilers) so I stopped. Now, I think that when I'm done my game engine in might just write games in Java. One question, off topic: Can you compile a Java application into platform-specific executables? Kinda like funneling off the JIT compiler output into an EXE file? That would be great, and remove my last qualm about programming a real application in Java.

- Splat

This topic is closed to new replies.

Advertisement