Boost logo

Boost :

Subject: [boost] How to get the size of an C-array?
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2009-10-14 12:53:41


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.

The C++ compiler or the C++ standard library knows this number of elements as it needs to call the destructor of each element when deleting the pointer. Is there something in Boost or a portable way to recover the number of elements of the array, maybe overloading the new [] operator and prefixing the allocate storage with some information? But what information can be stored so the implementation is portable?

Any hint is welcome. Best,
_____________________
Vicente Juan Botet Escribá


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