Jump to content
  • Advertisement

Archived

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

RegularKid

const IS EVIL!

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

This is sooo strange! I am currently programming for the GameBoy Advance and all the graphics have to be #included into the main game code since you can''t load anything at run time. So, I have a couple of huge tile data files that look like this: .... u16 tileData[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, ....ect }; .... Ok, this was working fine for a while, but then the files grew and the tile data arrays got very large. Then, for some strange reason, the program wouldn''t run anymore. And if I scaled back the tile data sizes, then i could get it to run again. I thought that maybe I had run out of space, but that wasn''t the case. After many hours of headaches, all I had to do was put the keyword "const" in front of the tile data arrays to get the program to run again. Like this: .... const u16 tileData[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, ....ect }; .... So now everything is working fine, but I just wanted to know if anyone knew why this was? I was thinking that maybe the const puts the arrays in different places in memory of something. I don''t know that much about low level stuff, so could someone please explain this weird problem to me? Thanks! ps - I posted this in the general programming forum because I figured that it might be something that is not GBA specific. So don''t yell at me

Share this post


Link to post
Share on other sites
Advertisement
Const is not evil.

When compiling a rom, const makes your game store and access the array in rom, not ram.

If you don''t use const, when your game starts up, it copies all that array to ram (you''ve only got 256k remember) so if the array is big then the GBA will crash.

It''s not const''s fault - it''s yours.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
I'm not sure if this is how it is on the GBA but... when creating "normal" variables, like int var = 0; or
u16 map[] = { 1,2,3,4,5,6,7,8,9,0}; they are created on the stack. The reason your program stoppped working was probably because of stack overflow, since you had so many large arrays of data. There are 2 solutions to combat this. The first one, as you found out, is to use const. This creates the data in your program's data segment instead of creating it on the stack at runtime. Pros: fast and easy, Cons: Increases program size, can't modify the data. The other way is to dynamically allocate memory off of RAM(sometimes called the free store or Heap), using either new or malloc(). Howerver, you can't initialize it using the braces {}. But you can modify it at run time if you need to.
Hope this increases your understanding!

EDIT: damn, siaspete beat me to it, in much shorter words too.
------------------------------
BASIC programmers don't die, they just GOSUB and don't return.

[edited by - GarlandIX on September 14, 2002 6:54:35 PM]

Share this post


Link to post
Share on other sites
By the way, having it const doesn''t make your app bigger. It should in fact be smaller, as it''s missing the code that copies it to ram, and take less ram, since it doesn''t keep a writable copy. The data for the array must be in your rom already if it''s const or not, no?


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
quote:
Original post by siaspete
By the way, having it const doesn''t make your app bigger. It should in fact be smaller, as it''s missing the code that copies it to ram, and take less ram, since it doesn''t keep a writable copy. The data for the array must be in your rom already if it''s const or not, no?


Sounds reasonable.

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!