Public Group

# Static Memory Problem

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

## Recommended Posts

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] = 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 on other sites
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 on other sites
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 on other sites
Quote:
 Original post by HaltyI 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 on other sites
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.

-Halty

1. 1
Rutin
68
2. 2
3. 3
4. 4
5. 5

• 11
• 11
• 21
• 10
• 33
• ### Forum Statistics

• Total Topics
633438
• Total Posts
3011882
• ### Who's Online (See full list)

There are no registered users currently online

×