473,796 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Four or Two Bytes?

Does anybody know the answer to the following? An unsigned short is 2
bytes long. Why is the following file created as 4 bytes instead of
2?

file = fopen("data.bin ", "wb");
if (file != NULL)
{
unsigned short s = 65535;
printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
short)); // 2 bytes - OK
fwrite(&s, sizeof(unsigned short), sizeof(s), file);
fclose(file); // File written as 4 bytes... hmm
}

- Hahnemann

Jun 27 '08
33 1832
Joachim Schmitz wrote:
Martin wrote:
>Joachim Schmitz <no*********@sc hmitz-digital.dewrote :
>>Guess Keith's point is that you should cast it to whatever the
format specified expects.

I'll wait for Keith's response, but I would say that sometimes
it may be better to change the format specified. Casting an
unsigned type to signed type of equal or less width has the
potential for data loss.

Yes it might have, but how likely is it that a data type is
really larger than INT_MAX?
If you simply examine limits.h you will probably find that UINT_MAX
is roughly twice the size of INT_MAX. To me, this indicates your
probability is roughly 2:1. Anything over 0 is bad.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

** Posted from http://www.teranews.com **
Jun 27 '08 #11
CBFalconer wrote:
Joachim Schmitz wrote:
>Martin wrote:
>>Joachim Schmitz <no*********@sc hmitz-digital.dewrote :

Guess Keith's point is that you should cast it to whatever the
format specified expects.

I'll wait for Keith's response, but I would say that sometimes
it may be better to change the format specified. Casting an
unsigned type to signed type of equal or less width has the
potential for data loss.

Yes it might have, but how likely is it that a data type is
really larger than INT_MAX?

If you simply examine limits.h you will probably find that UINT_MAX
is roughly twice the size of INT_MAX. To me, this indicates your
probability is roughly 2:1. Anything over 0 is bad.
So what? The basic daty types have sizes of 1 to 8 bytes. Sensibel structs
and arrays are most probaly well smaller than 32K, which is the minimun for
INT_MAX, so again: how big is the chance that a real life data type is
larger than that?

Bye, Jojo
Jun 27 '08 #12
Joachim Schmitz wrote:
CBFalconer wrote:
>Joachim Schmitz wrote:
>>Martin wrote:
Joachim Schmitz <no*********@sc hmitz-digital.dewrote :

Guess Keith's point is that you should cast it to whatever the
format specified expects.

I'll wait for Keith's response, but I would say that sometimes
it may be better to change the format specified. Casting an
unsigned type to signed type of equal or less width has the
potential for data loss.

Yes it might have, but how likely is it that a data type is
really larger than INT_MAX?

If you simply examine limits.h you will probably find that UINT_MAX
is roughly twice the size of INT_MAX. To me, this indicates your
probability is roughly 2:1. Anything over 0 is bad.
So what? The basic daty types have sizes of 1 to 8 bytes. Sensibel
structs and arrays are most probaly well smaller than 32K, which is
the minimun for INT_MAX, so again: how big is the chance that a real
life data type is larger than that?
On top of that: here it is about 2 or 4, which even on the DS9K would easily
fit into INT_MAX.

Bye, Jojo
Jun 27 '08 #13
Joachim Schmitz wrote:
On top of that: here it is about 2 or 4, which even on the DS9K would easily
fit into INT_MAX.
Not on the DS9K.
unsigned short can have INT_MAX padding bytes on the DS9K.

--
pete
Jun 27 '08 #14
pete wrote:
Joachim Schmitz wrote:
>On top of that: here it is about 2 or 4, which even on the DS9K
would easily fit into INT_MAX.

Not on the DS9K.
unsigned short can have INT_MAX padding bytes on the DS9K.
Chapter and verse please
Jun 27 '08 #15
Joachim Schmitz wrote:
pete wrote:
>Joachim Schmitz wrote:
>>On top of that: here it is about 2 or 4, which even on the DS9K
would easily fit into INT_MAX.
Not on the DS9K.
unsigned short can have INT_MAX padding bytes on the DS9K.
Chapter and verse please
N869
6.2.6.2 Integer types

