C++ Gurus...

Started by
19 comments, last by klubbkid 23 years, 6 months ago
I''m trying to find out how many elements are in an array: int blah[10]; I want to get the number 10 - Derek
Advertisement
You''re out of luck there... you''re supposed to KNOW how many elements are in an array, because you can''t query its size in C++. You can in Java though.


People might not remember what you said, or what you did, but they will always remember how you made them feel.
~ (V)^|) |<é!t|-| ~
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
sizeof( blah ) returns the number of bytes in the entire array. So, if you divide this by the size of a single element in the array, you''ll get the number of elements in the array.

For example, the program below prints "10"

#include

int main()
{
int blah[ 10 ];
printf( "%d\n", sizeof( blah ) / sizeof( blah[ 0 ] ) );
return 0;
}
quote:Original post by mother
sizeof( blah ) returns the number of bytes in the entire array.


No it doesn''t, it returns the number of times a char fits into the size of the array, which on most platforms happens to be a byte. This is not guaranteed to be a byte by the C++ standard.




People might not remember what you said, or what you did, but they will always remember how you made them feel.
~ (V)^|) |<é!t|-| ~
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Sir, I challenge you to a duel.

From section 5.3.3.1 of the HTML version of the December 1996 Working Paper for the ISO/ANSI C++ Standard (the Nov 97 version became the standard),

"The sizeof operator yields the number of bytes in the object representation of its operand."

and from section 5.3.3.2 of the same,

"This implies that the size of an array of n elements is n times the size of an element."

Also, straight from MSDN for VC++ 6.0,

"The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate
types)."

And from the example on the same page:
size_t sizearr =                       // Count of items in array             sizeof( array ) / sizeof( array[0] ); 


I also checked the Borland compiler''s help at work. It says bytes, as well.

So please document your claim there MadKeithV.

Mike Roberts
aka milo
mlbobs@telocity.com
Mike:

That works great...in the main loop


#include
#include
#include

void SortArray(int *array)
{
int temp;

for(int j = 0; j < 1000; j++)
{
for(int i = 0; i < 1000; i++)
{
if(array > array)<br> {<br> temp = array;<br> array = array;<br> array = temp;<br> }<br> }<br> }<br>}<br><br>void FillArray(int *array)<br>{<br> srand((unsigned)time(NULL));<br><br> for(int i = 0; i < 1000; i++)<br> array = abs(rand()%100);<br>}<br><br>int main()<br>{<br> int blah[1000];<br> size_t sizearr = sizeof( blah ) / sizeof( blah[0] );<br> printf("%d", sizearr);<br><br> FillArray(blah);<br> SortArray(blah);<br><br> return 0;<br>}<br></code><br><br>How come if I use the sizeof / sizeof I get a one inside the functions?<br><br>- Derek </i>
Actually, chars are always guaranteed to be bytes, as far as I can tell (they''re the only data type with a certain size on all platforms).

But to answer your question about why it doesn''t work elsewhere: you have to understand how the function sees it. It sees a pointer to an integer, which may or may not have integers after it. There''s no way to know at run time nor at compile time (without heavy parsing and restrictions). Pass in the length as another parameter. If this happens in other languages, it''s because they''re doing it this way under the hood (with the minor exception of certain constructs like strings in Pascal, where the length is a part of the dataset).
jrt
The sizeof operator has two distinct uses:
sizeof unary-expression
sizeof (type-name)
The result in both cases is an integer constant that gives the size in bytes of how much memory space is used by the operand (determined by its type, with some exceptions). The amount of space that is reserved for each type depends on the machine.
In the first use, the type of the operand expression is determined without evaluating the expression (and therefore without side effects). When the operand is of type char (signed or unsigned), sizeof gives the result 1. When the operand is a non-parameter of array type, the result is the total number of bytes in the array (in other words, an array name is not converted to a pointer type). The number of elements in an array equals sizeof array/ sizeof array[0] .

If the operand is a parameter declared as array type or function type, sizeof gives the size of the pointer. When applied to structures and unions, sizeof gives the total number of bytes, including any padding.
You cannot use sizeof with expressions of function type, incomplete types, parenthesized names of such types, or with an lvalue that designates a bit field object.
The integer type of the result of sizeof is size_t, defined in stddef.h.

You can use sizeof in preprocessor directives; this is specific to Borland C++.
In C++, sizeof(classtype), where classtype is derived from some base class, returns the size of the object (remember, this includes the size of the base class).

Borland C++ 5.0 Programmer''s Guide



Null and Void
"In this house we obey the laws of thermodynamics!" --Homer Simpson
You can find out how many elements are in an array very easily. Heres how.

Lets say you have an array called values. This line of code will tell how many elements are in that array.

int number = sizeof( values ) / sizeof( values[0] );

I hope this helps.

-Snyper
=============================Where's the 'any' key?=============================
Snyper, as discussed above, this will not work if you do not have the variable declared in the scope in which your sizeof expression appears.
jrt

This topic is closed to new replies.

Advertisement