[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

C Macro define contain symbol #

C Macro define contain symbol #

Wang WolfLouis

9/13/2011 9:48:00 AM

Dear all,

#define WIN32_PACKED #pragma pack(1)
I want to define a Macro to simpilify and unify my code for Windows
and Unix. The special character # is the key. anybody can give me some
suggesstion? Thanks.

Barry Briggs

9/13/2011 11:15:00 AM

Wang WolfLouis wrote:

> #define WIN32_PACKED #pragma pack(1)
> I want to define a Macro to simplify and unify my code for Windows
> and Unix. The special character # is the key. anybody can give me some
> suggestion?

http://catb.org/~esr/faqs/smart-ques...

$ cat cpp.c
#define WIN32_PACKED #pragma pack(1)
WIN32_PACKED

$ gcc -E cpp.c
# 1 "cpp.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "cpp.c"

#pragma pack(1)

Eric Sosman

9/13/2011 11:55:00 AM

On 9/13/2011 5:47 AM, Wang WolfLouis wrote:
> Dear all,
>
> #define WIN32_PACKED #pragma pack(1)
> I want to define a Macro to simpilify and unify my code for Windows
> and Unix. The special character # is the key. anybody can give me some
> suggesstion? Thanks.

Can't be done this way. For one thing, the # in the macro's
definition is an operator, with a different meaning than it would
have elsewhere. For another, the Standard says (6.10.3.4p3)

The resulting completely macro-replaced preprocessing token
sequence is not processed as a preprocessing directive even
if it resembles one [...]

So a macro expansion cannot generate a preprocessing directive.

However, the same paragraph continues

[...] but all pragma unary operator expressions within
it are then processed as specified in 6.10.9 below.

The "C99" version of the Standard introduced a _Pragma operator to
help with exactly this situation. You could try

#define WIN32_PACKED _Pragma("pack(1)")

to get the effect you want. "Try," I said, because _Pragma was new
in C99, and I have heard that Microsoft's C implementations (which
it looks like you might be using) are mostly stuck in the C90 era,
before _Pragma came along. It's worth a try, though.

... if you really think you need "pack(1)". Usually (not always,
but usually) that's the sign of a short-term hack leading to long-
term headaches.

--
Eric Sosman
esosman@ieee-dot-org.invalid

Roberto Waltman

9/13/2011 12:24:00 PM

Eric Sosman wrote:
> ... if you really think you need "pack(1)". Usually (not always,
>but usually) that's the sign of a short-term hack leading to long-
>term headaches.

Not when you are dealing with communication protocols, mapping
hardware control registers in embedded systems, etc.
Without the "pack" pragmas, instead of having a structure that
directly overlaps the desired memory layout, the packing/unpacking
would have to be in higher level software.
This is a much needed functionality, and I am puzzled why it never
became part of the language standard, instead of a not always
available compiler extension.
Even Pascal had packed records.
--
Roberto Waltman

[ Please reply to the group,
return address is invalid ]

Bartc

9/13/2011 12:51:00 PM

"Eric Sosman" <esosman@ieee-dot-org.invalid> wrote in message
news:j4ngch$oqb$1@dont-email.me...
> On 9/13/2011 5:47 AM, Wang WolfLouis wrote:

>> #define WIN32_PACKED #pragma pack(1)

> ... if you really think you need "pack(1)". Usually (not always,
> but usually) that's the sign of a short-term hack leading to long-
> term headaches.

I would guess that 'usually' it is to match a layout defined outside your
control. (In this case, perhaps because it's not practical to rewrite the
whole of Windows.)

--
Bartc

James Kuyper

9/13/2011 12:59:00 PM

On 09/13/2011 08:50 AM, BartC wrote:
> "Eric Sosman" <esosman@ieee-dot-org.invalid> wrote in message
> news:j4ngch$oqb$1@dont-email.me...
>> On 9/13/2011 5:47 AM, Wang WolfLouis wrote:
>
>>> #define WIN32_PACKED #pragma pack(1)
>
>> ... if you really think you need "pack(1)". Usually (not always,
>> but usually) that's the sign of a short-term hack leading to long-
>> term headaches.
>
> I would guess that 'usually' it is to match a layout defined outside your
> control. (In this case, perhaps because it's not practical to rewrite the
> whole of Windows.)

There's no need to rewrite Windows; just read the data into an array of
unsigned char, then unpack the array into each member of the structure
using memcpy(). Reverse the process when writing such data. I've written
such code far more often than I'd like to remember, and I wish C had
provided mechanisms that would allow me to avoid writing such code, but
it's not horribly difficult, either.
--
James Kuyper

Keith Thompson

9/13/2011 6:13:00 PM

Roberto Waltman <usenet@rwaltman.com> writes:
> Eric Sosman wrote:
>> ... if you really think you need "pack(1)". Usually (not always,
>>but usually) that's the sign of a short-term hack leading to long-
>>term headaches.
>
> Not when you are dealing with communication protocols, mapping
> hardware control registers in embedded systems, etc.
> Without the "pack" pragmas, instead of having a structure that
> directly overlaps the desired memory layout, the packing/unpacking
> would have to be in higher level software.
> This is a much needed functionality, and I am puzzled why it never
> became part of the language standard, instead of a not always
> available compiler extension.
> Even Pascal had packed records.

