C++ Question: How to retrieve the number of elements of an array of dynamic objects

Started by
56 comments, last by liquidAir 21 years, 4 months ago
quote:Original post by DrPizza
Ooops, that was meant to reply to SabreMan''s post.

You mean to eliminate the copy call? Even better, if an initialising copy is appropriate to the problem, and it sounds like it is.
Advertisement
ok, thanks Sabreman, im now convinced. But before I finally seal this up, letme tel u why I used the sizeof operator

I created an array of integers, say
int GoodIntegers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

sizeof(GoodIntegers) gave 40!

which implies that sizeof(int) is 4!

So, logically (or shuldi say mathematically?) the number of elements in the array is
sizeof(GoodIntegers/4)

Well I used this method, but I had the feeling it was wrong! Thanks anyway!

And to you guyz, keep up the fights and the flames! ha ha ha :D
oops, and i forgot...

"C++ in itself isn''t difficult. It just requires the mere ability of a human to understand its concepts and use em well!"
by ME! he he B)
quote:Original post by liquidAir
I created an array of integers, say
int GoodIntegers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

sizeof(GoodIntegers) gave 40!

which implies that sizeof(int) is 4!

Correct.
quote:
So, logically (or shuldi say mathematically?) the number of elements in the array is
sizeof(GoodIntegers/4)

Also correct.
quote:
Well I used this method, but I had the feeling it was wrong!

The problem is the idiotic manner in which arrays are designed to operate. You cannot pass arrays into or out of functions, only a pointer to the array. That means that once you are outside the scope where you declared the array, you only have a pointer. Any attempt to perform sizeof() on that will return the size of the pointer, not the size of the array. The same thing happens when you use new. You also cannot do a type-compatible copy of arrays, which is stupid. And people think C arrays are the best solution... sheesh!
sabes, i wouldn''t call c style arrays idiotic. it''s just the best way kernigan and richie could think of doing them at the time.

it would be idiotic though to prefer using them over std::vector, or std::deque without some extreme reason to do with memory or platform specific issues, or whatever.
quote:std::vector is probably closest to Java arrays ... except they are type-checked (e.g. std::vector).


erm.. Java arrays *are* type-checked.
Star Dart - Java Multiplayer Space Combat at http://www.stardart.net/
quote:sabes, i wouldn''t call c style arrays idiotic. it''s just the best way kernigan and richie could think of doing them at the time.

I find this very hard to believe.

There are plenty of old languages withot such chronic retardation; Pascal, for instance.

Of course, Pascal''s arrays went too far in the other direction; whilst C''s arrays always decay into a pointer (and hence lose sizing information and value semantics), Pascal''s never decay, not even into unsized arrays, which is prohibitively restrictive.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
quote:Original post by markuskidd
erm.. Java arrays *are* type-checked.


I was thinking of Java container classes. Thanks for correcting me.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement