Reputation: 995
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.
Upvotes: 1
Views: 700
Reputation: 215350
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.
Upvotes: 0
Reputation: 68114
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.
Upvotes: 1
Reputation: 22679
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.
Upvotes: 1
Reputation: 73503
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.
Upvotes: -1
Reputation: 2268
I know two reasons:
Upvotes: 0