question concerning BYTE in c++

Discussion in 'C++' started by =?ISO-8859-15?Q?Joachim_B=F6rger?=, Jan 28, 2007.

  1. Good evening!

    I'm using variables to count from 0 to 4 in a recursion, and I just
    realized that I could save a lot of memory (and computational time?) by
    using a BYTE instead of an int.

    But, having an assignment like

    BYTE b = 5, bb = 10;
    b+= bb;

    the vc8-compiler puts out a warning message C4244 saying: "conversion
    from int to byte, possible loss of data (or similar"). So, I guessed
    that the += operator converts all stuff to int and then back to BYTE?

    If I replace the second line by b = b + bb, the warning message
    disappears, but I prefer the += operator which saves memory allication.

    Any clues?

    Joachim
     
    =?ISO-8859-15?Q?Joachim_B=F6rger?=, Jan 28, 2007
    #1
    1. Advertising

  2. =?ISO-8859-15?Q?Joachim_B=F6rger?=

    Phlip Guest

    Joachim Börger wrote:

    > BYTE b = 5, bb = 10;
    > b+= bb;


    > If I replace the second line by b = b + bb, the warning message
    > disappears, but I prefer the += operator which saves memory allication.


    Google "premature optimization".

    At the level of bits, bytes, and integers, there's almost nothing you can do
    to individual statements that an optimizing compiler won't do better. And
    modern CPUs overwhelmingly optimize in favor of 32-bit integers. You cannot
    guess that 8 bit unsigned values will ever be faster or smaller, when they
    risk wasting 75% of the CPU's pipeline.

    And += is never smaller or faster than = x +. You should learn to write
    clear code that's easy to maintain. Someone reading your code shouldn't be
    confused by BYTE types on trivial values with no application-specific reason
    to only hold 8 bits.

    --
    Phlip
    http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
     
    Phlip, Jan 28, 2007
    #2
    1. Advertising

  3. =?ISO-8859-15?Q?Joachim_B=F6rger?=

    Daniel T. Guest

    Joachim Börger <> wrote:

    > I'm using variables to count from 0 to 4 in a recursion, and I just
    > realized that I could save a lot of memory (and computational time?) by
    > using a BYTE instead of an int.


    (I'm assuming that BYTE is just a typedef or define for unsigned char.)

    The computational time would likely be slower, not faster, but yes you
    might save a few bytes of space. Two questions, are you actually running
    out of space? Is this particular function really the biggest space hog
    you have in your system?

    > But, having an assignment like
    >
    > BYTE b = 5, bb = 10;
    > b+= bb;
    >
    > the vc8-compiler puts out a warning message C4244 saying: "conversion
    > from int to byte, possible loss of data (or similar"). So, I guessed
    > that the += operator converts all stuff to int and then back to BYTE?


    This I don't know.

    > If I replace the second line by b = b + bb, the warning message
    > disappears, but I prefer the += operator which saves memory allication.


    For intrinsic types, there is likely no advantage at all in using +=
    over = x + y.
     
    Daniel T., Jan 28, 2007
    #3
  4. =?ISO-8859-15?Q?Joachim_B=F6rger?=

    Jerry Coffin Guest

    In article <>,
    says...
    > Good evening!
    >
    > I'm using variables to count from 0 to 4 in a recursion, and I just
    > realized that I could save a lot of memory (and computational time?) by
    > using a BYTE instead of an int.


    Maybe. It's usually a time vs. space situation: if the compiler really
    does pack the variables to save any space, it'll cost extra time. Unless
    you can convert quite a few items, the space savings will be
    insignificant at best (and probably nonexistent) but you're still likely
    to lose some speed -- the compiler has to either override the
    instruction operand size (bigger, potentially slower instruction) or
    else add an extra instruction to mask the result to the correct result
    size. Depending on processor, you also stand a chance of creating a
    false dependency that will prevent instructions from executing in
    parallel, even though they really operate on independent data.

    > But, having an assignment like
    >
    > BYTE b = 5, bb = 10;
    > b+= bb;
    >
    > the vc8-compiler puts out a warning message C4244 saying: "conversion
    > from int to byte, possible loss of data (or similar"). So, I guessed
    > that the += operator converts all stuff to int and then back to BYTE?


    Yes, that's probably what you're seeing.

    > If I replace the second line by b = b + bb, the warning message
    > disappears, but I prefer the += operator which saves memory allication.


    What makes you think that? It probably makes no difference to the
    generated code -- though, as mentioned above, using ints throughout is
    probably your best bet.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Jan 28, 2007
    #4
  5. =?ISO-8859-15?Q?Joachim_B=F6rger?=

    terminator Guest

    On Jan 28, 4:44 am, Joachim Börger <> wrote:
    > Good evening!
    >
    > I'm using variables to count from 0 to 4 in a recursion, and I just
    > realized that I could save a lot of memory (and computational time?) by
    > using a BYTE instead of an int.
    >
    > But, having an assignment like
    >
    > BYTE b = 5, bb = 10;
    > b+= bb;
    >
    > the vc8-compiler puts out a warning message C4244 saying: "conversion
    > from int to byte, possible loss of data (or similar"). So, I guessed
    > that the += operator converts all stuff to int and then back to BYTE?
    >
    > If I replace the second line by b = b + bb, the warning message
    > disappears, but I prefer the += operator which saves memory allication.
    >
    > Any clues?
    >
    > Joachim


    with unsigned char I do not recieve that warning on vc7.
     
    terminator, Jan 28, 2007
    #5
  6. =?ISO-8859-15?Q?Joachim_B=F6rger?=

    Grizlyk Guest

    Joachim Börger wrote:
    >
    > BYTE b = 5, bb = 10;
    > b+= bb;
    >
    > the vc8-compiler puts out a warning message C4244 saying: "conversion from
    > int to byte, possible loss of data (or similar"). So, I guessed that the
    > += operator converts all stuff to int and then back to BYTE?


    Write declaration of BYTE here and try to compile your code to asm.

    --
    Maksim A Polyanin
     
    Grizlyk, Jan 28, 2007
    #6
  7. =?ISO-8859-15?Q?Joachim_B=F6rger?=

    peter koch Guest

    On 28 Jan., 02:44, Joachim Börger <> wrote:
    > Good evening!
    >
    > I'm using variables to count from 0 to 4 in a recursion, and I just
    > realized that I could save a lot of memory (and computational time?) by
    > using a BYTE instead of an int.

    Are you sure? How did you measure your savings?
    >
    > But, having an assignment like
    >
    > BYTE b = 5, bb = 10;
    > b+= bb;
    >
    > the vc8-compiler puts out a warning message C4244 saying: "conversion
    > from int to byte, possible loss of data (or similar"). So, I guessed
    > that the += operator converts all stuff to int and then back to BYTE?

    This is exactly what happens - as mandated by the standard.

    >
    > If I replace the second line by b = b + bb, the warning message
    > disappears, but I prefer the += operator which saves memory allication.


    There is no saving of anything, and the disappearance of the warning
    is simply a result of the compiler having an inconsistent view of what
    warnings to issue.
    I would go for the statements with the clearest demonstration of
    intent - and for me that is operator +=.

    /Peter
     
    peter koch, Jan 28, 2007
    #7
  8. On Jan 28, 3:10 am, "Phlip" <> wrote:
    > And += is never smaller or faster than = x +.


    One should take it easy with general statements like that, for built-
    in types that is probably true, but for user-specified types the
    difference can be huge.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 29, 2007
    #8
  9. =?ISO-8859-15?Q?Joachim_B=F6rger?=

    terminator Guest

    On Jan 28, 10:08 pm, "peter koch" <> wrote:
    > On 28 Jan., 02:44, Joachim Börger <> wrote:> Good evening!
    >
    > > I'm using variables to count from 0 to 4 in a recursion, and I just
    > > realized that I could save a lot of memory (and computational time?) by
    > > using a BYTE instead of an int.Are you sure? How did you measure your savings?

    >
    > > But, having an assignment like

    >
    > > BYTE b = 5, bb = 10;
    > > b+= bb;

    >
    > > the vc8-compiler puts out a warning message C4244 saying: "conversion
    > > from int to byte, possible loss of data (or similar"). So, I guessed
    > > that the += operator converts all stuff to int and then back to BYTE?This is exactly what happens - as mandated by the standard.

    >
    >
    >
    > > If I replace the second line by b = b + bb, the warning message
    > > disappears, but I prefer the += operator which saves memory allication.There is no saving of anything, and the disappearance of the warning

    > is simply a result of the compiler having an inconsistent view of what
    > warnings to issue.
    > I would go for the statements with the clearest demonstration of
    > intent - and for me that is operator +=.
    >
    > /Peter


    VC compilers are good optimizers 4 such simple cases. no need to think
    about it.
     
    terminator, Jan 29, 2007
    #9
  10. peter koch schrieb:
    >
    > On 28 Jan., 02:44, Joachim Börger <> wrote:
    >> Good evening!
    >>
    >> I'm using variables to count from 0 to 4 in a recursion, and I just
    >> realized that I could save a lot of memory (and computational time?) by
    >> using a BYTE instead of an int.

    > Are you sure? How did you measure your savings?


    Well, one BYTE (typedef'd as unsigned char) is one byte, while one
    typically needs 4 bytes. I need variables counting from 0 to 4 to store
    search states in a backtracking algorithm, and storing 1 Mio states with
    about 32 variables each (for a card game) can be much cheaper using
    bytes than ints.

    But I actually overcame the situation using ints and a reformulation of
    my algorithm hich needs far fewer states. Now, I've got new problems ;-)

    Thanks for Your hints anyway! Ciao,

    Joachim
     
    =?ISO-8859-1?Q?Joachim_B=F6rger?=, Jan 29, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: Question concerning Timer Control

    Kevin Spencer, Sep 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    410
    Kevin Spencer
    Sep 21, 2004
  2. =?Utf-8?B?U3RhbmxleSBBbGV4?=

    Question concerning Timer Control

    =?Utf-8?B?U3RhbmxleSBBbGV4?=, Sep 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    314
    =?Utf-8?B?U3RhbmxleSBBbGV4?=
    Sep 21, 2004
  3. djc
    Replies:
    2
    Views:
    553
  4. Joel Zinn
    Replies:
    2
    Views:
    589
    Scott Allen
    Mar 29, 2005
  5. =?Utf-8?B?RmxhY2s=?=

    Newbie question concerning page load event

    =?Utf-8?B?RmxhY2s=?=, May 17, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    515
    TDAVISJR
    May 17, 2005
Loading...

Share This Page