Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C Macro define contain symbol #

Reply
Thread Tools

C Macro define contain symbol #

 
 
Wang WolfLouis
Guest
Posts: n/a
 
      09-13-2011
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.
 
Reply With Quote
 
 
 
 
Noob
Guest
Posts: n/a
 
      09-13-2011
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-questions.html

$ 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)
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      09-13-2011
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Roberto Waltman
Guest
Posts: n/a
 
      09-13-2011
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 ]
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      09-13-2011
"Eric Sosman" <(E-Mail Removed)> wrote in message
news:j4ngch$oqb$(E-Mail Removed)...
> 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

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      09-13-2011
On 09/13/2011 08:50 AM, BartC wrote:
> "Eric Sosman" <(E-Mail Removed)> wrote in message
> news:j4ngch$oqb$(E-Mail Removed)...
>> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-13-2011
Roberto Waltman <(E-Mail Removed)> 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) (E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
tom st denis
Guest
Posts: n/a
 
      09-13-2011
On Sep 13, 8:24*am, Roberto Waltman <(E-Mail Removed)> 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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-13-2011
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
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      09-13-2011
"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Roberto Waltman <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RCR: String#contain? and Array#contain? Roger Pack Ruby 3 09-28-2010 04:13 PM
Does string contain A, and if so, does a section of string contain B Jason Carlton Javascript 11 12-08-2009 06:07 PM
Error: symbol string may not contain `\0' Dylan Lukes Ruby 2 11-20-2009 03:10 PM
Does Perl 5.8.0 contain symbol Perl_sv_2pv? DirtyBear Perl Misc 3 07-15-2008 01:17 PM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM



Advertisments