[Tfug] Need help with a C++ algorithm

Robert Hunter hunter at tfug.org
Thu Mar 15 06:17:07 MST 2007


> Is there a way to determine the size of an allocated block of memory if
> only
> a pointer to it is known?
>
> Example:
>
> int* p_int = new int[some_random_unknown_variable];
>
> Is there a way to determine the size of the memory that was allocated,
> given
> that information?  Yes, I've heard of vector; I don't want to use it (too
> much overhead, inefficient with large numbers of elements, etc.).

Hi, Jude.  I'm assuming you are referring to dynamic allocation. The
obvious solution would be to keep track of the size of each allocation. 
It seems like you would require that information in any case.  If you
only are working with a few objects, then it is a simple a matter of a
few extra variables. Otherwise, you just need a way to map the pointer to
the extra meta-data.  E.g, you could use something such as an associative
data structure, or perhaps you could get fancy and write your own
allocator/wrapper.  This was, in fact, a homework assignment in one of
classes.



>
> Thank you in advance.
> _______________________________________________
> Tucson Free Unix Group - tfug at tfug.org
> Subscription Options:
> http://www.tfug.org/mailman/listinfo/tfug_tfug.org
>


-- 
Rob




More information about the tfug mailing list