how to measure size of an array

A

asit dhal

Can anyone tell how to measure the size of an array without use of
sizeof operator ?
 
B

Ben Pfaff

asit dhal said:
Can anyone tell how to measure the size of an array without use of
sizeof operator ?

How about referring to one of the many threads in the last few
months that discuss this same lame question, instead of starting
a new thread?
 
L

lovecreatesbea...

Can anyone tell how to measure the size of an array without use of
sizeof operator ?

The size information is given at declaration of an array. When an
array is passed into a function, the sizeof can't evaluate the size of
the array inside the function.
 
K

Keith Thompson

asit dhal said:
Can anyone tell how to measure the size of an array without use of
sizeof operator ?

Use the sizeof operator. That's what it's for.
 
R

Richard Heathfield

(e-mail address removed) said:
The size information is given at declaration of an array. When an
array is passed into a function, the sizeof can't evaluate the size of
the array inside the function.

You don't pass an array to a function. You pass a pointer to that
array's first element.
 
M

Malcolm McLean

Jamie Boy said:
(char*)pover - (char*)p
Make that unsigned char *.
char * should be reserved for genuine character data, and can contain trap
representations. A highly unlikely but allowable implementation could even
crash your program if one of the pointers points to a trap representation.
 
B

Ben Pfaff

Malcolm McLean said:
Make that unsigned char *.
char * should be reserved for genuine character data, and can contain trap
representations. A highly unlikely but allowable implementation could even
crash your program if one of the pointers points to a trap representation.

I don't believe this. Can you cite chapter & verse on that?
 
F

Flash Gordon

Malcolm McLean wrote, On 14/02/07 22:50:
Make that unsigned char *.
char * should be reserved for genuine character data, and can contain trap
representations. A highly unlikely but allowable implementation could even
crash your program if one of the pointers points to a trap representation.

What the pointers point to is irrelevant since they are not dereferenced
so casting to char* is fine. The problem with it is how you determine
the correct value of pover before you know the size of the array.
 
K

Keith Thompson

Malcolm McLean said:
Make that unsigned char *.
char * should be reserved for genuine character data, and can contain trap
representations. A highly unlikely but allowable implementation could even
crash your program if one of the pointers points to a trap representation.

I don't believe so. The code does not attempt to evaluate any
pointed-to char object, just the pointers themselves.

Yes, unsigned char is generally better than char for this kind of
thing, but in this case there's no problem using char. (Assuming that
"pover" and "p" are defined appropriately; "Jamie Boy" gave no hint of
what they are.)
 
M

Malcolm McLean

Ben Pfaff said:
I don't believe this. Can you cite chapter & verse on that?
No, you are right. It only traps when it is a "lvalue", which doesn't mean
"left hand side of the equation" but when you try to read it. So in this
case neither of the pointers are dereferenced and it can't trap. Just
pointing to it is OK. Additonally the rules have been changed on character
traps. It seems that they don't generate UB any more until you pass them to
a text stream or sprintf().
 
B

Beej

Can anyone tell how to measure the size of an array without use of
sizeof operator ?

If you don't want to use the sizeof operator, two things that come to
mind are:

1) Keep track of the length of the array (or a pointer to the end) as
you build it, and that way you always know how big it is.

2) Put a sentinel value at the end of the array and the count the
number of elements from the start of the array until you hit the
sentinel.

The string library functions often work this second way, with a '\0'
character at the end of the array. That's how they know when to stop
counting, copying, printing, or whatever.

-Beej
 
2

2008music

If you don't want to use the sizeof operator, two things that come to
mind are:

1) Keep track of the length of the array (or a pointer to the end) as
you build it, and that way you always know how big it is.

2) Put a sentinel value at the end of the array and the count the
number of elements from the start of the array until you hit the
sentinel.

The string library functions often work this second way, with a '\0'
character at the end of the array. That's how they know when to stop
counting, copying, printing, or whatever.

-Beej

very very good!!!!!!!!!!!!!!!
 

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
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top