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.

Coding > problems with new

#18368 - Darmstadium - Thu Mar 25, 2004 12:51 am

when I try to use the new operator, I get a ton of errors when I compile using DevKitAdv. Why doesn't it work?

#18370 - LOst? - Thu Mar 25, 2004 1:22 am

Darmstadium wrote:
when I try to use the new operator, I get a ton of errors when I compile using DevKitAdv. Why doesn't it work?


Maybe you should enable the CPP option in the crt0.s, and maybe use the CPP libs when compiling.

new is a C++ operator only I guess, but I'm not sure.

#18374 - johnny_north - Thu Mar 25, 2004 4:51 am

Ensure these options are part of your linker options:

-lstdc++ -lgcc

You'll get c++ support which includes the new operator

BTW- I'm not sure if you can do this without an appropriate crt0 file. Jeff Frowein's crt0/lnkscript contains code to support c++.

#18379 - NoMis - Thu Mar 25, 2004 8:48 am

or just use g++ instead of gcc, it does the above automatic.

NoMis

#18380 - Lupin - Thu Mar 25, 2004 8:52 am

Why don't just use the malloc function? Or don't use any dynamic memory at all? I think dynamic memory on a gba is not a good idea... i really wonder how the libraries manage the memory allocation since there is not endless ram space and most of the ram is most likely occupied by code and data...
_________________
Team Pokeme
My blog and PM ASM tutorials

#18384 - OptiRoc - Thu Mar 25, 2004 10:06 am

Overloading "new" to operate on a statically allocated list of appropriately sized memory chunks is often a good solution.

#18406 - Sweex - Thu Mar 25, 2004 4:58 pm

Lupin wrote:
Why don't just use the malloc function? Or don't use any dynamic memory at all? I think dynamic memory on a gba is not a good idea... i really wonder how the libraries manage the memory allocation since there is not endless ram space and most of the ram is most likely occupied by code and data...


No dynamic memory allocation? Are you serious? Sure, it is possible for small projects. If it becomes just a little bit bigger than, let's say, a tetris clone then it's requiring some serious effort. And besides, why should the programmer be the memory manager when you can have a computer do it for you? Sure, memory is limited on the GBA and one should be careful, but no dynamic mem allocation...?
_________________
If everything fails, read the manual: If even that fails, post on forum!

#18412 - sajiimori - Thu Mar 25, 2004 7:50 pm

Lupin, you know that 'new' does more than malloc(), right? You can't just substitute malloc().

Also, I wouldn't presume to know how other people arrange their programs. Making everything static can be really inconvenient for some applications.

#18416 - Lupin - Thu Mar 25, 2004 8:18 pm

Well, i was just wondering how the compiler knows where it can actually allocate data?

New does more than malloc()? What does it more? I thought it's both just memory allocation :/
_________________
Team Pokeme
My blog and PM ASM tutorials

#18421 - poslundc - Thu Mar 25, 2004 8:44 pm

Constructors... not to mention overloaded constructors...

Dan (and I don't even use C++).

#18435 - Lupin - Thu Mar 25, 2004 11:19 pm

Well, i don't use C++ too, i try to program as low level as i can (because my C skills are... you know :)), preferably in asm :)
_________________
Team Pokeme
My blog and PM ASM tutorials

#18448 - col - Fri Mar 26, 2004 2:53 pm

Lupin wrote:
Well, i don't use C++ too, i try to program as low level as i can (because my C skills are... you know :)), preferably in asm :)


so you shouldn't have been trying to answer a question about c++, right? ;)

Col

#18456 - LOst? - Fri Mar 26, 2004 8:36 pm

Lupin wrote:
Why don't just use the malloc function? Or don't use any dynamic memory at all? I think dynamic memory on a gba is not a good idea... i really wonder how the libraries manage the memory allocation since there is not endless ram space and most of the ram is most likely occupied by code and data...


I tried to switch to the new operator after using malloc a while, and it worked much better until I needed to use realloc. I didn't know anything in C++ that can realloc the old memory. I tried new instead of realloc but it failed.

new works great with GBA games, but I like to have control of where I put my data, and new is not giving me the choice of optimizing the data space myself.

#18457 - poslundc - Fri Mar 26, 2004 8:43 pm

LOst? wrote:
new works great with GBA games, but I like to have control of where I put my data, and new is not giving me the choice of optimizing the data space myself.


It should be just as easy to configure the new operator as it is to configure malloc.

But if you care so much about optimizing the data space manually, it's probably easier to do without either of these and just manage the memory directly using arrays and pointers. No heap means no fragmentation. I personally don't bother with using either malloc or new (my program still uses dynamic entities like linked lists, though).

Dan.

#18463 - LOst? - Fri Mar 26, 2004 9:03 pm

poslundc wrote:
LOst? wrote:
new works great with GBA games, but I like to have control of where I put my data, and new is not giving me the choice of optimizing the data space myself.


It should be just as easy to configure the new operator as it is to configure malloc.

But if you care so much about optimizing the data space manually, it's probably easier to do without either of these and just manage the memory directly using arrays and pointers. No heap means no fragmentation. I personally don't bother with using either malloc or new (my program still uses dynamic entities like linked lists, though).

Dan.


I want to play your games. Do you have any links? And remember, I'm want games to be perfect synchronized (probably why I'm failing in GBA programming) and most GBA games doesn't give me that feeling.

