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.

DS development > A question about array allocation, new and C vs. C++

#111322 - OOPMan - Tue Dec 05, 2006 6:12 pm

Hey there, something I'm working on looks like, at a future point, it may
require the use of dynamically-sized arrays.

Now, if I recall, this is easy enough to do in C++ as follows:

int *blah = new int[somenumber];

However, I'd like to avoid using C++, in case the compiler decides to try and link in the C++ libs, etc...

So, can I do the above in C using malloc(), or is there some trick to it?

Alternatively, could I just go ahead and use the C++ method without any performance worries since the only actually piece of C++ I need is the new and delete operators in this context.

Or have I gotten C++ completely mixed up with Java with regards to basic array allocation? (For some reason I keep getting this feeling...)
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#111325 - Mighty Max - Tue Dec 05, 2006 6:21 pm

OOPMan wrote:
int *blah = new int[somenumber];


int *blah = (int *)malloc(somenumber * sizeof(int)) ;

Or basically
#define NEW(size,type) (type *)malloc((size) * sizeof(type))

PS: Dont forget to do a free() instead of delete. After allocating via malloc.
_________________
GBAMP Multiboot

#111327 - kusma - Tue Dec 05, 2006 6:23 pm

Code:

*int blah = new int[somenumber];

is practically identical to:
Code:

*int blah = (int *)malloc(sizeof(int) * somenumber);

#111330 - OOPMan - Tue Dec 05, 2006 6:31 pm

Right, okay, thanks for that guys. One more question on this...

If I recall, in C++ you can then access blah using the array method, as opposed to using pointer math...

Will this be possible in C as well?
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#111331 - Lick - Tue Dec 05, 2006 6:35 pm

I think C++ learned that feature (pointer as array access) from C. So yes.
_________________
http://licklick.wordpress.com

#111335 - OOPMan - Tue Dec 05, 2006 6:45 pm

Okay, thanks.

My C is *really* rusty. Bloody universities are all about C++ and Java nowadays, so my C is more than 4 years out of practise :-)
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#111336 - sirpoonga - Tue Dec 05, 2006 6:45 pm

I haven't worked in C or C++ for years. What is the advantage of using a pointer to the array instead of just saying int blah[somenumber];?

#111337 - OOPMan - Tue Dec 05, 2006 6:47 pm

Is there an advantage?

I guess the disadvantage would be that you can fry the memory in an environment that allocates memory in a funny way.

Is this actually a problem on the DS, though?

I mean, there's no MMU, so does memory ever get mapped non-linearly for things like arrays or malloc()s?
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#111345 - kusma - Tue Dec 05, 2006 7:23 pm

sirpoonga wrote:
I haven't worked in C or C++ for years. What is the advantage of using a pointer to the array instead of just saying int blah[somenumber];?


Code:

int blah[somenumber];

is non-standard (at least not supported in c89 - the newest well-supported c-standard).

Edit: non-standard as long as somenumber is a variable and not a constant, that is.

#111348 - OOPMan - Tue Dec 05, 2006 7:36 pm

I thought C99 was considered well-supported now?
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#111354 - sajiimori - Tue Dec 05, 2006 7:52 pm

Use dynamic allocation when it's appropriate. Here are some possible reasons:

- You don't know the size of the array until runtime.

- You don't know how many arrays you want until runtime.

- You don't want to have the allocation take up memory all the time; only when it's needed.

#111365 - OOPMan - Tue Dec 05, 2006 9:58 pm

Mmmmmm, good points to keep in mind saja. Dynamic allocation is all good, as long as you keep a tight handle on the deallocation end of things, since memory leaks are not a good thing :-)

On the plus side, I guess bare-metal DS software doesn't always need to clean up certain allocated resources, since the system is reset when loading new software :-)

EDIT: Now that I think about it, this thread is kind of in the wrong forum. Should have probably been in C/C++. My bad :-(
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#111368 - sajiimori - Tue Dec 05, 2006 10:43 pm

RAII makes memory leaks all but extinct. I used to think garbage collection was the only way dynamic allocation could be safe, but my tune has changed with a better understanding of C++.

#111379 - kusma - Wed Dec 06, 2006 1:29 am

OOPMan wrote:
I thought C99 was considered well-supported now?


Nope, there's really only a few compilers who implement larger portions of C99. Neither msvc nor gcc - arguably the two most commonly used C compilers - support more than just a sub-set of the C99-functionality. And in the embedded world it's even worse.

#111406 - OOPMan - Wed Dec 06, 2006 8:31 am

I guess that's beauracracy for you. Someone makes a standard and it then takes 15 years for it to be implemented widely :-)
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#111426 - masscat - Wed Dec 06, 2006 12:14 pm

There is also:
Code:
void *calloc(size_t nmemb, size_t size);

Which allocates memory for 'nmemb' number of elements of size 'size'.

Some things to consider when using dynamic allocation other than leaking memory.
It is not immediately obvious at build time the memory footprint of the program. On the DS this is important as, unlike a PC, you have rather limited memory resources.
You have to cope with the case where the allocation fails in a pleasant manner. If this is not be possible then you have to stop the cases where memory will become exhausted from happening.
There is an overhead in the call. If you are using it in time critical code think about redesigning your code to move the allocation outside the fast bit.
The memory heap can become fragmented meaning that you cannot allocate sufficient memory in a single continuous block. This depends on the allocation algorithm of the malloc call, the way you arrange your malloc and free calls and the relative lifespans of the allocated memory blocks. If you are doing lots of allocation and deallocation it may be better to allocated a block of memory and then write your own allocation and deallocation functions to operate on this.

In general, on the DS and other similar limited resource systems give more thought to how your use of dynamic allocation will impact the system.

sirpoonga wrote:
What is the advantage of using a pointer to the array instead of just saying int blah[somenumber];?

Some of it is programming style, some people like thinking using pointers.
One nice thing about being able to use pointers as arrays is that it is very easy to lay an array over a block of memory.
Code:
struct some_struct *a_ptr = (struct some_struct *)some_mem_address;
for ( a number of times) {
  /* do something to a_ptr[index] element */
}


EDIT: wrongly assigned the quote to sajiimori, apologies.


Last edited by masscat on Wed Dec 06, 2006 9:50 pm; edited 1 time in total

#111457 - sajiimori - Wed Dec 06, 2006 7:02 pm

masscat, you misquoted me. ;)

Quote:
It is not immediately obvious at build time the memory footprint of the program. On the DS this is important as, unlike a PC, you have rather limited memory resources.
This is important on PC as well, believe me! I'm not sure how the myth got started that you don't need to think about memory on a PC, but it sure seems like a popular one.

Quote:
You have to cope with the case where the allocation fails in a pleasant manner.
All my games assert on the success of memory allocations. I don't know of any games that try to recover.

Quote:
If you are doing lots of allocation and deallocation it may be better to allocated a block of memory and then write your own allocation and deallocation functions to operate on this.
This is only worth doing if you know something that malloc doesn't, and you can take advantage of it to reduce fragmentation or otherwise solve a problem.

Quote:
...on the DS and other similar limited resource systems...
Where do I get one of these implied unlimited resource systems? ;)

About pointers and arrays: It's better to think of arrays as degrading to pointers when you use them. a[i] is *(a+i).

#111474 - sirpoonga - Wed Dec 06, 2006 9:38 pm

Well, you guys certainly answered my question :)
Like I said, it's been a long time since I did C and C++. You guys just jogged my memory on why you'd want an array pointer, dynamic allocation. Thanks.