which *int* ?

All off topic discussions go here. Everything from the funny thing your cat did to your favorite tv shows. Non-programming computer questions are ok too.

If you must have an unsigned integer of size 32 bits, how would you code it up?

u32int
4
6%
uint32_t
30
42%
u32int_t
0
No votes
uint32
12
17%
unsigned int (I don't care about portability)
8
11%
something else
17
24%
 
Total votes: 71

User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: which *int* ?

Post by Troy Martin »

char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long.

Portability is a waste of time for me. I don't plan on it at all.

I'm an x86 freak :mrgreen:
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: which *int* ?

Post by Brynet-Inc »

Troy Martin wrote:char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long.

Portability is a waste of time for me. I don't plan on it at all.

I'm an x86 freak :mrgreen:
And then you realize that even C compilers on the x86 can be inconsistent with integer sizes.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: which *int* ?

Post by Troy Martin »

Okay, then, I'm a GCC-i586 freak. Fixed!
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: which *int* ?

Post by Solar »

...in its current version. (Yes, I admit, it is unlikely they'll change it, but...)

This ain't about portability. IMHO, this goes much, much deeper.

You need an integer of exactly 32 bits. If you are the type that will simply check if int or long is good enough and then uses that deserves every bit of pain buggy code ever inflicts on him. And some more.

"int" might be 32 bit, but it does neither guarantee nor say so. "int" says "a whatever-sized integer value, I don't really care". Whereas int32_t says "a 32bit integer, and make it exactly 32 bits because it's important".

And as for using standards vs. rolling your own... sure there might be some advantages in driving on the wrong side of the road (such as, no-one in front of you to slow you down), but in the long run you aren't making friends that way...

Edit: That paragraph above about "saying so" refers to the documentation value of "int32_t" vs. "int", of course.
Last edited by Solar on Thu Sep 10, 2009 12:13 pm, edited 1 time in total.
Every good solution is obvious once you've found it.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: which *int* ?

Post by Troy Martin »

I don't like pain, can I change my vote? :wink:

Thanks, Solar, for that. I think I'm going to start #including <stdint.h> from now on and using whatevar's defined in thar.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
spere
Posts: 6
Joined: Mon Sep 07, 2009 1:51 pm
Location: Where ever my CBR600rr takes me..
Contact:

Re: which *int* ?

Post by spere »

Being a C# developer, it would be
uint
or
System.UInt32
:P didn't expect that one, did you?

Then again in masm i would use
VarName dw
mov ah, 4ch
int 21h
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: which *int* ?

Post by earlz »

spere wrote:Being a C# developer, it would be
uint
or
System.UInt32
:P didn't expect that one, did you?

Then again in masm i would use
VarName dw
ugh.. I hate C# in that respect.. Uint32. Just look at it. What an ugly name. It's inconsistent with every other language, and itself(where core types are lower case, such as int, float, object, string) Microsoft should've thought a little harder about that...
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: which *int* ?

Post by NickJohnson »

Solar wrote:...in its current version. (Yes, I admit, it is unlikely they'll change it, but...)

This ain't about portability. IMHO, this goes much, much deeper.

You need an integer of exactly 32 bits. If you are the type that will simply check if int or long is good enough and then uses that deserves every bit of pain buggy code ever inflicts on him. And some more.

"int" might be 32 bit, but it does neither guarantee nor say so. "int" says "a whatever-sized integer value, I don't really care". Whereas int32_t says "a 32bit integer, and make it exactly 32 bits because it's important".

And as for using standards vs. rolling your own... sure there might be some advantages in driving on the wrong side of the road (such as, no-one in front of you to slow you down), but in the long run you aren't making friends that way...

Edit: That paragraph above about "saying so" refers to the documentation value of "int32_t" vs. "int", of course.
If you think about it, why do compilers even have the types "int", "short", etc. anymore? It's not any faster, and it's not any simpler. Plus, I rarely have a circumstance where I don't care exactly how many bits I can use in an integer. If you want to make any remotely portable code, you're going to end up using int32_t and int16_t, or at least intmax_t, and that requires including an extra header.

Why doesn't the C standard simply force the compiler to have those types as builtins, instead of the normal integer types? Then people would write correct code *from the beginning*, without the argument of laziness for using normal types.

If I ever write a C compiler (and I'm planning to... eventually), I would more than happily throw "int" out the window. :twisted:
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: which *int* ?

Post by earlz »

NickJohnson wrote:
Solar wrote:...in its current version. (Yes, I admit, it is unlikely they'll change it, but...)

This ain't about portability. IMHO, this goes much, much deeper.

You need an integer of exactly 32 bits. If you are the type that will simply check if int or long is good enough and then uses that deserves every bit of pain buggy code ever inflicts on him. And some more.

"int" might be 32 bit, but it does neither guarantee nor say so. "int" says "a whatever-sized integer value, I don't really care". Whereas int32_t says "a 32bit integer, and make it exactly 32 bits because it's important".

And as for using standards vs. rolling your own... sure there might be some advantages in driving on the wrong side of the road (such as, no-one in front of you to slow you down), but in the long run you aren't making friends that way...

Edit: That paragraph above about "saying so" refers to the documentation value of "int32_t" vs. "int", of course.
If you think about it, why do compilers even have the types "int", "short", etc. anymore? It's not any faster, and it's not any simpler. Plus, I rarely have a circumstance where I don't care exactly how many bits I can use in an integer. If you want to make any remotely portable code, you're going to end up using int32_t and int16_t, or at least intmax_t, and that requires including an extra header.

Why doesn't the C standard simply force the compiler to have those types as builtins, instead of the normal integer types? Then people would write correct code *from the beginning*, without the argument of laziness for using normal types.

If I ever write a C compiler (and I'm planning to... eventually), I would more than happily throw "int" out the window. :twisted:

This is very true.. Why do they have int?

Well, I have one reason, is the speed for trivial small things like cycling through a loop that should never grow to more than 200. For it, you would of course want the fastest integer possible, which presumably is int. And to cycle through such a list should not involve a design decision as to if your going to limit this to 16 bits or 32 bits or 64 bits..

I think the int32_fast_t or whatever they are called types need to be utilized more often(possibly by a better and shorter naming scheme)

Other than the speed and memory issue and the pain of typing intptr_t, I have no problem with int forever being gone as a generic unknown type of death.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: which *int* ?

Post by Troy Martin »

earlz wrote:This is very true.. Why do they have int?
Cause generally, programmers that don't have a f**king clue about 32-bit integers versus 16-bit integers versus 64-bit ones etc. just use int in all their code cause "it's easier". Well, if I were a programmer that didn't need to deal with low-level stuffs like uint32_t and friends I'd just slap int everywhere without thinking about the bytes I could be saving by using short or char or the appropriate ?int*_t type.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: which *int* ?

Post by earlz »

Troy Martin wrote:
earlz wrote:This is very true.. Why do they have int?
Cause generally, programmers that don't have a f**king clue about 32-bit integers versus 16-bit integers versus 64-bit ones etc. just use int in all their code cause "it's easier". Well, if I were a programmer that didn't need to deal with low-level stuffs like uint32_t and friends I'd just slap int everywhere without thinking about the bytes I could be saving by using short or char or the appropriate ?int*_t type.
Well well... maybe programmers should be made more aware..

and when people make something like

Code: Select all

char *longstring;

...
int i;
for(i=0;i<strlen(longstring);i++){
   putc(longstring[i]);
}
and when they have "long" strings that take up 2gb of space(I'm sure it'll be possible and plausible one day) on their 64bit computer running Windows(where int is still 32bit) and wonder why their code either doesn't print the whole string or just utterly fails, and they wonder why it isn't working.. then They will finally realize it and suddenly it's Y2K all over again..

This could be much less of a problem though if we could safely assume sizeof(int)==sizeof(void*)
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: which *int* ?

Post by pcmattman »

Code: Select all

for(i=0;i<strlen(longstring);i++){
   putc(longstring[i]);
}
Any decent compiler (MSVC's, and GCC, in the least) will throw a warning about the conversion from unsigned to signed (strlen returns a size_t). So your example is invalid, because programmers are made aware that their code is faulty.
Cause generally, programmers that don't have a f**king clue about 32-bit integers versus 16-bit integers versus 64-bit ones etc. just use int in all their code cause "it's easier"
Actually, the reason why we "still have" int is for backwards compatibility (with the current standard, too). If you'd like to make a variant of C that doesn't have int, short, etc... - go ahead. But it's non-standard C. Just use stdint.h, which was introduced in C99.

The names of each types worked fine when C was first created, because you would be targeting a specific hardware platform with your code. Once portability became a concern steps were taken to make it possible to write portable code without breaking old code.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: which *int* ?

Post by Troy Martin »

Looks like what we need to do is tell the world that int and friends are deprecated and we should all use the wonders of platform independent variable types.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: which *int* ?

Post by earlz »

Troy Martin wrote:Looks like what we need to do is tell the world that int and friends are deprecated and we should all use the wonders of platform independent variable types.
I still want to implement a language where you do

Code: Select all

int:8 byte;
int:16 word;
int:21 wtf;
so you can have any size you want.. lol

edit:
Anyone know how to specify a 16bit number in C where int and long are 64 bits? (presumably short would be 32)

And any hope of getting a uint128_t anytime soon?
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: which *int* ?

Post by Troy Martin »

earlz wrote:Anyone know how to specify a 16bit number in C where int and long are 64 bits? (presumably short would be 32)
Oh come on earlz, read through the topic again! Use uint16_t! :mrgreen:
And any hope of getting a uint128_t anytime soon?
I think it's an unofficial extension on some compilers (likely MSVC) but as far as I know it's nonstandard. Might be standard in C1x, but as of the latest draft, I couldn't find anything about it. Googling it displays something about GCC 3.1 on the PowerPC having uint128_t.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
Post Reply