once aand for aall

G

grahamo

Can somebody answer me this..

1)

when am I guaranteed the size in bytes of primitive types? Can I
always assume a char is one byte? can I always assume that my float is
4 bytes? likewise for longs, unsigned longs etc. What are the hard and
fast rules?


2)

also, in the following code, when an instace of derived has been
created, why exactly is the function F not ambiguous?


class base
{
public:

virtual int f(int) { };
};

class derived : public base
{
public:

derived()
{
f(0);
};

int f(char* s) { };
};


3)
is this legal? It all appears on the same line.

#define ABS(a) (a) <0 ? -(a):(a) #define GTR(b) (b)>9? (1) : (0)


4)

lastly, if I have

int matrix[][2] = { {1}, {2} };


can somebody tell me exactly whats initialised to what and why the []
is legal. What exactly have I declared and initialised above?



thanks much for your help.


GrahamO
 
D

David Harmon

when am I guaranteed the size in bytes of primitive types?

Almost never. There are some minimum constraints, but no exact size
guarantees.
Can I always assume a char is one byte?

Yes. "char" and "byte" are used interchangeably.

Very important: "char" and "byte" DOES NOT have to be exactly 8 bits.
It must be at least 8 bits, but can be more, like e.g. 9 or 16.

The constant CHAR_BITS defined in <limits.h> tells you how many bits in
a byte in your particular implementation of C++.

The sizeof() operator tells you how many CHAR_BITS units other types are
in your particular implementation of C++.

can I always assume that my float is 4 bytes?

No. float could easily be 1 byte on a machine with 32-bit bytes.
likewise for longs, unsigned longs etc. What are the hard and
fast rules?

long must be at least 32 bits. Could be more.
int must be at least 16 bits. Could be more.
char must be at least 8 bits. Could be more.
etc.

If the smallest physically addressable unit of storage on your machine
is 32 bits, then char, int, and long could possibly all be the same
size.
2)

also, in the following code, when an instace of derived has been
created, why exactly is the function F not ambiguous?

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[23.6] What's the meaning of, Warning: Derived::f(float) hides
Base::f(int)?" It is always good to check the FAQ before posting. You
can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
 
S

Sharad Kala

grahamo said:
Can somebody answer me this..

You have already got anwers to your first two questions.
3)
is this legal? It all appears on the same line.

#define ABS(a) (a) <0 ? -(a):(a) #define GTR(b) (b)>9? (1) : (0)

Doesn't seem valid code to me.
Instead if the macros are on different lines then it should be fine.
4)

lastly, if I have

int matrix[][2] = { {1}, {2} };


can somebody tell me exactly whats initialised to what and why the []
is legal. What exactly have I declared and initialised above?

[] is legal because compiler is smart enough to find the number of rows in such
initializations.
You have declared an array with 2 rows and 2 columns.
Here matrix[0][0] = 1
matrix[0][1]=0
matrix[1][0]=2
matrix[1][1]=0

Note that in -
int i[10] ={2} only i[0] has a value of 2, all others are 0.

-Sharad
 

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

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top