Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question concerning BYTE in c++

Reply
Thread Tools

question concerning BYTE in c++

 
 
=?ISO-8859-15?Q?Joachim_B=F6rger?=
Guest
Posts: n/a
 
      01-28-2007
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
 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      01-28-2007
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!!!


 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      01-28-2007
Joachim Börger <(E-Mail Removed)> 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.
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      01-28-2007
In article <(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed)
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.
 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      01-28-2007


On Jan 28, 4:44 am, Joachim Börger <(E-Mail Removed)> 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.

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-28-2007
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


 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      01-28-2007


On 28 Jan., 02:44, Joachim Börger <(E-Mail Removed)> 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

 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-29-2007
On Jan 28, 3:10 am, "Phlip" <(E-Mail Removed)> 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

 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      01-29-2007


On Jan 28, 10:08 pm, "peter koch" <(E-Mail Removed)> wrote:
> On 28 Jan., 02:44, Joachim Börger <(E-Mail Removed)> 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.

 
Reply With Quote
 
=?ISO-8859-1?Q?Joachim_B=F6rger?=
Guest
Posts: n/a
 
      01-29-2007
peter koch schrieb:
>
> On 28 Jan., 02:44, Joachim Börger <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a Primative byte array to a Byte array object Kirby Java 3 10-08-2004 03:01 AM
byte[] > string > byte[] problem Peter Java 3 08-05-2004 10:55 AM
connection 1: transfer chunk 1 (byte 0 to byte 1024) Jean-Daniel Gamache Java 0 07-14-2004 03:57 AM
Single byte addressable, multiple byte readout. Andreas VHDL 1 05-04-2004 01:49 PM
Appending byte[] to another byte[] array Bharat Bhushan Java 15 08-05-2003 07:52 PM



Advertisments