Sign in to follow this  
PaoloStoppa

memory allocation - DS

Recommended Posts

PaoloStoppa    122
Hello Guys/Ladies ! i would like to allocate memory (heap) dynamically at Ninetendo DS, but I cannot do it... For example i would like to do something, like : int a; int *pa; a=5; pa=&a; but I fails... Later I would like this technics to extent to objects. Neither malloc, nor new.... nothing works (((( can anyone help me ? Cheers, Paolo

Share this post


Link to post
Share on other sites
PaoloStoppa    122
sorry, perhaps I did not write my problem well. I emphasize, "dynamically". So something like with "new" command memory allocation. malloc is also welcomed...

Share this post


Link to post
Share on other sites
Monder    993
What exactly doesn't work about your example? You're not allocating memory dynamically but that should still work (unless of course this is in a function body and you return that pointer).

Share this post


Link to post
Share on other sites
PaoloStoppa    122
Quote:
Original post by Monder
What exactly doesn't work about your example? You're not allocating memory dynamically but that should still work (unless of course this is in a function body and you return that pointer).


Exactly I am included in a group, whose intention is to write a game, like pacman. So there are monsters against the pac-man like figure. And I have created some basic class, to contain some behaviour of these monsters. There are a maximum of 8 number of monsters, but they do not appear all immediately at the beginning of the game, but after certain times. So at first there is no monster on the screen, then 1 monster appear, then two, etc. And sometimes not all the monsters appear, 8 is only a theoretically maximum number. Therefore I need dynamic memory allocation. This monster class - I was wondering - could look, like this :

class CMonster
{
private :
int monsterindex; // 1..8 - monster number in the rank
long posx,posy; // screen position of the center of the monster
int lastdir;


public :
BOOL active; // if false, not on the screen. true - on the screen
void EatEnvironment();
enum kind
{
NOBBIN,//basic monster, it can kill you, but cant eat wall
HOBBIN //it can eat everything(wall in the labirint as well)
};
int GetPosX();
int GetPosY();
CMonster();
void Vibrating(int posx,int posy);
void Draw();
void Generate(); // generate nobbin
void Move();
void Kill(); // player kills a monster
void Die(); // monster kill a player
BOOL GetActive();
BOOL Detect(long posx, long posy,long objx,long objy);
int timetoappear;

};

So I would like to create these monsters dynamically....
Is it a good idea ?

Paolo



Share this post


Link to post
Share on other sites
ZMaster    240
You need to use the SDKs own functions to allocate memory on the HEAP. See the Nitro SDK function reference for more information (subsection memory). You can then write your own custom allocator to use the new and delete operators.

You can also go to www.warioworld.com and register for the newsgroup. Post there to retrieve more information, because things are covered by an NDA, so you won't get any more information from here...

Share this post


Link to post
Share on other sites
PaoloStoppa    122
Quote:
Original post by ZMaster
You need to use the SDKs own functions to allocate memory on the HEAP. See the Nitro SDK function reference for more information (subsection memory). You can then write your own custom allocator to use the new and delete operators.

You can also go to www.warioworld.com and register for the newsgroup. Post there to retrieve more information, because things are covered by an NDA, so you won't get any more information from here...


I have registered to the mentioned website. I have seen some example programm, but I cannot imagine, that I need big .h files, in order to apply dynamic memory allocation. It should be very simple, as it is often required in programming.

Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by PaoloStoppa
I have registered to the mentioned website. I have seen some example programm, but I cannot imagine, that I need big .h files, in order to apply dynamic memory allocation. It should be very simple, as it is often required in programming.
You'll need "big .h files" to do dynamic allocation on PCs too. Except the code is in libraries.
For the DS, you just need to use the OS memory allocation call to allocate from the heap (It should be obvious what the function is, but I'm not able to post the name of it here for obvious reasons). That's sort of your malloc() function.

Share this post


Link to post
Share on other sites
nagromo    676
In c, you need <malloc.h> to get memory allocation. It's the same on the GBA.

A dynamic memory allocator is a fairly complicated thing. It has to keep track of which parts of memory are used and which aren't. It's not a free operation, either, although it should be pretty fast.

You need to include the right header files and use the right (probably non-standard) functions to allocate memory on the DS.

If you don't believe us, that's your choice, but don't expect us to help if you can't/won't take our advice.

Share this post


Link to post
Share on other sites
Palidine    1315
Quote:
Original post by PaoloStoppa
It should be very simple, as it is often required in programming.


Nothing is simple when programming for consoles. non-PC hardware is _very_ fussy about how and when things are done.

Quote:
Original post by nagromo
It's not a free operation, either, although it should be pretty fast.


Memory allocation is often one of the slowest operations you can do. Certainly it appears "fast" for things with long lifetimes, but allocating memory will often be a bottleneck for things like particle engines or projectiles. That's why game programmers spend so much time writing memory managers that do only a single malloc at the beginning of the application and then just pass pointers to people asking for memory.

-me

Share this post


Link to post
Share on other sites
PaoloStoppa    122
Quote:
Original post by nagromo
In c, you need <malloc.h> to get memory allocation. It's the same on the GBA.

A dynamic memory allocator is a fairly complicated thing. It has to keep track of which parts of memory are used and which aren't. It's not a free operation, either, although it should be pretty fast.

You need to include the right header files and use the right (probably non-standard) functions to allocate memory on the DS.

If you don't believe us, that's your choice, but don't expect us to help if you can't/won't take our advice.



Okey, I believe in people here. I read something like this in a code :

CMonster* pmonster. //see above, what is monster
OS_Init();

u32 arenaLow = ROUND_UP (OS_GetMainArenaLo(), 16);
u32 arenaHigh = ROUND_DOWN(OS_GetMainArenaHi(), 16);
u32 heapSize = arenaHigh - arenaLow;
void* heapMemory = OS_AllocFromMainArenaLo(heapSize, 16);
//int* pa;

gHeapHandle = NNS_FndCreateExpHeap(heapMemory, heapSize);
SDK_ASSERT( gHeapHandle != NNS_FND_HEAP_INVALID_HANDLE );

this works. Just after it I wanted to write

pFruitManager= new (CFruitManager);

but I received error message :

Panic:OS_AllocFromHeap(): heap not initialized. i received this message, when the breakpointer reached this line during debugging.


Share this post


Link to post
Share on other sites
PaoloStoppa    122
Quote:
Original post by Palidine
Quote:
Original post by PaoloStoppa
It should be very simple, as it is often required in programming.


Nothing is simple when programming for consoles. non-PC hardware is _very_ fussy about how and when things are done.

Quote:
Original post by nagromo
It's not a free operation, either, although it should be pretty fast.


Memory allocation is often one of the slowest operations you can do. Certainly it appears "fast" for things with long lifetimes, but allocating memory will often be a bottleneck for things like particle engines or projectiles. That's why game programmers spend so much time writing memory managers that do only a single malloc at the beginning of the application and then just pass pointers to people asking for memory.

-me


Okey, i did not know, that this is such a difficult thing at game programming. Perhaps, it is quicker, if I seek for an existing memory allocation code, and then I use that ?

Share this post


Link to post
Share on other sites
PaoloStoppa    122
Quote:
Original post by PaoloStoppa
Quote:
Original post by Palidine
Quote:
Original post by PaoloStoppa
It should be very simple, as it is often required in programming.


Nothing is simple when programming for consoles. non-PC hardware is _very_ fussy about how and when things are done.

Quote:
Original post by nagromo
It's not a free operation, either, although it should be pretty fast.


Memory allocation is often one of the slowest operations you can do. Certainly it appears "fast" for things with long lifetimes, but allocating memory will often be a bottleneck for things like particle engines or projectiles. That's why game programmers spend so much time writing memory managers that do only a single malloc at the beginning of the application and then just pass pointers to people asking for memory.

-me


Okey, i did not know, that this is such a difficult thing at game programming. Perhaps, it is quicker, if I seek for an existing memory allocation code, and then I use that ?


Okey, i now use an existing allocation code. I run the programm, and at

pmonster=new CMonster, I go an ensata error message :

Stopped due to abort or undefined instruction in ARM9.

Whz is this error ?



Paolo

Share this post


Link to post
Share on other sites
ZMaster    240
Quote:
Original post by PaoloStoppa
Okey, i now use an existing allocation code. I run the programm, and at

pmonster=new CMonster, I go an ensata error message :

Stopped due to abort or undefined instruction in ARM9.

Whz is this error ?



Paolo


Unfortunately, this is the only error message of ensata that I've ever run into and it can mean everything that has to do with memory: eg. Double freeing memory blocks, writing to / reading from freed memory, writing to / reading from memory that has not been allocated, writing / reading out of array bounds, etc.

Regarding your the error message you posted earlier, you probably did not initialize the heap arena at the correct point in your program. Apart from the main application entry point you do have a start-up function, where memory initializations go! I won't post any more details, because of the NDAs I wrote about earlier!

By the way, you shouldn't post any Nitro SDK specific code on this board! Nintendo will get to your balls, if they hear about that. You are violating contracts. However, I do appreciate that you hold the opinion of people on this board in such high esteem, but the official newsgroup is read (and replied to) by many Nintendo officials and people from other, well experienced, development teams with Nitro licenses, you would have probably had a solution to your problem in fewer time, too!

Cheers!

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