Yes, but Pascal packed records, as I recall, are merely a hint to the
compiler that space should be minimized. They're not necessarily
suitable for matching a specific externally imposed layout.

One example of a problem that would have to be solved:

struct foo {
char c;
int i;
};
#pragma pack(struct foo) /* or whatever the syntax is */

void some_func(int *p);

struct foo obj;
some_func(&obj.i);

I've worked on systems where an int* cannot contain an address that
isn't word-aligned. Do you forbid taking the address of a member of a
packed struct?

Ada has "pragma Pack", which is a hint to minimize space, *and* an
elaborate system of representation clauses that let you specify layout
down to the bit level.

C has unsigned char[], memcpy(), and bit masks.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Tom St Denis

9/13/2011 6:21:00 PM

On Sep 13, 8:24 am, Roberto Waltman <use...@rwaltman.com> wrote:
> Eric Sosman wrote:
> >     ... if you really think you need "pack(1)".  Usually (not always,
> >but usually) that's the sign of a short-term hack leading to long-
> >term headaches.
>
> Not when you are dealing with communication protocols, mapping
> hardware control registers in embedded systems, etc.

I write device drivers as part of my job (among other things) ... the
long-short of that is if you want a driver that has a hell of a chance
of being portable you don't use packing tricks or overlaying of
registers. If you're really tight on one platform then you can
deviate but then you're really heading down a nightmare of forked
code.

I've never used a pragma in any of my development efforts. If I need
to serialize some data I manually pack it using appropriate C
constructs (like shifting bits out of a long to get a stream of
bytes). Aliasing is also a big no-no and generally a sign of trouble
ahead.

Tom

Ian Collins

9/13/2011 7:55:00 PM

On 09/14/11 12:24 AM, Roberto Waltman wrote:
> Eric Sosman wrote:
>> ... if you really think you need "pack(1)". Usually (not always,
>> but usually) that's the sign of a short-term hack leading to long-
>> term headaches.
>
> Not when you are dealing with communication protocols, mapping
> hardware control registers in embedded systems, etc.
> Without the "pack" pragmas, instead of having a structure that
> directly overlaps the desired memory layout, the packing/unpacking
> would have to be in higher level software.
> This is a much needed functionality, and I am puzzled why it never
> became part of the language standard, instead of a not always
> available compiler extension.

Try using "pack(1)" on something like Sparc that does not allow
misaligned access....

--
Ian Collins

Kenneth Brody

9/13/2011 8:55:00 PM

On 9/13/2011 5:47 AM, Wang WolfLouis wrote:
> Dear all,
>
> #define WIN32_PACKED #pragma pack(1)
> I want to define a Macro to simpilify and unify my code for Windows
> and Unix. The special character # is the key. anybody can give me some
> suggesstion? Thanks.

As noted elsethread, a preprocessor directive cannot generate another
preprocessor directive.

However, just about all Windows implementations (and many *nix
implementations) I have seen include a set of #include files with the
necessary implementation-specific way of specifying structure packing.
Simply surround the code with a pair of #include's. For example:

#include <pshpack1.h>
/* Code that requires 1-byte packing. */
#include <poppack.h>

- or -

#include <pshpack2.h>
/* Code that requires 2-byte packing. */
#include <poppack.h>

And so on.

It's certainly not standard, but specifying such packing is necessarily
implementation-specific anyway, and this at least comes close to being
"portable" across a variety of platforms.

--
Kenneth Brody

Bartc

9/13/2011 11:53:00 PM

"Keith Thompson" <kst-u@mib.org> wrote in message
news:lnbouob9av.fsf@nuthaus.mib.org...
> Roberto Waltman <usenet@rwaltman.com> writes:

>> Even Pascal had packed records.
>
> Yes, but Pascal packed records, as I recall, are merely a hint to the
> compiler that space should be minimized. They're not necessarily
> suitable for matching a specific externally imposed layout.
>
> One example of a problem that would have to be solved:
>
> struct foo {
> char c;
> int i;
> };
> #pragma pack(struct foo) /* or whatever the syntax is */
>
> void some_func(int *p);
>
> struct foo obj;
> some_func(&obj.i);

> I've worked on systems where an int* cannot contain an address that
> isn't word-aligned. Do you forbid taking the address of a member of a
> packed struct?

Suppose *all* addresses have to be word-aligned (which used to be quite
common). Then you'd have the same problem with:

struct foo {
char c[2];
int i;
};

and trying to take the address of c[1] (or even just trying to access it).
(This assumes you don't just make char and int the same size.)

The Pascal 'packed' directive perhaps *was* an instructive to pack things
more tightly than hardware considerations would normally dictate.

--
Bartc


Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
C Macro define contain symbol #

Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te

Il servizio è completamente GRATUITO!