1
typedef intptr_t        ngx_int_t; 
typedef uintptr_t       ngx_uint_t; 
typedef intptr_t        ngx_flag_t;

What can we benifit from this ?I can't think of one to be honest...

The above code are from the famous nginx project,check it if interested.

DriverBoy
  • 937
  • 1
  • 11
  • 23

5 Answers5

1

One of the typedef purposes is portability. E.g. different compilers and platforms have various type sizes, e.g. sizeof(int) on x86, Linux, gcc is not the same as on Texas Instrument's processors :) But it's still int.

So,

typedef int INT32 

saves one when porting the code.

Another purpose of typedef, is to declare types in order to make shorter declarations.

typedef sharted_ptr<MyClass> MyClassPtr;

And now, you can use MyClassPtr as a type, instead of writing the whole shared_ptr... string.

And the very common usage of typedef with structures:

typedef struct {
   int x;
   int y;
} Point;

or

struct point {
   int x;
   int y;
}

typedef struct point Point;

Both typedefs let you avoid typing struct keyword every time.

Zaur Nasibov
  • 22,280
  • 12
  • 56
  • 83
  • @Zaur Nasibov,how can `typedef int INT32` saves one when porting the code? – DriverBoy May 26 '11 at 09:30
  • 2
    Because if the code requires a 32-bit int, but you're compiling on a platform where int is, say, 16 bits, you can just change the typedef to 'typedef long INT32' and it'll all work without having to change all occurances of int in the code. – David Given May 26 '11 at 09:44
  • There are int types with fixed sizes in C99, so if you are using C99, use those instead of your own typedefs. – Lundin May 26 '11 at 09:51
  • 2
    @Lundin, If you're looking for portability, then don't assume that you're on C99 with fixed size types. – BMitch May 26 '11 at 11:06
  • @David - If you are porting to a 16 bit machine you are much less likely to need that many 32-bit ints. How does the typedef help me select those? – Bo Persson May 26 '11 at 19:05
  • @Bo: well, the programmer obviously needs to be aware which bits of the code use numbers that need a 32-bit int and use the INT32 type for those. By using the typedef'd name, it's much easier to reconfigure your types after the fact --- the type of int is fixed by the compiler, but the type of INT32 is determined by you. – David Given May 27 '11 at 08:44
  • 1
    @Lundin: actually, if you're not on C99, then the C99 types are trivial to define yourself. These days there no reason *not* to use them in all code. – David Given May 27 '11 at 08:45
  • @B Mitch If you aren't on C99, you typedef them yourself to the names used in C99. Ie use "uint8_t" rather than "My_own_little_uint8", no matter which standard the compiler uses. – Lundin May 27 '11 at 12:39
1

It's often done for code portability, and is particularly relevant for embedded systems.

Suppose you have some integer values that absolutlely MUST be 32-bits long. Maybe they need to map to network/disk structures, maybe they need to hold values of that magnitude, or whatever.

Now, suppose you develop your code on a compiler where 'int' is 32 bits. You write...

struct s {
 int a,b,c,d;
}

...and it works fine. But, if you need to switch to a compiler where int is only 16-bits, but long is 32, you would need to change all those declarations to

struct s {
 long a,b,c,d;
}

Worse yet, you can't do just search/replace, because some of the 'ints' you probably don't care about the size. So, the best approach is to to this:

typedef long INT32; // change this typedef according to compiler

struct s {
 INT32 a,b,c,d;
}

Then, all you need to is change the typedefs.

Roddy
  • 66,617
  • 42
  • 165
  • 277
0

I know two reasons:

  • Aliasing, turning complex declaration something simpler
  • Portability, at different architecture, a type could be differently just, as very simple example: u32, where at some places could be defined as unsigned int, other unsigned long type.
Pih
  • 2,282
  • 15
  • 20
0

The reason could be that they wish to change the pointer type when porting the code. On another system, there might be different addressing modes ("banking" etc), and then they might need to use non-standard syntax, like

typedef far intptr_t        ngx_int_t; 

If they never port the code to any system with more than one addressing mode on the same system, portability of pointers would never be an issue and the typedef would be redundant.

Lundin
  • 195,001
  • 40
  • 254
  • 396
-1

One of the reason I have seen people do this is that they think the if there is a need to change the actual type they just have to change the typedef. I am not too convined about that argument though.

Naveen
  • 74,600
  • 47
  • 176
  • 233