Jump to content
  • Advertisement

Archived

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

klubbkid

C++ Gurus...

This topic is 6497 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

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|-| ~

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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[i + 1])
{
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}

void FillArray(int *array)
{
srand((unsigned)time(NULL));

for(int i = 0; i < 1000; i++)
array[i] = abs(rand()%100);
}

int main()
{
int blah[1000];
size_t sizearr = sizeof( blah ) / sizeof( blah[0] );
printf("%d", sizearr);

FillArray(blah);
SortArray(blah);

return 0;
}


How come if I use the sizeof / sizeof I get a one inside the functions?

- Derek

Share this post


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

Share this post


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

Share this post


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

Share this post


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

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!