why typedef ?

J

junky_fellow

In most of the codes that i have seen,
i observed the following typedefs.

typedef unsigned int t_uint32
typedef int t_sint32
typedef unsigned char t_uint8
typedef char t_sint8

what is the purpose of doing all that ?
i understand that its quicker to write t_uint32 instead of unsigned int,
and the code becomes more readable.
but is there any other reason for using these typedefs ?

In some of the articles i read that these typedefs make the code
portable, but i couldn't understand how ?

can i write a portable code without using all these typedefs ?

thanx in advance for any help.....
 
R

Ravi Uday

junky_fellow said:
In most of the codes that i have seen,
i observed the following typedefs.

typedef unsigned int t_uint32
typedef int t_sint32
typedef unsigned char t_uint8
typedef char t_sint8

what is the purpose of doing all that ?
i understand that its quicker to write t_uint32 instead of unsigned int,
and the code becomes more readable.
but is there any other reason for using these typedefs ?

Helps in porting of applications to different architecture set.
i.e for example - at some point of time if you come to know that 'unsigned
int' is now of 16 bits than 32 bits, and 'short' is 32 bit than 16bits !!!
then there will be less headache in your code (minimal code
rewrites/changes) all you have to do now is to redeclare the typedef
keeping t_uint32 as is.
typedef short t_uint32;

This was just one use i thought, others in this group might be more clearer
with other examples/usages.
In some of the articles i read that these typedefs make the code
portable, but i couldn't understand how ?

can i write a portable code without using all these typedefs ?
The whole purpose of this typedef is to make your life easier so use
them..
 
J

Joona I Palaste

junky_fellow said:
In most of the codes that i have seen,
i observed the following typedefs.
typedef unsigned int t_uint32
typedef int t_sint32
typedef unsigned char t_uint8
typedef char t_sint8
what is the purpose of doing all that ?
i understand that its quicker to write t_uint32 instead of unsigned int,
and the code becomes more readable.
but is there any other reason for using these typedefs ?
In some of the articles i read that these typedefs make the code
portable, but i couldn't understand how ?

It makes the code more portable by abstracting away the underlying
types. You now have types that can suit a particular requirement, no
matter what concrete types satisfy that requirement. You can use the
types "t_uint32" etc. in thousands of places in your program, and if
you should ever encounter a platform where an unsigned 32-bit integer
is not unsigned int, all you have to change is one line.
can i write a portable code without using all these typedefs ?

Yes, but it will be much more tiresome.
thanx in advance for any help.....

You're welcome.
 
R

Richard Bos

In most of the codes that i have seen,
i observed the following typedefs.

typedef unsigned int t_uint32
typedef int t_sint32
typedef unsigned char t_uint8
typedef char t_sint8

what is the purpose of doing all that ?

Laziness and a misplaced idea of "being low-level", I guess. Uusually
Nothing rational.
Exception: if this code is used for communication between programs which
might be compiled with different integer sized. In that case, an #if'ed
sequence of #define <type_for_this_implementation> <required_size_type>
is useful. For single-program use, not.

Richard
 
E

Erik de Castro Lopo

junky_fellow said:
In most of the codes that i have seen,
i observed the following typedefs.

typedef unsigned int t_uint32
typedef int t_sint32
typedef unsigned char t_uint8
typedef char t_sint8

what is the purpose of doing all that ?

Those typedefs are pretty braindead.
i understand that its quicker to write t_uint32 instead of unsigned int,
and the code becomes more readable.
but is there any other reason for using these typedefs ?

In some of the articles i read that these typedefs make the code
portable, but i couldn't understand how ?

It does become more portable if you use the 1999 ISO C standard
types which are defined in stdint.h, name int8_t, uint8_t etc.
can i write a portable code without using all these typedefs ?

As long as you don need portability across everything
from supercomputers to micro controllers then yes. If
you pick a subset like "All 32 bit OSes" then its
relatively easy.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo (e-mail address removed) (Yes it's valid)
+-----------------------------------------------------------+

GPLG
GPLGPLGP
GPLGPLGPLGP
GPLGP
GPL MICROSOFT
GPLGP
GPLGPLGPLGP
GPLGPLGPL
GPLGPL
 
S

sathya_me

junky_fellow said:
In most of the codes that i have seen,
i observed the following typedefs.

typedef unsigned int t_uint32
typedef int t_sint32
typedef unsigned char t_uint8
typedef char t_sint8

what is the purpose of doing all that ?
i understand that its quicker to write t_uint32 instead of unsigned int,
and the code becomes more readable.
but is there any other reason for using these typedefs ?

In some of the articles i read that these typedefs make the code
portable, but i couldn't understand how ?

can i write a portable code without using all these typedefs ?

thanx in advance for any help.....

Click this links:


http://groups.google.com/[email protected]&frame=off


http://groups.google.com/groups?hl=...off&[email protected]&rnum=3

http://groups.google.com/groups?hl=...off&[email protected]&rnum=9


http://groups.google.com/groups?hl=...off&[email protected]&rnum=2

http://groups.google.com/[email protected]#link10

[More if you type "typedef" in google's comp.lang.c search]


--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)
 
J

junky_fellow

Joona I Palaste said:
It makes the code more portable by abstracting away the underlying
types. You now have types that can suit a particular requirement, no
matter what concrete types satisfy that requirement. You can use the
types "t_uint32" etc. in thousands of places in your program, and if
you should ever encounter a platform where an unsigned 32-bit integer
is not unsigned int, all you have to change is one line.


Yes, but it will be much more tiresome.


You're welcome.

thank you all for the useful tips and help...
 
M

Malcolm

junky_fellow said:
typedef unsigned int t_uint32
typedef int t_sint32
typedef unsigned char t_uint8
typedef char t_sint8

what is the purpose of doing all that ?
It says that this variable must be exactly 8 or 32 bits wide.
i understand that its quicker to write t_uint32 instead of unsigned int,
and the code becomes more readable.
but is there any other reason for using these typedefs ?
Speed of typing is an extremely poor reason for doing this. The main reason
is if the size of underlying types changes when code is ported.
In some of the articles i read that these typedefs make the code
portable, but i couldn't understand how ?
Let's say we're storing 32 bit RGBA images. If we move to a platform where
"long" is 64 bits, our code will work, but will waste half the memory. Since
images are often very large, this may not be acceptable. However if we use
t_unint32, and simply redefine it as an int (ints being 32 bits, lets
suppose), the code should be fine.
can i write a portable code without using all these typedefs ?
Usually the places you need a fixed size are few and far between. Usually
you only need a minimum size, and this is given by the C standard. So your
code shouldn't be littered with defined types. The exception is when a third
party has chosen to do just that, often to make it harder to port platforms
to other platforms, or because psychologically it says "my library is so
important it defines even the basic types you use", or occasionally because
the code isn't written in C and there is a genuine technical reason. If you
are using a library that makes heavy use of defined types it is reasonable
to do likewise.
 
B

boa

Malcolm said:
Let's say we're storing 32 bit RGBA images. If we move to a platform where
"long" is 64 bits, our code will work, but will waste half the memory. Since
images are often very large, this may not be acceptable. However if we use
t_unint32, and simply redefine it as an int (ints being 32 bits, lets
suppose), the code should be fine.

Another reason may be that you share data between platforms, then it may
be necessary to used fixed size datatypes.

boa@home
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top