#define
does not affect the binary or object files in any way, because it is a preprocessor command that basically copy-pastes stuff.
typedef
is processed by the compiler, so I am wondering in what capacity it may affect the final binary/object files, and if by using it, one uses up more resources compared to #define
.
My expectation is that typedef
works something like this:
int (*)[3]
, it has some structure that, for example, means “this type is a pointer, and here is further information on what it points to,” and that further information says “this type is an array of 3 elements, and here is further information on the element type,” and that further information says “this type is an int
.”typedef
, it memorizes the type information (it saves the data structures described above) and associates it with the identifier declared as the type alias.So the end result is the same as if the original type in the typedef
had been used again everywhere the identifier appears (adjusting C rules to match, such as rules about defining or not defining structure types multiple times).
However, that is just for the activity of the compiler needed for generating object code (or intermediate code which later becomes object code). But the compiler does more than that; it also records information for debugging. You say “Preprocessor macros are like copy paste, and it was like they were never there,” but this is not quite true; a compiler may record information about macros so that it can produce better diagnostic information, helping the user understand how macro replacement led to a particular error. You can see this in some Clang error messages. Similarly, while the compiler may replace a typedef
name with the type information, it might also keep some information and include it in the output files to help with debugging.