returning array size

T

theAnswer

hello,
Is there a way to implement a little function to return the size of an array
(not a string)?

Something like int size(int *array)

thanx
 
J

Joona I Palaste

theAnswer said:
hello,
Is there a way to implement a little function to return the size of an array
(not a string)?
Something like int size(int *array)

If the array is passed as an int * parameter, such a function is
impossible. However, if its declaration is in scope, then sizeof array
will return the array's size in bytes.
 
D

Derk Gwen

# hello,
# Is there a way to implement a little function to return the size of an array
# (not a string)?
#
# Something like int size(int *array)

The language does not define any way of extracting the allocated size
from a pointer to the first (or other) element. While an implementation
might (for example if pointer are segregated to different pages based on
allocation size), the portable solutions are to pass the length around
with the pointer or use a sentinel vector element.
 
M

Martin Ambuhl

theAnswer said:
hello,
Is there a way to implement a little function to return the size of an array
(not a string)?

Something like int size(int *array)

No. Please check the FAQ before posting.
 
L

Leor Zolman

# hello,
# Is there a way to implement a little function to return the size of an array
# (not a string)?
#
# Something like int size(int *array)

The language does not define any way of extracting the allocated size
from a pointer to the first (or other) element. While an implementation
might (for example if pointer are segregated to different pages based on
allocation size), the portable solutions are to pass the length around
with the pointer or use a sentinel vector element.

If the OP chooses to pass the length along with the pointer, I could
show him a _real_ efficient implementation of size() ...
-leor

P.S. Have I gauged this right, such that it would be perceived as
humorous rather than sarcastic? I didn't want to put a smiley there
and prejudice the verdict...


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
R

Richard Heathfield

Leor said:
If the OP chooses to pass the length along with the pointer, I could
show him a _real_ efficient implementation of size() ...

Quite so. I near... but then I thought better of it.

-leor

P.S. Have I gauged this right, such that it would be perceived as
humorous rather than sarcastic?

There's a difference? Sarcasm is the lowest form of wit only because nobody
is beneath using it.
I didn't want to put a smiley there
and prejudice the verdict...

Not putting smileys in often enough has got me into lots of trouble. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top