once aand for aall

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • grahamo

    once aand for aall

    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
  • David Harmon

    #2
    Re: once aand for aall

    On 25 Feb 2004 16:58:30 -0800 in comp.lang.c++,
    graham_walsh50@ hotmail.com (grahamo) was alleged to have written:[color=blue]
    >
    >when am I guaranteed the size in bytes of primitive types?[/color]

    Almost never. There are some minimum constraints, but no exact size
    guarantees.
    [color=blue]
    > Can I always assume a char is one byte?[/color]

    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++.

    Also, there are lots of other interesting numbers defined in <limits>
    that you may wish to look at.
    [color=blue]
    > can I always assume that my float is 4 bytes?[/color]

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

    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.
    [color=blue]
    >2)
    >
    >also, in the following code, when an instace of derived has been
    >created, why exactly is the function F not ambiguous?[/color]

    This issue is covered in Marshall Cline's C++ FAQ. See the topic
    "[23.6] What's the meaning of, Warning: Derived::f(floa t) hides
    Base::f(int)?" It is always good to check the FAQ before posting. You
    can get the FAQ at:


    Comment

    • Sharad Kala

      #3
      Re: once aand for aall


      "grahamo" <graham_walsh50 @hotmail.com> wrote in message
      news:79528aa8.0 402251658.66f0c 64a@posting.goo gle.com...[color=blue]
      > Can somebody answer me this..[/color]

      You have already got anwers to your first two questions.
      [color=blue]
      > 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)[/color]

      Doesn't seem valid code to me.
      Instead if the macros are on different lines then it should be fine.[color=blue]
      >
      >
      > 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?[/color]

      [] 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


      Comment

      Working...