Size of an array.

Dec 18, 2009 at 7:23am
I feel a little stupid posting this, but on a lot of forums / sites and what not I've seen it's not possible to get the size of a newed array. So I kept on reading a few forums the other night and saw no one had an answer to get the size for a newed int, double, float and long array etc.

Reason I feel stupid posting this is - are they right in you cannot obtain the size of those arrays? Anyway I whipped together a small program that was able to grab the size of these. And from what I can tell it returns the correct size. But it's probably easy for this to break, crash and burn as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <typeinfo>

template<typename type>
int GetSize(type* tArray)
{
	int element = 0;
	while(!tArray[element])
		element++;

	return ( (sizeof(type) * element) / sizeof(type) );
}

template<typename type>
void SizeOfType( type* tArray )
{
	std::cout << "Size of [" << typeid(tArray).name() << "] \t= " << GetSize(tArray) << std::endl;
}

int main()
{
	int* intArray = new int[10];
	for( int i = 0; i < 10; i++ )
		intArray[i] = 0;

	SizeOfType(intArray);
	
	double* doubleArray = new double[20];
	for( int i = 0; i < 20; i++ )
		doubleArray[i] = 0.0;

	SizeOfType(doubleArray);

	float* floatArray = new float[30];
	for( int i = 0; i < 30; i++ )
		floatArray[i] = 0.0f;

	SizeOfType(floatArray);

	long* longArray = new long[40];
	for( int i = 0; i < 40; i++ )
		longArray[i] = 0;

	SizeOfType(longArray);

	return 0;
}
Dec 18, 2009 at 7:50am
To be honest with you I dont see a problem with the code or the logic behind it. I think it is more of an issue of good practice to explicitly know the size( i think). Other than that though I dont see this code getting broken.

What gets me though is line 8 and 9

what exactly does that do. with the "!" operator

i would figure that would force it passed the bounds, but what does a statement like that return.
Dec 18, 2009 at 7:55am
Well it's what confuses me in this case as well. As it'd be the same as tArray[element] == NULL Which in my mind shouldn't be correct?
Dec 18, 2009 at 8:01am
Lol yea. That is wierd. I am so freaking confused by that statement. if !tarray[element] ... increment. what the hell does tarray[element] do??
Dec 18, 2009 at 8:04am
If im not mistaken for the loops to work it has to get a value of true and when the statement is false it fails the test. I wonder if it is because what you set the array too in main??? Because is 1== true and 0 == false ?? This might be the case though. You see what im saying??
Dec 18, 2009 at 8:09am
Haha, how could I have missed that. Without even trying it I can see you're right :D
Dec 18, 2009 at 8:15am
lol cool. I dont know how the hell i figured it out but owell. is that how it works though?? If you change the initialization of the array to some other number it wont work?? Thats crazy if it doesnt

edit: the funny thing is that you just happend to initialize all of your arrays to 0 and it worked
Last edited on Dec 18, 2009 at 8:17am
Dec 18, 2009 at 8:17am
You're correct if it's anything but 0 it just gives a trash number.
Dec 18, 2009 at 8:23am
glad I could help
Topic archived. No new replies allowed.