AFAIK there is no standard way to query the size you passed in to malloc() 
by the returned pointer, even malloc_usable_size is not standard.

malloc_usable_size() may return a larger size, but I definitely wouldn't 
count on that this is always 4 (see here: 
http://man7.org/linux/man-pages/man3/malloc_usable_size.3.html).

Some platforms or allocator libs have an msize() or _msize() function, but 
I haven't found this in the dlmalloc sources.

If you absolutely need to store the size passed to alloc inside a memory 
block then I think you need to implement this on your own (allocate 
additional space for a header, store size in header, and return pointer 
after header). This will be dangerous though if you cannot guarantee that 
all alloc/free calls are properly paired and go through your own wrapper 
functions.

Cheers,
-Floh. 

Am Mittwoch, 5. August 2015 12:29:03 UTC+2 schrieb awt:
>
> Hi,
>
> I need to retrieve the size of a buffer that is dynamically allocated and 
> this is what I did:
>
>
> size_t size = 64;
> size_t alignment = 8;
> memalign(alignment, size);
>
> size_t measuredSize = malloc_usable_size(test);
> std::cout << "measuredSize:" << measuredSize << " size:" << size << 
> std::endl;
>
> However, the size returned by malloc_usable_size is always 4 more than the 
> amount that I pass in to memalign. In this case, measuredSize will be 68. I 
> tried using malloc and the result is the same.
>
> Is there a way to get the size that I pass into memalign or can I always 
> assume that malloc_usable_size will always report a higher size of 4 bytes?
>

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to