Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

beginner question about storing binary data in buffers, seeing binarydata in a variable, etc

Question posted by: darren (Guest) on July 4th, 2008 09:25 PM
Hi there

Im working on an assignment that has me store data in a buffer to be
sent over the network. I'm ignorant about how C++ stores data in an
array, and types in general.

If i declare an array of chars that is say 10 bytes long:
char buff[10];
does this mean that i can safely store 80 bits of data?

When i think of an array of chars, i think each spot in the array as a
sequence of 8 1's or 0's. Is this a correct visualization? I guess
my question here is why do most buffers seem to be implemented as char
arrays? Can any binary value between 0 and 255 be safely put into a
char array slot (00000000 to 11111111). Why not implement a buffer
using uint8_t ?

Obviously I have a very loose grasp on how buffers are saving data,
and how a receiver gets this data on their end. I understand the
sockets stuff, just not the buffer-specific stuff. Any enlightenment
would be most appreciated.

thanks.
Daniel T.'s Avatar
Daniel T.
Guest
n/a Posts
July 4th, 2008
11:25 PM
#2

Re: beginner question about storing binary data in buffers, seeing binarydata in a variable, etc
darren <minofifa@gmail.comwrote:
Quote:
If i declare an array of chars that is say 10 bytes long:
char buff[10];
does this mean that i can safely store 80 bits of data?


Use unsigned char for buffers instead of char. Yes, you can store at
least 80 bits of data, possibly more. The exact number of bits you can
store are 10 * CHAR_BIT.
Quote:
When i think of an array of chars, i think each spot in the array
as a sequence of 8 1's or 0's. Is this a correct visualization?


Close. There may be more than 8 bits, and the first bit may be treated
special if the char is signed. Better to use unsigned char.
Quote:
I guess my question here is why do most buffers seem to be
implemented as char arrays?


I don't think they are, I think most are implemented as unsigned char
arrays.
Quote:
Can any binary value between 0 and 255 be safely put into a char
array slot (00000000 to 11111111).


Look in limits.h. A 'char' is guaranteed to hold at least from 0 to 127
inclusive. A 'signed char' is guaranteed to hold at least from -127 to
127 inclusive. An 'unsigned char' is guaranteed to hold at least from 0
to 255 inclusive. So again, of the three, unsigned char is your best
choice.
Quote:
Why not implement a buffer using uint8_t ?


No such type exists in the language.

James Kanze's Avatar
James Kanze
Guest
n/a Posts
July 5th, 2008
07:45 AM
#3

Re: beginner question about storing binary data in buffers, seeing binarydata in a variable, etc
On Jul 4, 11:35 pm, "Alf P. Steinbach" <al...@start.nowrote:
Quote:
* darren:
Quote:
Im working on an assignment that has me store data in a
buffer to be sent over the network. I'm ignorant about how
C++ stores data in an array, and types in general.

Quote:
A textbook would be good resource.


Most probably don't say anything about it, because it's
unspecified. Except for the specifications of how pointer
arithmetic works within arrays.

[...]
Quote:
Quote:
I guess my question here is why do most buffers seem to be
implemented as char arrays?

Quote:
Are they?


Transmission buffers, yes. char[] or unsigned char[] are really
your only two choices. (I generally use unsigned char, but the
C++ standard does try to make char viable as well. And on most
typical architectures, where converting between char and
unsigned char doesn't change the bit pattern, both work equally
well in practice.)
Quote:
Quote:
Can any binary value between 0 and 255 be safely put into a
char array slot (00000000 to 11111111).

Quote:
It depends what you're really asking.

Quote:
If you provided a concrete example and what you expected as
result, one could say whether that was correct or not.

Quote:
But a char has guranteed at least 256 possible bitpatterns
(minimum 8 bits), yes.


On the other hand, I think in theory, char could be signed 1's
complement, and assigning a negative 0 (0xFF) could force it to
possitive (which would mean that you could never get 0xFF by
assignment---but you could memcpy it in). I think: I'm too lazy
to verify in the standard, and of course, any implementation
that actually did this would break so much code as to be
unviable.
Quote:
Quote:
Why not implement a buffer using uint8_t ?

Quote:
That's not presently a standard C++ type.


It's still a viable alternative.

It's possible to write code for binary network protocols in a
perfectly portable manner. It's rarely worth it, since it
entails some very complex work arounds for what are, in the end,
very rare and exotic machines that most of us don't have to deal
with. Thus, I know that much of the networking software I write
professionally will fail on a machine with an unusual convertion
of unsigned to signed (i.e. which isn't 2's complement, and
doesn't just use the underlying bit pattern).

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze's Avatar
James Kanze
Guest
n/a Posts
July 5th, 2008
02:05 PM
#4

Re: beginner question about storing binary data in buffers, seeing binarydata in a variable, etc
On Jul 5, 10:48 am, "Alf P. Steinbach" <al...@start.nowrote:
[...]
Quote:
Quote:
On the other hand, I think in theory, char could be signed 1's
complement, and assigning a negative 0 (0xFF) could force it to
possitive (which would mean that you could never get 0xFF by
assignment---but you could memcpy it in). I think: I'm too lazy
to verify in the standard, and of course, any implementation
that actually did this would break so much code as to be
unviable.

Quote:
In that case any implementation for 1's complement that used
1's complement also for signed 'char' would be unviable... :-)


As it happens, in the two implementations I'm aware of where
signed integers are not 2's complement, plain char is unsigned,
thus avoiding the problem. (If one of the goals of plain char
is to contain text characters, then it really should be unsigned
anyway. Historically, however, making char unsigned had a
non-negligible runtime cost on a PDP-11, and since back then,
all the world was PDP-11, and the only character set which
counted was ASCII, which only uses the lower 7 bits...)
Quote:
It makes an interesting case for dropping that support in the
standard, and go for requirement of two's complement for all
signed integral types.


Perhaps a better choice would be to require that plain char be
unsigned, so that you could safely use it with the results of
e.g. istream::get() or fgetc(). (Or both, but I don't think
you'll find much support for either in the committee.)
Quote:
Quote:
Quote:
> Why not implement a buffer using uint8_t ?

Quote:
Quote:
Quote:
That's not presently a standard C++ type.

Quote:
Quote:
It's still a viable alternative.

Quote:
Yes, and the main reason for "why not" is that it's not a
standard C++ type.

Quote:
Quote:
It's possible to write code for binary network protocols in a
perfectly portable manner. It's rarely worth it, since it
entails some very complex work arounds for what are, in the end,
very rare and exotic machines that most of us don't have to deal
with. Thus, I know that much of the networking software I write
professionally will fail on a machine with an unusual convertion
of unsigned to signed (i.e. which isn't 2's complement, and
doesn't just use the underlying bit pattern).

Quote:
Tune up the warning level, perhaps? <g>


What I'm waiting for is a machine which will core dump if the
conversion fails (i.e. doesn't result in the same value). I
don't really expect to see it, however, given that one standard
idiom in C is things like:

int ch = fgetc( input ) ;
while ( ch != EOF && someOtherConditions( ch ) ) {
*p ++ = ch ; // Where p is a char*...
}

It's a bit surprising that something this widespread is
implementation defined, and may result in an implementation
defined signal (according to the C standard---the C++ standard
still has the imprecisions of C90). Because it is so
widespread, however, I don't expect to see a compiler which
doesn't support it anytime soon. (As I said, all of the
"exotic" architectures that I know make plain char unsigned,
which effectively removes the "implementation defined" here.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Not the answer you were looking for? Post your question . . .
189,170 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors