Re: My current dummy_hcd queue

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 16 Dec 2011, Sebastian Andrzej Siewior wrote:

> So you don't have to go that long back. Unless I'm mistaken ANSI C
> defines unsigned integer to be in range from 0..65535 which makes it
> 16bit. It got extended by C90 or C99 which also introduced long long
> (my memory may not be completely correct here).
> If you take any piece of serious software and use on that compiler it
> will blow up because most people assume an integer is 32bit and you end 
> up with overflows.
> 
> So what should I learn from this Alan? If the variable I'm using should
> hold values in the range 0..65535 I should use unsigned int. and if I
> plan anything in the range 0..4294967295 I shall use u32?

Well, Linux does have certain requirements, even if they aren't written
down explicitly anywhere.  Nobody expects to run Linux in a 16-bit
environment.  Nor does anyone expect to build Linux in an environment 
where the C compiler doesn't support a bunch of GNU extensions.

So if you plan to use numbers that won't range above 4294967295 then 
unsigned int is fine.  If you know that the value must always occupy 
exactly 32 bits, then u32 is appropriate -- but this is more like a 
statement about the memory location where the value is stored than 
about the value itself.

On the other hand, if you know you will be working with values larger 
than 4294967295, then unsigned int is not appropriate.  Neither is 
unsigned long, despite the fact that it might be sufficient for your 
needs on a 64-bit CPU (exception: if you're writing code that is 
specifically meant only for 64-bit systems).

The general rule is simple.  Unless you have a good reason not to,
always use int or unsigned int.  Good reasons include things like: the
value must occupy a certain amount of space, or the value is passed by
reference to a routine that expects a particular type, or int/unsigned
int is too small on some supported systems to fulfill your needs.  In
the first case, use a type that specifies the size expliclty (like
u32); in the second case, use the type expected; in the third case, use
the smallest type that is sufficiently large on all supported systems.

Alan Stern

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux