Boost logo

Boost :

Subject: Re: [boost] How to get the size of an C-array?
From: Bo Persson (bop_at_[hidden])
Date: 2009-10-15 17:15:19


vicente.botet wrote:
> Hi,
>
> On TBoost.STM, I need to do something before deleting an array on
> each one of the elements of the array, or more precissely with the
> address of each one of the elements.
>
> T* ptr = new T[3];
>
> // in another file, the number of elements to which ptr points is
> unknown.
> // before deleting
> for (size_t i=0; i< 'ptr size'; ++i) {
> // do something with &ptr[i]
> }
> delete [] ptr;
>
> As the number of elemenst is unknow in the separated unit, I have
> no mean to iterate on ptr to get the address of the elements.
>

You just have to remember the size of the allocation.

const ptr_size = 3;
T* ptr = new T[ptr_size];

That's all there is to it!

Bo Persson


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk