gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Large Arrays (32k)

#25880 - pyros - Mon Aug 30, 2004 7:23 pm

Today, I tried to make a big (32k) array like so:

Code:
   u8 myarray[32768];

However, when I wrote to the array things went bad. I don't think gcc was actually giving it enough memory.


When I defined the contents (i.e.)

Code:
   u8 myarray[32768] = {0,0,0,0.............};


it didn't cause the problem.


To work around this apparent compiler error I did this:

Code:
   u8* myarray;
   myarray = (u8*) malloc(32768);

and then accessed the array as before (e.g.)

Code:
   myarray[n] = x;


I am guessing that the problem was caused by gcc trying to put the array in IWRAM which is only 32k, thus overwriting everything in IWRAM including the program itself.


Does anyone know anything else about this? And am I doing anything bad?


I also thought a 'tips' forum might be nice as the coding forums are all labelled 'questions'. It would be an easy way to share short simple things, although i suspect there would be plenty of criticism for 'poor coding practice' and the like.

Paul.

#25881 - poslundc - Mon Aug 30, 2004 7:35 pm

Global variables are placed into IWRAM, which yes, means an array of that size would deplete all of your IWRAM and leave your program non-functional.

In most devkit configurations, malloc assigns memory out of EWRAM, which you have 256K of, which is why it would work in that implementation. If you don't like using dynamic memory allocation, an alternative is to just grab a u8 pointer to 0x02000000 and use it as your array. (Assuming you aren't already using EWRAM for anything; ie. your game isn't multiboot.)

Dan.

#25891 - sajiimori - Mon Aug 30, 2004 10:05 pm

Regarding the "tips" idea, each of the forums is sort of a database of tips in itself, because there are so many answers in each. In other words, most of the threads start as questions, and turn into answers after a short time.

#25893 - dagamer34 - Tue Aug 31, 2004 12:15 am

Maybe someone who is board could make like a "tips" compilation of previous posts that commonly keep recurring on the board. Like how many times does someone ask how to use the BIOS Vblank swi?
_________________
Little kids and Playstation 2's don't mix. :(

#25894 - sajiimori - Tue Aug 31, 2004 12:25 am

That's what the FAQ is for, but using interrupts is complicated enough that directing people to the tutorials is probably better.

#25895 - dagamer34 - Tue Aug 31, 2004 12:31 am

What ever happened to putting that FAQ on the main page somewhere, like making it a link on the side like the others? That would definitely be more effective to me that telling someone to go look at the FAQ.
_________________
Little kids and Playstation 2's don't mix. :(

#25896 - tepples - Tue Aug 31, 2004 3:14 am

Q: How do I put large (> 16 KB) arrays into a program without it crashing?

Answer now in FAQ.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.