Jump to content
  • Advertisement

Archived

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

Splat

Weird array problems

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Guest Anonymous Poster

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[i].member1 = 0;
myStructs[i].member2 = 0;
}

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.

-- Remnant

Share this post


Link to post
Share on other sites
Advertisement
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).]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!