Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
While browsing some code on net I came across #define macro used inside structure:
For example:

C++
typedef struct
{
#define XYZ                 \
   LONG           x;         \
   INT            y;        \
   CHAR           z;

   XYZ

} Data;


I guess it does not matter whether the macro is defined inside the structure or outside.
What is the benefit of declaring structure members through a macro?
Posted
Updated 15-Apr-13 8:27am
v2
Comments
Sergey Alexandrovich Kryukov 15-Apr-13 14:31pm    
Sounds complete gibberish to me. Preprocessor is evil.
And please, try to ask the questions only your own code. You could bring enormous amount of trash from the Wen, but why?
—SA

1 solution

It has "advantages" if you are going to include the same data in a number of different structures, and want to declare the common data in a single place - but generally speaking that is a poor way to do things anyway.

The only real "use" of this is when you want to define a set of related structures:

C#
typedef struct
{
#define XYZ                 \
   LONG           x;         \
   INT            y;        \
   CHAR           z;

   XYZ
} Base;

typedef struct
{
   XYZ
   int            Count;
} Derived;
To emulate inheritance in more OOP oriented languages, where things like
C++
typedef struct
{
   LONG           x;         
   INT            y;        
   CHAR           z;
} Base;
 
typedef struct
{
   Base           A;
   int            Count;
} Derived;
Could cause following data to be aligned differently (as the structure size will be rounded up to a machine word). But I'd want it well and truly commented before I let it into my code base.

But for general use, it's a waste of time, and confusing to read.
 
Share this answer
 
Comments
rupeshkp728 15-Apr-13 14:55pm    
Thanks Griff for the reply.
That OOP example clears my doubt.
The intel code is also trying to implement some oop(base/derived) kind of structure, but using macros this way is really confusing.
OriginalGriff 15-Apr-13 14:58pm    
It is indeed - but it is slightly better than declaring the same variables in eight places, then forgetting to modify one of them when you make a change...:laugh:

That's why I'd want it well commented!
rupeshkp728 15-Apr-13 15:05pm    
But the same can also be acheived by using a common struct like "Base" in your example instead of using a macro.
Will using macro instead of a common struct make any difference?
OriginalGriff 15-Apr-13 15:16pm    
It can, yes. The size of a structure is not just the sum of the bytes in the variables it contains - it is always padded to align on machine words. So if you define three characters in a structure on a 16 bit system, the structure is 4 bytes long. Same for a 32 bit system, and 8 bytes for a 64 bit OS. If you include an instance of the padded base structure as part of the derived structure, you do not end up with the same result as if you had included all the variables individually. This can have nasty effects if you expect two different applications (or even teams working on the same application) using the same base structure to interact correctly.

Including them via a #DEFINE means that no such padding happens.
CPallini 15-Apr-13 15:37pm    
Good point. I didn't consider that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900