determine array size

Started by
9 comments, last by Daivuk 20 years, 7 months ago
(sorry about my english, I''m from quebec) Hi, I just want to know the size of one of my array. What I mean is : I have a type : dk_Poly And I have an array of poly : dk_Poly *PolyList; Then I create a array of a specified length : PolyList = new dk_Poly[NbPoly]; But I dont want to pass NbPoly to all functions and proc. So I''m just asking if there is a way to know the size of PolyList?? I tried sizeof(), but this only give me the size of the type or pointer. thx
Advertisement
int size=sizeof(*ptr)/sizeof(ptr); 


should do the trick
quote:Original post by CloudNine
int size=sizeof(*ptr)/sizeof(ptr);   


should do the trick


No. No. No. No. No. No.
It will *never* work with a pointer.

Daivuk - either use a C++ vector, or pass the size explicitely. There are no other options.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on September 3, 2003 12:27:18 PM]
"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
ya, ok. So i''ll will pass my NbPoly to all functions, proc and bla bla bla....

thanks
Why not? If the pointer is valid, I don''t see why it wouldn''t work.
I tried it.

sizeof(ptr) return 4 (size of a pointeur dah!)
sizeof(*ptr) return me 192 (size of my struct (type) dk_Poly)
quote:Original post by CloudNine
Why not? If the pointer is valid, I don''t see why it wouldn''t work.


Because a dereferenced pointer to a dk_Poly points to a single dk_Poly, not a whole array.
An array knows its size. A pointer doesn't know if it's pointing to a single element or an array and therefore cannot be used to find the size of the array.

You can write a function that either returns the correct result (with an array) or fails to compile (with a pointer) instead of returning an incorrect result.
template<class T, size_t n> size_t array_size( T(&)[n] ){  return n;}int array[100];int *ptr = array;int i = array_size(array); // Ok, returns 100int j = array_size(ptr);   // Doesn't compile


Daivuk - learn to use C++ vectors. They do the job nicely.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on September 3, 2003 12:28:59 PM]
"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
template

i''ve seen template when defining a template class.
i''ve seen template when defining templates functions.
but what is that line aboe mean?
is this part of the stl vector???
please explain that.
like i just said in another post. STL is your friend. theres no excuse to not use an STL vector now days (unless you have an old old compiler).

Bungo!
Bungo!

This topic is closed to new replies.

Advertisement