• Advertisement

Archived

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

determine array size

This topic is 5251 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
ya, ok. So i''ll will pass my NbPoly to all functions, proc and bla bla bla....

thanks

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
Guest Anonymous Poster
Fruny''s template for array_size is confusing at first, but it all becomes clear when you realize that C++ can have unnamed function parameters. I believe this is discussed in one of the first few hints in Meyer''s Essential STL book, but in a different context.

Anyway, basically array_size is a parameterized function that takes an array reference as a parameter. Just like if you were to write this function...

int doSomething (int (&array)[90]);

...it would only compile if you did something like this:

int somearray[90];
int otherarray[91];

int main ()
{
doSomething (somearray); // fine
doSomething (otherarray); // not fine
}

By the way, the parenthesis above "int (&array)[90]" are because of operator precedence, I assume.

Now onto unnamed function parameters... you actually see this all the time in C code:

int somefunction (int, int, float);

The above is a valid prototype, and... if for some reason the function has a parameter which it doesn''t use, it can safely remain anonymous during the actual function body.

As it turns out, that''s exactly what we have with array_size; we don''t care at all about what the array actually is, just its size. Therefore the parameter can (but does not have to) remain unnamed.

Anyway, assuming you understand typed and non-typed template parameters, array_size should be pretty clear to you now.

Share this post


Link to post
Share on other sites

  • Advertisement