Sign in to follow this  
Halty

Static Memory Problem

Recommended Posts

Halty    122
Ok, I have a bit of experiance in Visual Basic and imigrated to C++. I read Sams learn C++ in 21 days cover to cover, although I didn't do the examples so I don't remember all of it. Im also dyspraxic and don't have Word installed so I can't run a spell checker. I appologise for any spelling mistakes. Anyway, my problem. I'm writing a game with OpenGL using GLFW when I execute this code:
int H;
int I;
for (H = 0; H <= 9; H++)
{
	for (I = 0; I <= 9; I++)
	{
		CameraCubeDistance[H][I] = sqrt(((H*2 + (H+1)*0.25 - 10.375) + CameraX)*
			((H*2 + (H+1)*0.25 - 10.375) + CameraX) + ((I*2 + (I+1)*0.25 - 10.375) + CameraZ)*
			((I*2 + (I+1)*0.25 - 10.375) + CameraZ) + CameraY*CameraY);
	}
}




It stops displaying the textures. It also doesn't work as it should, doubling the value when it gets to [9][2], I worked out why it doesn't work properly with:
cout << &CameraCubeDistance[9][1] << " " << &CameraX << endl;




The memory addresses for each are the same, and I don't have a clue why. They're both declered globally.
double CameraX;
double CameraY;
double CameraZ;
double CameraCubeDistance[9][9];




I've tried to look it up but can't find anything. I was pretty sure C++ was supposed to do this for you. Thanks. -Halty

Share this post


Link to post
Share on other sites
JoshM    128
Your problem is that you've declared this:

double array[9];

Which means that valid indices are 0 through 8. So if you do this:

&array[9]

You actually get the address of the *tenth* element of the array. Which is, of course, invalid. The compiler is telling you what the address of the tenth entry would be IF the array were that large.

For example, if you do "&array[1234567]" the compiler will give you an address. It won't be a useful address, but you will get one anyway.

Make sense?

Share this post


Link to post
Share on other sites
Halty    122
I think I get it. Thanks. Expanding the array to [10][10] fixed everything up fine. I feel a little silly now.

Thanks again, it was driving me nuts.
-Halty

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by Halty
I think I get it. Thanks. Expanding the array to [10][10] fixed everything up fine. I feel a little silly now.

Thanks again, it was driving me nuts.
-Halty


This is probably not the solution you want. What you want to do is make sure you're using the "0th" element as well. Sometimes this will require a bit of correction on array indices, especially if you're getting them from user input. However, in the long run - trust me as well as many other experienced programmers here on this - it will make your math simpler, and avoid having teammates curse you out for your n00bishness in the future :) Don't fight 0-based array indexing; use it as it was intended to be used.

Share this post


Link to post
Share on other sites
Halty    122
I guess I should explain my folly. I wanted an array of ten elements, but messed up thinking that because zero was counted I'd have ten if I put [9] in(you know like you would in VB - "0 to 9"). So I wanted valid indices 0 to 9. Maybe you didn't see the loop (I wouldn't blame you) but it does go through 0. It just goes one above what the array should.

Thankyou for the advice anyway.
-Halty

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this