Array size



 DEVELOP > c-Plus-Plus > Array size

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Luc Claustres"
Date: 05 Dec 2004 03:06:35 PM
Object: Array size
Is it possible to retrieve the number
of elements in a template array
dynamically allocated ?
void allocate()
{
int size = ...
T *pointer = new T[size];
}
void compute(T *pointer)
{
// Here I need the size but I can only access the pointer
}
Thanks.
.

User: "Buster"

Title: Re: Array size 05 Dec 2004 09:52:37 PM
Luc Claustres wrote:

Is it possible to retrieve the number
of elements in a template array
dynamically allocated ?

In general no. Store the size too if you're going to need it.

void allocate()
{
int size = ...
T *pointer = new T[size];

Memory leak.

}

void compute(T *pointer)
{
// Here I need the size but I can only access the pointer

In fact you can't access either.

}

--
Regards,
Buster
.

User: "Virtual"

Title: Re: Array size 06 Dec 2004 04:43:34 AM
Not really, but you could add another template parameter that comes
before anything that's deduced by the compiler. I've assumed function
templates for simplicity.
template<class T>
void allocate()
{
int size = T *pointer = new T[size];
compute<size>(T);
}
template<int U, class T>
void compute(T *pointer)
{
//do whatever you want here
}
Hope that helps.
.
User: "David Harmon"

Title: Re: Array size 06 Dec 2004 08:02:55 PM
On 6 Dec 2004 02:43:34 -0800 in comp.lang.c++, "Virtual"
<justin.busby@gmail.com> wrote,

template<class T>
void allocate()
{
int size = T *pointer = new T[size];

Bogus.

compute<size>(T);

Also bogus, using a variable size for a template parameter.
.


User: "David Harmon"

Title: Re: Array size 05 Dec 2004 08:52:57 PM
On Sun, 5 Dec 2004 22:06:35 +0100 in comp.lang.c++, "Luc Claustres"
<luc.claustres@numericable.fr> wrote,

Is it possible to retrieve the number
of elements in a template array
dynamically allocated ?

void allocate()
{
int size = ...
T *pointer = new T[size];
}

void compute(T *pointer)
{
// Here I need the size but I can only access the pointer
}

Use std::vector, then use the vector's size() member function to get
the size. Avoid roll-your-own substitutes for standard library
classes.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER