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 > Get memory size

#166821 - hacker013 - Thu Feb 19, 2009 5:23 pm

hey everybody,

How do I get the size of the allocated memory from a pointer? I've tryed sizeof but that doesn't work.

gr,

newbie013
_________________
Website / Blog

Let the nds be with you.

#166822 - elhobbs - Thu Feb 19, 2009 5:42 pm

it depends on the allocator used. you are better off tracking it yourself if you need this information.

#166824 - hacker013 - Thu Feb 19, 2009 6:14 pm

i'm making a memory handling library, it is just using malloc, calloc, realloc and free.
_________________
Website / Blog

Let the nds be with you.

#166828 - a128 - Thu Feb 19, 2009 6:43 pm

Code:
void meminfo() {

#ifdef DEBUG
    struct mallinfo info = mallinfo();

    int byte=(info.usmblks + info.uordblks);
/*
    if (byte<1024)
        iprintf("Memory use: %d bytes\n",byte);
    else
        iprintf("Memory use: %d KB \n",byte/1024);
   */
 iprintf("Memory use: %d bytes\n",byte);

    iprintf("Total heap size: %d KB\n", info.arena/1024);
#endif



}



This code is for showing what mem you have left.

use the search engine...this mem question is posted dozens of times


Last edited by a128 on Fri Feb 20, 2009 9:22 am; edited 1 time in total

#166829 - Miked0801 - Thu Feb 19, 2009 6:43 pm

From where? Which libs alloc functions are you using? There's a good chance that they have their own memory reporters as well.

#166831 - kusma - Thu Feb 19, 2009 7:16 pm

hacker013 wrote:
How do I get the size of the allocated memory from a pointer?

Easy; you don't. This is not something C/C++ allows you to. As someone else pointed out, you must track this information yourself if you need it.

#166844 - hacker013 - Fri Feb 20, 2009 11:34 am

I googled it and there is existing a function for that called: _msize() but is this function supported by devkitARM ? i'm using just de basic library's
_________________
Website / Blog

Let the nds be with you.

#166845 - sverx - Fri Feb 20, 2009 11:47 am

hacker013 wrote:
How do I get the size of the allocated memory from a pointer?


You have to check the size of the type of the memory pointed. So if you have a pointer to a struct, then sizeof(struct).

btw the size of the allocated memory is decided when you malloc it so just keep it somewhere in memory...
_________________
libXM7|NDS programming tutorial (Italiano)|Waimanu DS / GBA|A DS Homebrewer's Diary

#166849 - elhobbs - Fri Feb 20, 2009 4:09 pm

Code:
   The `malloc_usable_size' function takes a pointer to a block
allocated by `malloc'.  It returns the amount of space that is
available in the block.  This may or may not be more than the size
requested from `malloc', due to alignment or minimum size constraints.
not sure if it fully implemented but you could give it a try if you insist on ignoring the advice that people have provided you. keep in mind that this does not correlate to the size of the object that you requested, but rather to the size of the block of memory that malloc provides to you.

Last edited by elhobbs on Fri Feb 20, 2009 4:32 pm; edited 1 time in total

#166850 - kusma - Fri Feb 20, 2009 4:09 pm

hacker013 wrote:
I googled it and there is existing a function for that called: _msize() but is this function supported by devkitARM ? i'm using just de basic library's

_msize is specific to Microsoft's CRT implementation, and can't be used on other platforms than Windows.