473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jun 28 '08 #1
4 3913
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
Jun 28 '08 #2
On Jun 28, 9:52 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
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:ja*********@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
Jun 29 '08 #3
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.

Jun 29 '08 #4
On Jun 29, 5:16 am, Rolf Magnus <ramag...@t-online.dewrote:
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...
Jun 30 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
2
by: Christopher Benson-Manica | last post by:
Is the following program conforming under C99? #include <stdio.h> typedef struct foo { int bar; int baz; } foo; foo foos={
1
by: Jeff N | last post by:
It it possible to change a button's or text box's font size during runtime? When I try to build my application with this code: this.ActiveControl.Font.Size = "8.25"; I get this error message:...
1
by: Andrea Gavana | last post by:
Hello NG, sorry if the message is not strictly Python-related, but it is fantastically impossible to send post to Image-SIG. I am using PIL to load and display some pictures (via wxPython) in a...
5
by: sunny | last post by:
Hi All Is there any way to determine stack and heap size, during runtime. i.e can we predict stack overflow. etc
4
by: tamasumalta | last post by:
I am just new to vb.net. I used to program in vb 6. Can you kindly indicate how I can maximise a form window to full in runtime. I think this is a standard thing to design controls. If this is...
5
by: frankleggett | last post by:
Hi Please excuse my ignorance. I am trying to declare an array with a variable amount but I'm having no luck. this is what I've tried Dim Number as string X = Val(Number) dim Array(X) as...
1
by: shofu_au | last post by:
Hi Group, I am trying to define a class that has a fixed size array of a structure containing a fixed size array of a structure. I am using System.Runtime.InteropServices and trying to define...
7
by: kadarkarai | last post by:
I am developing photo gallery in Macromedia Flash 8 and XML to retrive the Image. Image Size is all in different size. my question is How to Change image size at runtime in flash? so that image...
1
by: peachdot | last post by:
hi, how do i create an unlimited array size?
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.