I'm doing some network programming in Linux at the moment and for this I made myself some typdefs for portability
typedef char int8;
typedef unsigned char uint8;
typedef short int16;
typedef unsigned short uint16;
typedef int int32;
typedef unsigned int uint32;
Now I'm at a point where I want to write some wrapper functions for often used socket functions. A short example would be:
int32 rawSocketCreate(int* sockfd, int protocol) {
*sockfd = socket(AF_PACKET, SOCK_RAW, htons(protocol));
if (*sockfd == -1) {
printf("Error creating raw socket\n");
return -1;
}
return 0;
}
My question now is: For parameters I pass on to library functions, should I keep using the standard data types or also use my own?
rawSocketCreate(int* sockfd, int protocol)
/* OR */
rawSocketCreate(int32* sockfd, int32 protocol)
C pet peeve... what is wrong with <stdint.h>
and the standard typedefs it provides? Everybody seems to use typedef's of his own invention, but virtually no-one seems to use the standard include (which comes with proper macros for printf()
and scanf()
handling etc.)...
If an API call expects an int
, you should pass it an int
, without making assumptions about the native width of said int
(because assumptions tend to break in the most unfortunate moments).
Use defined-width typedefs if the exact width matters, only. (For example cross-platform binary compatibility of struct layouts, hardware drivers, ...)