Jump to content
  • Advertisement

Archived

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

t2sherm

Arrays - Memory Allocation

This topic is 6634 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

I was having a problem before where a variable was in the same memmory location as another. I had an array like this: int Map[18][25]; but it was supposed to be Map[25][18]. I was writing up to Map[25][18], so i see how it could have written past the end of the array, but I was reading some previous posts here about arrays and i thought one said that the computer allocates memmory for 25*18 = 450 integers either way. But if that is the case how could it be writing past the end? How can flipping the array around change anything? I am have been programming in C++ for a couple years, but I still don''t really know how exactly some stuff works yet. t2sherm ô¿ô

Share this post


Link to post
Share on other sites
Advertisement
each time you increase y, you jump 25 ints pr y.
int map[y][25]. (y*25 + x, where y -> 0-24, x-> 0-17)

the other way around you jump only 18 ints pr y.
int map[y][18]. (y*18 + x, where y -> 0-24, x-> 0-17)

so you will jump way too far in the map[y][25] array.





Edited by - Claus Hansen Ries on June 21, 2000 11:18:23 AM

Share this post


Link to post
Share on other sites
If you've a variable

int Map[18][25]

and you want to access Map[ i ][j], the adress is calculated with

base adress of Map + (25 * i + j)*sizeof(int)

The first adress after the array is &Map[18][0] = &Map[17][25]
= base adress of Map + (25 * 18)*sizeof(int)

so if you want to allocate e.g. element Map[20][0], the adress is

base adress of Map + (25*20)*sizeof(int)

which is past the end of the array.

If you add 1 to the first index, 25*sizeof(int) is added to the adress.
If you would declare Map[25][18], only 18 would be added so you can have higher first indices although both arrays need the same amount of memory.

Visit our homepage: www.rarebyte.de.st

GA

Edited by - ga on June 21, 2000 11:33:21 AM

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!