Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Weird array problems


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 Splat   Members   -  Reputation: 122

Like
Likes
Like

Posted 31 October 1999 - 04:44 AM

Ok, this has been bugging me for a while and I'm pretty sure it's Visual C++ screwing up but here it goes:

I have a class defined as follows:

class ... {
public:
....
private:
struct ... myStructs[100];
}

Ok, now, I initialize the variables in another function.

void ...::Initialize() {
int i;
for (i = 0; i <= 100; i++) {
myStructs[i].member1 = 0;
myStructs[i].member2 = 0;
}
}

Ok, here's where it really screws up: When the program quits, it tells me "HEAP[GameTest.exe]: Heap block at 008C6F20 modified at 008C7558 past requested size of 630"

Now, I've found 2 ways to fix this: One is to increase the array size by 1, and NEVER touch that last entry. The second is to not touch the "member2" member of the structure. Either one will avoid the heap overflow.

I have encountered variations of this problem before in Visual C++. Even without structures, with simple int arrays.

WTF is going on?!?!?!?! Is Visual C++ not allocating enough space for scoped arrays??

I can avoid it for now with the additional element at the end of the array, but I would REALLY REALLY REALLY like to know why this is happening to me.

- Splat

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


Sponsor:

#2 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 26 October 1999 - 05:57 PM


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


#3 foofightr   Members   -  Reputation: 130

Like
Likes
Like

Posted 27 October 1999 - 11:27 AM

*chuckle*

#4 Joshua Schpok   Members   -  Reputation: 122

Like
Likes
Like

Posted 27 October 1999 - 11:50 AM

BWAHAHAH!

#5 Splat   Members   -  Reputation: 122

Like
Likes
Like

Posted 27 October 1999 - 12:05 PM

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).]


#6 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 27 October 1999 - 01:51 PM

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


#7 Niels   Members   -  Reputation: 122

Like
Likes
Like

Posted 27 October 1999 - 11:31 PM

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


#8 Niels   Members   -  Reputation: 122

Like
Likes
Like

Posted 27 October 1999 - 11:51 PM

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


#9 Splat   Members   -  Reputation: 122

Like
Likes
Like

Posted 28 October 1999 - 12:16 PM

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).]


#10 Niels   Members   -  Reputation: 122

Like
Likes
Like

Posted 29 October 1999 - 04:09 AM

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


#11 Splat   Members   -  Reputation: 122

Like
Likes
Like

Posted 29 October 1999 - 10:05 AM

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


#12 Niels   Members   -  Reputation: 122

Like
Likes
Like

Posted 30 October 1999 - 07:59 AM

Yes, you can.

However, I think that in a few years we'll be in a situation where we'll see bytecode that out performs precompiled code.

The reason is simple: Bytecode can be optimized on-the-fly to fit the exact architecture of the machine it is running on (I.e. Anything from processor, memory size and cache to bus speed and various addon hardware (DSP, GPU etc.) can be considered during optimization - something you can't do in precompilation, unless you want 5000 different executables)

And because of the extremely strict rules that the bytecode adhere to, it is much easier to optimize than e.g. C++.

SUN's hotspot compiler is the first preview of the capabilities of this awesome technology!

/Niels


#13 Splat   Members   -  Reputation: 122

Like
Likes
Like

Posted 30 October 1999 - 09:54 AM

That will certainly be cool. However, my reason for wanting to be able to create Java executables is more for esae of use and install:

I don't want to have to install a Java interpreter and then run my program through the interpreter. I'd rather just be able to click on an EXE file and it will start up.

I'm not sure about the size of the bytecode interpreter, but what might be cool is to in the future attach the interpreter to the bytecode in such a way that basically running the EXE runs the interpreter to load the attached bytecode and run it.

Enough blabbering.

- Splat


#14 Niels   Members   -  Reputation: 122

Like
Likes
Like

Posted 31 October 1999 - 03:59 AM

Since you talk .exe's I assume you are targeting the windows platform - Any windows platform with IE (or NS for that matter) installed WILL also have the Java run-time installed (Unless they went through a lot of trouble to get rid of it ).

If you want to get a better idea of whats possible in pure Java today, check out demos such as "forward" (I think it's by "komplex" or something like that, might be wrong tho')...

Their new stuff is at (haven't seen it yet):

ftp://ftp.scene.org/pub/incoming/ASSEMBLY99/java/

/Niels



#15 Alastair   Members   -  Reputation: 122

Like
Likes
Like

Posted 31 October 1999 - 04:44 AM

This is a reply to an earlier question from Splat. The latest version of the GNU C/C++ compiler (version 2.95) comes with a Java compiler that can generate native code for a lot of platforms, including Windows. And its free! I've looked at the code it generates and its quite good. That was for an ARM processor mind you!

Lastly, anyone interested in fast Java VMs should check out Sun's Hotspot VM. It figures out which part of your code should be compiled and which should be interpreted at runtime.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS