Skip to main content
GameDev.net gamedev.net
🔒 Locked

determine array size

Started by Daivuk Sep 3, 2003 at 11:08 AM 9 replies 1.5k views
Original Post
Daivuk
Daivuk
(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
CloudNine
CloudNine

int size=sizeof(*ptr)/sizeof(ptr);


should do the trick
Fruny
Fruny
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
Daivuk
Daivuk
ya, ok. So i''ll will pass my NbPoly to all functions, proc and bla bla bla....

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

sizeof(ptr) return 4 (size of a pointeur dah!)
sizeof(*ptr) return me 192 (size of my struct (type) dk_Poly)
Polymorphic OOP
Polymorphic OOP
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.
Fruny
Fruny
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 100

int 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
GekkoCube
GekkoCube
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.
BungoMan85
BungoMan85
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!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.