Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Daivuk

determine array size

This topic is 5378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

(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

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
I tried it.

sizeof(ptr) return 4 (size of a pointeur dah!)
sizeof(*ptr) return me 192 (size of my struct (type) dk_Poly)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!