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...
|