[Tfug] Need help with a C++ algorithm

Jude Nelson judecn at gmail.com
Thu Mar 15 10:07:19 MST 2007


Thanks for all the replies!

What I ended up doing is writing a template class that just stores a pointer
to allocated data and the number of elements it contains, plus a few methods
to access private data and an overloaded [ ] operator to access elements
like in an array.  You're sure glibc's allocator doesn't have a method for
determining the size of an allocated block, given a pointer to it?  I would
be very surprised if it didn't, because then how would free() work?

On 3/15/07, Robert Hunter <hunter at tfug.org> wrote:
>
>
> > 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
>
> _______________________________________________
> Tucson Free Unix Group - tfug at tfug.org
> Subscription Options:
> http://www.tfug.org/mailman/listinfo/tfug_tfug.org
>



More information about the tfug mailing list