Re: advantage of using typedefs




junky_fellow@xxxxxxxxxxx wrote:
> I was looking at the source code of linux or open BSD. What I found
> that lots of typedefs
> were used. For example, consider the offset in a file. It was declared
> as
> off_t offset;
> and off_t is typedefed as
> typedef long off_t;
>
> I wanted to know what advantage do we get by typedefs ? Why we did not
> declare
> offset simply as
> long off_t;
>
> Similar is the case with size of file and so on.....
>
> Does any one has any clue of why do we use typedefs in such cases ?
>
> Thanx for any help advance ....

There are several reasons for doing this.

1. At some point in the future, a "long" may no longer be adequate for
representing offset values, and all offsets will need to be long long
(or some other type). By creating the typedef, you only need to make
that change in one place; i.e., change

typedef long off_t;

to

typedef long long off_t;

This is preferable to searching the source code for all offset
variables (which may or may not be named "offset") and changing their
definitions individually.

2. It allows you to implement the same types differently across
different platforms, depending on what's the most efficient or
practical for that platform. A 32-bit offset type would be wasteful on
a 16-bit platform, whereas it wouldn't be sufficient for a 64-bit
platform. Then you can have segmented architectures, were the offset
can be defined as a page number as well as displacement. Again, you
only have to make the change in one place, as opposed to finding and
changing every offset variable.

3. Sort of as a corollary to 2, it allows you to hide certain
implementation details from other programmers. You probably don't want
people making assumptions about the size of the offset available to
them, precisely because that value can change from platform to
platform.

.



Relevant Pages

  • Re: STL allocator for shared memory
    ... > Joe wrote: ... > contains an offset (using the allocator::pointer typedef). ...
    (microsoft.public.vc.language)
  • Object handle table position and typedef needed
    ... I need typedef and offset for HANDLE_TABLE from current process handle. ... I know these info on Windows XP and 2000, can you get me these information ... typedef struct _WIN2K_HANDLE_TABLE ...
    (microsoft.public.win32.programmer.kernel)
  • Re: time_t definition
    ... But, time_t is of type int32, from a typedef statement. ... typedef int zzz; ... We did not change it on the i386 platform. ...
    (freebsd-questions)
  • Re: advantage of using typedefs
    ... >the biggest possible unsigned integer to hold offset) then why not declare ... Because then its size would vary from platform to platform, ... c90 implementation it would be an error. ...
    (comp.lang.c)
  • PROBLEM: Compatibility problem with C++, i386 & ia64 platform
    ... unsigned long new, int size) ... On Itanium 2 platform: ... Compiling on Itanium 2 works if you add som typedefs and a define, which is normally defined when __KERNEL__ is enabled. ... typedef long s64; ...
    (Linux-Kernel)