PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > size of an array at runtime?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
size of an array at runtime?

Réponse
 
LinkBack Outils de la discussion
Vieux 28/06/2008, 20h26   #1
aaragon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut size of an array at runtime?

Hello everyone,

I was wondering if there is a way to determine the size of an array at
runtime. Let's say I have a class that has one of its constructors
taking an array:

template <class T>
class A {

// constructors
....
A(const T array[]) {
// use array
}
// other fns...
};

So of course I cannot use the sizeof operator because the array can be
created at runtime, so sizeof(array)/sizeof(array[0]) won't work. Now,
operator delete[] can still figure the size of an array, right?
Therefore, there has to be a way to determine the size of it.

Thanks for viewing my post.

aa
  Réponse avec citation
Vieux 28/06/2008, 20h52   #2
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: size of an array at runtime?

aaragon wrote:

> Hello everyone,
>
> I was wondering if there is a way to determine the size of an array at
> runtime. Let's say I have a class that has one of its constructors
> taking an array:
>
> template <class T>
> class A {
>
> // constructors
> ...
> A(const T array[]) {
> // use array
> }
> // other fns...
> };
>
> So of course I cannot use the sizeof operator because the array can be
> created at runtime, so sizeof(array)/sizeof(array[0]) won't work. Now,
> operator delete[] can still figure the size of an array, right?


It only has to if T has a non-trivial destructor. Otherwise, delete[] only
needs to figure out how much memory needs to be released. That only gives
an upper bound for the size. E.g., if you allocate 116 chars, the actual
memory allocated may very well be 128 bytes; and delete only needs to know
that these 128 bytes are now freed.

> Therefore, there has to be a way to determine the size of it.


(a) non sequitur: even assuming that delete [] knows the number of elements
in the array, there does not need to be a language mechanism that allows
you to get at that piece of information. And as far as I can tell, there
happen to not to be such a mechanism.

(b) If you need that information, the easiest way is to use std::vector. If
you really want to use arrays, the only way to get the length is to not
forget it: when you new[] the array, you know the length; just don't throw
away that piece of information.


Best

Kai-Uwe Bux
  Réponse avec citation
Vieux 29/06/2008, 10h18   #3
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: size of an array at runtime?

On Jun 28, 9:52 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> aaragon wrote:
> > I was wondering if there is a way to determine the size of
> > an array at runtime. Let's say I have a class that has one
> > of its constructors taking an array:


> > template <class T>
> > class A {


> > // constructors
> > ...
> > A(const T array[]) {
> > // use array
> > }
> > // other fns...
> > };


> > So of course I cannot use the sizeof operator because the
> > array can be created at runtime, so
> > sizeof(array)/sizeof(array[0]) won't work. Now, operator
> > delete[] can still figure the size of an array, right?


[...]
> > Therefore, there has to be a way to determine the size of it.


[...]
> (b) If you need that information, the easiest way is to use
> std::vector. If you really want to use arrays, the only way to
> get the length is to not forget it: when you new[] the array,
> you know the length; just don't throw away that piece of
> information.


If the arrays are dynamically allocated (as his mention of
delete[] suggests), then he definitely should be using
std::vector. If they're not, of course, something like:

template< typename T >
class A
{
template< size_t N >
Array( T const (&array)[ N ] ) ...
} ;

can also be used.

(More generally, if he wants to support both, it should probably
be:

template< typename ForwardIterator >
Array( ForwardIterator begin, ForwardIterator end ) ;

He can then use the de facto standard begin and end:

template< typename T, size_t N >
T*
begin( T (&array)[ N ] )
{
return array ;
}

template< typename T, size_t N >
T*
end( T (&array)[ N ] )
{
return array + N ;
}

to invoke this constructor with a C style array.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Vieux 29/06/2008, 11h16   #4
Rolf Magnus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: size of an array at runtime?

aaragon wrote:

> Hello everyone,
>
> I was wondering if there is a way to determine the size of an array at
> runtime. Let's say I have a class that has one of its constructors
> taking an array:
>
> template <class T>
> class A {
>
> // constructors
> ...
> A(const T array[]) {
> // use array
> }
> // other fns...


That constructor isn't taking an array, but rather a pointer in disguise.
It's 100% equivalent to:

A(const T* array) {
// use array
}

> };
>
> So of course I cannot use the sizeof operator because the array can be
> created at runtime, so sizeof(array)/sizeof(array[0]) won't work.


That operation will work, but it won't give you the result you want, because
you're doing it on a pointer, not an array. This doesn't have anything to
do with how your array got created.

> Now, operator delete[] can still figure the size of an array, right?
> Therefore, there has to be a way to determine the size of it.


The only way of determining it is by remembering it.

  Réponse avec citation
Vieux 30/06/2008, 05h08   #5
aaragon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: size of an array at runtime?

On Jun 29, 5:16 am, Rolf Magnus <ramag...@t-online.de> wrote:
> aaragon wrote:
> > Hello everyone,

>
> > I was wondering if there is a way to determine the size of an array at
> > runtime. Let's say I have a class that has one of its constructors
> > taking an array:

>
> > template <class T>
> > class A {

>
> > // constructors
> > ...
> > A(const T array[]) {
> > // use array
> > }
> > // other fns...

>
> That constructor isn't taking an array, but rather a pointer in disguise.
> It's 100% equivalent to:
>
> A(const T* array) {
> // use array
> }
>
> > };

>
> > So of course I cannot use the sizeof operator because the array can be
> > created at runtime, so sizeof(array)/sizeof(array[0]) won't work.

>
> That operation will work, but it won't give you the result you want, because
> you're doing it on a pointer, not an array. This doesn't have anything to
> do with how your array got created.
>
> > Now, operator delete[] can still figure the size of an array, right?
> > Therefore, there has to be a way to determine the size of it.

>
> The only way of determining it is by remembering it.


Thank you for your answers...
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h01.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16915 seconds with 13 queries