Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: struts struct
While browsing some code on net I came across #define macro used inside structure:
For example:
 
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 15-Apr-13 9:07am
Edited 15-Apr-13 9:27am
skydger4.8K
v2
Comments
Sergey Alexandrovich Kryukov at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 
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
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.
  Permalink  
Comments
rupeshkp728 at 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 at 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 at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 295
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 174
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100