[#1] For unsigned integer types other than unsigned char,
the bits of the object representation shall be divided into
two groups: value bits and padding bits (there need not be
any of the latter). If there are N value bits, each bit
shall represent a different power of 2 between 1 and 2N-1,
so that objects of that type shall be capable of
representing values from 0 to 2N-1 using a pure binary
representation; this shall be known as the value
representation. The values of any padding bits are
unspecified.

--
pete
Jun 27 '08 #16
Martin <m@b.cwrites:
On Fri, 30 May 2008 04:59:22 +0100, Keith Thompson <ks***@mib.orgw rote:
>"%d" expects an argument of type int; you're giving it an argument of
type size_t. You should cast the value to int (one of the few cases
where a cast is appropriate):

printf("Size of s: %d bytes\n", (int)sizeof s);

Generally, isn't it better to use

printf("Size of s: %lu bytes\n", (unsigned long)sizeof s);

as recommended in the answer to FAQ question 7.15?
Yes. In this particular case, the expression ``sizeof s'' is the size
of an object of type unsigned short, so it's practically certain to be
no greater than INT_MAX. But using int rather than unsigned long was
just laziness in my part; IMHO it's better to get into the habit of
using unsigned long whether it's necessary or not, rather than wasting
time figuring out whether int is ok in this or that particular
instance.

In C99, you can use

printf("Size of s: %zu bytes\n", sizeof s);

to print a size_t value directly. I look forward eagerly to the day
when C99 is widely implemented and that code is reasonably portable.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #17
pete <pf*****@mindsp ring.comwrites:
Joachim Schmitz wrote:
>On top of that: here it is about 2 or 4, which even on the DS9K
would easily fit into INT_MAX.

Not on the DS9K.
unsigned short can have INT_MAX padding bytes on the DS9K.
Padding *bits*, not padding bytes.

But I think you have to pay for the commercial version of the DS9K C
compiler to get unsigned short with CHAR_BIT*INT_MA X padding bits; the
free version only has sizeof(unsigned short)==INT_MAX .

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #18
Joachim Schmitz wrote:
>
Martin wrote:
On Fri, 30 May 2008 10:19:31 +0100, Joachim Schmitz
<no*********@sc hmitz-digital.dewrote :
Guess Keith's point is that you should cast it to whatever the format
specified expects.
I'll wait for Keith's response, but I would say that sometimes it may
be better to change the format specified. Casting an unsigned type to
signed type of equal or less width has the potential for data loss.
Yes it might have, but how likely is it that a data type is really larger
than INT_MAX?
As I recall, "huge model" on real-mode x86 processors use 32-bit
pointers, yet 16-bit ints. As such, I would expect that size_t
could hold values larger than INT_MAX.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Jun 27 '08 #19
Kenneth Brody <ke******@spamc op.netwrites:
Joachim Schmitz wrote:
>Martin wrote:
On Fri, 30 May 2008 10:19:31 +0100, Joachim Schmitz
<no*********@sc hmitz-digital.dewrote :
Guess Keith's point is that you should cast it to whatever the format
specified expects.

I'll wait for Keith's response, but I would say that sometimes it may
be better to change the format specified. Casting an unsigned type to
signed type of equal or less width has the potential for data loss.
Yes it might have, but how likely is it that a data type is really larger
than INT_MAX?

As I recall, "huge model" on real-mode x86 processors use 32-bit
pointers, yet 16-bit ints. As such, I would expect that size_t
could hold values larger than INT_MAX.
Which is not particularly relevant in this case. The issue, as
Joachim wrote, is whether "a data type is really larger than INT_MAX".
It's not about whether the data type (unsigned short in this case) is
able to hold values larger than INT_MAX; it's about whether the size
of the data type is larger than INT_MAX bytes.

If I had an integer type bigger than 262136 bits, I don't think I'd
call it "short".

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2266
by: William S. Huizinga | last post by:
I've got an array.array of unsigned char and would like to make a slice of that array (e.g. a) become one long like I would in "C" : l = ((unsigned long *) (&a)); I have been getting what I want in this sort of manner : l = 0L l = a l += a << 8
11
5226
by: fivelitermustang | last post by:
Actually, how would I go about allocating a four-dimensional dynamic array? I only know how to make two dimensional dynamic arrays: double **v; v = new double*; for (int i=0; i<C; i++) { v = new double; }
1
1769
by: Chris Lutka | last post by:
I've been racking my brains all day over this. And I'm not the best at SQL either. I need a query that will produce the following results: Product,Warehouse,Sold_LastYear,Sold_ThisYear,Sold_3Months,Sold_MTD I've got four queries to give me the Sold_* fields. Their fields are Product, Warehouse, and Units. I also have a table which these queries originated from. The table UV_SPPROD (I didn't name it) has Product, Warehouse, Period...
19
5887
by: Lorenzo J. Lucchini | last post by:
My code contains this declaration: : typedef union { : word Word; : struct { : byte Low; : byte High; : } Bytes; : } reg;
1
2473
by: les | last post by:
If all are unsigned integers, then why not make it easy on yourself and just multiply each by a scaler to effect the shift? ie each position is just multiplied by 128^n-1 byte1 + byte2*128 + byte3 *(128 *128) + byte4 * (128 * 128 *128) >-----Original Message-----
6
2200
by: lovecreatesbeauty | last post by:
/* It seems that when an int with width of four bytes is assigned to a one byte width char, the first three bytes from left to right are discarded and the rightest byte is assigned to that char. So, I can just assign an int to a char to retrieve the rightest byte of that int, and I also use this method plus right-shift operation to get the leftest byte of an int.
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10468
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10205
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7559
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5458
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.