C++ Gurus...

Started by
19 comments, last by klubbkid 23 years, 7 months ago
Bjarne Stroustrup, the C++ Programming Language:
quote:
Sizes of C++ objects are expressed in terms of multiples of the size of a char, so by definition the size of a char is 1. The size of an object or type can be obtained using the sizeof operator. This is what is guaranteed about sizes of fundamental types:

1 == sizeof( char ) <= sizeof( short ) <= sizeof( int ) <= sizeof( long )

[ snip ]

In addition, it is guaranteed that a char has at least 8 bits.


Note the "at least" in there. There is no guarantee for the 8 bits, it could be 16, depending on the system size of a char.


People might not remember what you said, or what you did, but they will always remember how you made them feel.
~ (V)^|) |<é!t|-| ~

Edited by - MadKeithV on October 4, 2000 5:07:01 AM
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Advertisement
Die Unicode ! Die ! ;-)))) Don''t we all hate it ?
Tim--------------------------glvelocity.gamedev.netwww.gamedev.net/hosted/glvelocity
I''m sure the Japanese and Chinese don''t hate it!


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.
Want a real dumb a weird way to do it....

pass through the array with na infinite loop, take out the value at the array every time (and add some counter, obviosly) AND**** when you get an error you''ll know your reached the end... So youd have to catch this error (methinks)...

WOOOOOOOppss, I forgot, what happens if at the end of the array you have another piece of data, which doesn''t throw any kind of error... weeeellll back to the drawing board for another wierd and dumb idea....
make a array class

class myIntArray{  public:    myIntArray(int size):s(size), a(new int[size]){}    ~myIntArray()    {      delete [] a;    }    size(); // more functions like this - add(int) and stuff   private:    int s;    int * a;}[\code]    
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~I'm looking for work
Ummm... why have add() ??
make a template class and use operator overloding instead...
note tho that anyway you do it youll get an performance hit.
quote: Original post by MadKeithV
Bjarne Stroustrup, the C++ Programming Language:


Sizes of C++ objects are expressed in terms of multiples of the size of a char, so by definition the size of a char is 1. The size of an object or type can be obtained using the sizeof operator. This is what is guaranteed about sizes of fundamental types:

1 == sizeof( char ) <= sizeof( short ) <= sizeof( int ) <= sizeof( long )

In addition, it is guaranteed that a char has at least 8 bits.


Note the "at least" in there. There is no guarantee for the 8 bits, it could be 16, depending on the system size of a char.


That is all well and good, but Mr. Stroustrup is not the final word on C++. The ISO/ANSI board is the final word. And quoting their documents (per my earlier post) (anyone know where the final or most current version of the standards doc is on the web) it says bytes. Would you like to argue that a byte on some systems is not 8 bits.

Also, given Mr Stroustrup's definition, wouldn't the calculation of an array size given by many in this thread still work out correctly in all instances? Can you have a type smaller than a char on a system where char is 16 bits. I don't think sizeof is going to return .5 for such a type. So can it exist?

And while we are on the subject, and I may be wrong here, even though sizeof is an operator, I believe its result is always calculated at compile time and not at run time. Anyone know if I'm right or wrong on that one.

Mike Roberts
aka milo
mlbobs@telocity.com


Edited by - milo on October 4, 2000 5:21:34 PM

Edited by - milo on October 4, 2000 5:24:44 PM
You all just go ahead and discuss... I''m just letting the original poster know the way to work around this.

If you want an array with (for example) the 10 high scores, the define a variable MAX_HIGH_SCORES and use this in the array definition and in your code.

here they are:
// Define max. number of high scores
#define MAX_HIGH_SCORES 10

// Declare variable
int iaHighScores[MAX_HIGH_SCORES];

// Clear array
for( int i = 0; i < MAX_HIGH_SCORES; i++ )
{
iaHighScores = 0;
}
Check my games out at:http://www.GamePlayHeaven.com
Use STL for arrays. STL rocks.

-Jussi

"All it took was one lonely kiss"

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

It definitely won''t work with dynamically allocated memory. Besides, you always specify the size of an array in code, so it will probably be easier just to look in the code.


Snale
+--Java Game

This topic is closed to new replies.

Advertisement