#18464 - Lupin - Fri Mar 26, 2004 9:10 pm

yah, i also want to know what all the regulars are working on atm... I think that might be some interesting stuff ^^
_________________
Team Pokeme
My blog and PM ASM tutorials

#18467 - LOst? - Fri Mar 26, 2004 9:15 pm

Lupin wrote:
yah, i also want to know what all the regulars are working on atm... I think that might be some interesting stuff ^^

Yea tepples, Miked0801, poslundc, please show us some of your god works! You are so good in all these tech and programming stuff, so how are you in game designing?

#18471 - Miked0801 - Fri Mar 26, 2004 10:09 pm

N.D.A. :(

Go buy Lord of the Rings: Return of the King, Sims: Bustin Out, James Bond: Everything or Nothing, or Digimon Racing. Worked as a technology/support programmer on those. The game I'm lead on currently will be mastering shortly and then I can tell you about it as well.

Or look at the 2 previous GBC releases of Harry Potter or Heroes of Might And Magic where I was lead as well.

No code to show, just good games :)

#18472 - poslundc - Fri Mar 26, 2004 10:58 pm

LOst? wrote:
I want to play your games. Do you have any links? And remember, I'm want games to be perfect synchronized (probably why I'm failing in GBA programming) and most GBA games doesn't give me that feeling.


You want games to be perfectly synchronized? I'm not sure what you mean by this... synchronized to what?

http://www.danposluns.com/gbadev has some of the stuff I've been working on, although it's kind of out of date.

The Mode 7 engine, in particular, now supports random map generation, dynamic sprite z-ordering, arbitrary fades and multiply-fades, a basic event model, map collision detection, and status bars, menus and text windows. Currently working on manually-scaled sprites (to deal with the sprite cycles/line limit problem).

Dan.

#18478 - sajiimori - Sat Mar 27, 2004 5:11 am

If anybody cares, I'm currently working on a very compact Scheme (subset) implementation for GBA, to use as a game logic language for all my current and future projects. (The faster I can make it, the less I have to write in C/asm, so that's good motivation.)

If all goes well, I'll release a whole development suite centered around this implementation. Games that use it will run on GBA and anything that supports SDL.

#18535 - f(DarkAngel) - Sun Mar 28, 2004 5:58 pm

Lupin wrote:
Well, i was just wondering how the compiler knows where it can actually allocate data?

New does more than malloc()? What does it more? I thought it's both just memory allocation :/


It has an allocation table. malloc and new uses different tables, and this is why you can't mix new/delete with malloc/free.

You can check out glibc docs (gnu.org) for info on how malloc works.
_________________
death scream...

#18546 - tepples - Sun Mar 28, 2004 7:52 pm

f(DarkAngel) wrote:
malloc and new uses different tables, and this is why you can't mix new/delete with malloc/free.

Does this mean that one should not mix C code that dynamically allocates memory with C++ code that dynamically allocates memory? Or can you explain a safe way to make new use malloc as a backend or vice versa?

Quote:
You can check out glibc docs (gnu.org) for info on how malloc works.

The newlib docs may prove more helpful. GNU/Linux uses glibc; the publicly available tools for the GBA use Red Hat's newlib.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#18558 - f(DarkAngel) - Sun Mar 28, 2004 10:30 pm

If i recall correctly, there's no problem with using C & C++ allocation in the same program. malloc and new just handles the table differently (i've accidently written that the different thing is table, not table handling), ie, they should be using the same table (there's a chance that both uses seperate tables, but i don't think this can be true because if this was true, malloc and new had to check each other's table. this is first an extra job for cpu, and second malloc doesn't/can't know anything about new in a C program). If this wasn't true, then there would be horrible problems in library usage (especially in real-mode/non-protected-mode system). Using malloc/new & free/delete on the same block has undefined results (same for deallocation an array with delete x instead of delete [] x, vice and versa).

It's strictly not recommended for C++ programming not to use malloc/free because they don't handle the call to the consturctor/destructors. Also malloc requires an explict pointer type-case which is an extra work for programmer.

edit: i've accidently written that the different thing is tables instead of table handling/method. sorry for the confusion...
_________________
death scream...


Last edited by f(DarkAngel) on Sun Mar 28, 2004 10:50 pm; edited 1 time in total