Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is this #define legal?

Reply
Thread Tools

Is this #define legal?

 
 
Sensei
Guest
Posts: n/a
 
      08-21-2006
Hi! I'm concerned about the legality of such a definition:

#define funcX funcY

where funcX belongs to the *standard* C functions. Is it legal to do
this? The standard says "any function declared in a header may be
additionally implemented as a macro defined in the header, so a library
function should not be declared explicitly if its header is included".

Is this applicable to standard functions? And, what if the definition
is outside the header where the funcY function is defined, and what if
I use it in a source file?

Thanks!

--
Sensei <>

The optimist thinks this is the best of all possible worlds.
The pessimist fears it is true. [J. Robert Oppenheimer]

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-21-2006
Sensei wrote:

> Hi! I'm concerned about the legality of such a definition:
>
> #define funcX funcY
>
> where funcX belongs to the *standard* C functions. Is it legal to do
> this? The standard says "any function declared in a header may be
> additionally implemented as a macro defined in the header, so a library
> function should not be declared explicitly if its header is included".


The program may fail to compile if the appropriate header
has already #define'd funcX and your funcY does not happen to
match the existing definition perfectly. For example

#include <string.h>
#define strlen my_strlen

may fail, because <string.h> may already contain something like

size_t strlen(const char*); /* ordinary function */
#define strlen _builtin_strlen /* compiler magic */

You could avoid that potential problem:

#include <string.h>
#undef strlen /* just in case */
#define strlen my_strlen

> Is this applicable to standard functions?


Yes: the section of the Standard you quoted concerns the
standard library functions.

> And, what if the definition is
> outside the header where the funcY function is defined, and what if I
> use it in a source file?


I'm sorry; I do not understand your questions. In the first
part you seem to be talking of two different kinds of "definition,"
and I'm not sure what each refers to. "Use it in a source file" is
also confusing: First, it's not clear what "it" means, and second,
where but in a source file could you use *any* C construct?

--
Eric Sosman
lid


--
Eric Sosman
lid
 
Reply With Quote
 
 
 
 
Harald van =?UTF-8?B?RMSzaw==?=
Guest
Posts: n/a
 
      08-21-2006
Eric Sosman wrote:
> Sensei wrote:
>
>> Hi! I'm concerned about the legality of such a definition:
>>
>> #define funcX funcY
>>
>> where funcX belongs to the *standard* C functions. Is it legal to do
>> this? The standard says "any function declared in a header may be
>> additionally implemented as a macro defined in the header, so a library
>> function should not be declared explicitly if its header is included".

>
> The program may fail to compile if the appropriate header
> has already #define'd funcX and your funcY does not happen to
> match the existing definition perfectly. For example
>
> #include <string.h>
> #define strlen my_strlen
>
> may fail, because <string.h> may already contain something like
>
> size_t strlen(const char*); /* ordinary function */
> #define strlen _builtin_strlen /* compiler magic */
>
> You could avoid that potential problem:
>
> #include <string.h>
> #undef strlen /* just in case */
> #define strlen my_strlen


That's not allowed in standard C. It's covered by 7.1.3p2:
"No other identifiers are reserved. If the program declares or defines an
identifier in a context in which it is reserved (other than as allowed by
7.1.4), or defines a reserved identifier as a macro name, the behavior is
undefined."
And more practically, defining a macro with the same name as a standard
library function can break other macros which try to call the standard
function.

>> Is this applicable to standard functions?

>
> Yes: the section of the Standard you quoted concerns the
> standard library functions.
>
>> And, what if the definition is
>> outside the header where the funcY function is defined, and what if I
>> use it in a source file?

>
> I'm sorry; I do not understand your questions. In the first
> part you seem to be talking of two different kinds of "definition,"
> and I'm not sure what each refers to. "Use it in a source file" is
> also confusing: First, it's not clear what "it" means, and second,
> where but in a source file could you use *any* C construct?


I'm confused as well, the best I can come up with is:

x1.c:
#define printf dummy
extern void printf(void);
int main(void) {
printf();
}

x2.c:
#include <stdio.h>
void dummy(void) {
puts("Hello, world!");
}

If this is meant, I don't see anything disallowing it. It's extremely poor
style, but not invalid.
 
Reply With Quote
 
Sensei
Guest
Posts: n/a
 
      08-21-2006
On 2006-08-21 14:36:05 +0200, Harald van Dijk <> said:

> That's not allowed in standard C. It's covered by 7.1.3p2:
> "No other identifiers are reserved. If the program declares or defines an
> identifier in a context in which it is reserved (other than as allowed by
> 7.1.4), or defines a reserved identifier as a macro name, the behavior is
> undefined."
> And more practically, defining a macro with the same name as a standard
> library function can break other macros which try to call the standard
> function.


Can you clarify the statement above? Read next:

>> I'm sorry; I do not understand your questions. In the first
>> part you seem to be talking of two different kinds of "definition,"
>> and I'm not sure what each refers to. "Use it in a source file" is
>> also confusing: First, it's not clear what "it" means, and second,
>> where but in a source file could you use *any* C construct?


> I'm confused as well, the best I can come up with is:
>
> x1.c:
> #define printf dummy
> extern void printf(void);
> int main(void) {
> printf();
> }
>
> x2.c:
> #include <stdio.h>
> void dummy(void) {
> puts("Hello, world!");
> }
>
> If this is meant, I don't see anything disallowing it. It's extremely poor
> style, but not invalid.


Yes, That's what I meant, using the #define inside a source file just
the way you wrote. I know it's poor, but it's what I found

Clarifying my concerns, I have a non standard C library that hasn't all
the functions it should, in the example code I found that since, for
instance, they have no printf or strlen, they use precompiler magic:

#include <stdio.h>

#define printf _sdk_PRINT_TO_FOO
#define strlen _sdk_STRING_LENGTH_BAR

int main(void)
{
printf("Hello, world!\n");
return 0;
}

The #defines are *inside* the application code, not in the libc. The
(pseudo)libc code has _sdk_PRINT_TO_FOO and _sdk_STRING_LENGTH_BAR
correclty (I hope) working though. I found this source quite weird, and
I didn't know about the legality of such definitions.


--
Sensei <>

The optimist thinks this is the best of all possible worlds.
The pessimist fears it is true. [J. Robert Oppenheimer]

 
Reply With Quote
 
Harald van =?UTF-8?B?RMSzaw==?=
Guest
Posts: n/a
 
      08-21-2006
Sensei wrote:
> Clarifying my concerns, I have a non standard C library that hasn't all
> the functions it should, in the example code I found that since, for
> instance, they have no printf or strlen, they use precompiler magic:
>
> #include <stdio.h>
>
> #define printf _sdk_PRINT_TO_FOO
> #define strlen _sdk_STRING_LENGTH_BAR
>
> int main(void)
> {
> printf("Hello, world!\n");
> return 0;
> }
>
> The #defines are *inside* the application code, not in the libc. The
> (pseudo)libc code has _sdk_PRINT_TO_FOO and _sdk_STRING_LENGTH_BAR
> correclty (I hope) working though. I found this source quite weird, and
> I didn't know about the legality of such definitions.


That's not allowed in standard C, but since your implementation isn't a
conforming one, it's not really relevant what the standard says, just what
works. It would probably be a good idea to hide that in a
#ifndef __STDC__ / ... / #endif
block, though.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-21-2006
Eric Sosman <> writes:
> Sensei wrote:
>> Hi! I'm concerned about the legality of such a definition:
>> #define funcX funcY
>> where funcX belongs to the *standard* C functions. Is it legal to do
>> this? The standard says "any function declared in a header may be
>> additionally implemented as a macro defined in the header, so a
>> library function should not be declared explicitly if its header is
>> included".

>
> The program may fail to compile if the appropriate header
> has already #define'd funcX and your funcY does not happen to
> match the existing definition perfectly. For example
>
> #include <string.h>
> #define strlen my_strlen
>
> may fail, because <string.h> may already contain something like
>
> size_t strlen(const char*); /* ordinary function */
> #define strlen _builtin_strlen /* compiler magic */


A quibble: if <string.h> defined strlen as a macro, it must be a
function-like macro, such as:
#define strlen(s) _builtin_strlen(s)
so that a program can call
(strlen)(s)
to call the actual function.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Sensei
Guest
Posts: n/a
 
      08-22-2006
On 2006-08-21 20:04:35 +0200, Harald van Dijk <> said:

> Sensei wrote:
>> Clarifying my concerns, I have a non standard C library that hasn't all
>> the functions it should, in the example code I found that since, for
>> instance, they have no printf or strlen, they use precompiler magic:
>>
>> #include <stdio.h>
>>
>> #define printf _sdk_PRINT_TO_FOO
>> #define strlen _sdk_STRING_LENGTH_BAR
>>
>> int main(void)
>> {
>> printf("Hello, world!\n");
>> return 0;
>> }
>>
>> The #defines are *inside* the application code, not in the libc. The
>> (pseudo)libc code has _sdk_PRINT_TO_FOO and _sdk_STRING_LENGTH_BAR
>> correclty (I hope) working though. I found this source quite weird, and
>> I didn't know about the legality of such definitions.

>
> That's not allowed in standard C, but since your implementation isn't a
> conforming one, it's not really relevant what the standard says, just what
> works. It would probably be a good idea to hide that in a
> #ifndef __STDC__ / ... / #endif
> block, though.


So, to make the library more standard-adherent I have to modify its
code to provide the ISO functions myself by any allowed means. Is that
right?

--
Sensei <>

The optimist thinks this is the best of all possible worlds.
The pessimist fears it is true. [J. Robert Oppenheimer]

 
Reply With Quote
 
Sensei
Guest
Posts: n/a
 
      08-22-2006
On 2006-08-21 22:31:54 +0200, Keith Thompson <kst-> said:

> A quibble: if <string.h> defined strlen as a macro, it must be a
> function-like macro, such as:
> #define strlen(s) _builtin_strlen(s)
> so that a program can call
> (strlen)(s)
> to call the actual function.


Keith, a code like the following

#include <stdio.h>

#define printf _sdk_PRINT_TO_FOO
#define strlen _sdk_STRING_LENGTH_BAR

int main(void)
{
printf("Hello, world!\n");
return 0;
}

is illegal, right? See the post from Harald van Dijk.

Thanks to anyone!

--
Sensei <>

The optimist thinks this is the best of all possible worlds.
The pessimist fears it is true. [J. Robert Oppenheimer]

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-22-2006
Sensei <> writes:
> On 2006-08-21 22:31:54 +0200, Keith Thompson <kst-> said:
>> A quibble: if <string.h> defined strlen as a macro, it must be a
>> function-like macro, such as:
>> #define strlen(s) _builtin_strlen(s)
>> so that a program can call
>> (strlen)(s)
>> to call the actual function.

>
> Keith, a code like the following
>
> #include <stdio.h>
>
> #define printf _sdk_PRINT_TO_FOO
> #define strlen _sdk_STRING_LENGTH_BAR
>
> int main(void)
> {
> printf("Hello, world!\n");
> return 0;
> }
>
> is illegal, right? See the post from Harald van Dijk.


Yes, I think so, but that's not what I was talking about.

A program may not define strlen as a macro, but the implementation
(particularly the <string.h> header) is permitted to do so.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      08-22-2006
2006-08-21 <>,
Keith Thompson wrote:
> Eric Sosman <> writes:
>> Sensei wrote:
>>> Hi! I'm concerned about the legality of such a definition:
>>> #define funcX funcY
>>> where funcX belongs to the *standard* C functions. Is it legal to do
>>> this? The standard says "any function declared in a header may be
>>> additionally implemented as a macro defined in the header, so a
>>> library function should not be declared explicitly if its header is
>>> included".

>>
>> The program may fail to compile if the appropriate header
>> has already #define'd funcX and your funcY does not happen to
>> match the existing definition perfectly. For example
>>
>> #include <string.h>
>> #define strlen my_strlen
>>
>> may fail, because <string.h> may already contain something like
>>
>> size_t strlen(const char*); /* ordinary function */
>> #define strlen _builtin_strlen /* compiler magic */

>
> A quibble: if <string.h> defined strlen as a macro, it must be a
> function-like macro, such as:
> #define strlen(s) _builtin_strlen(s)
> so that a program can call
> (strlen)(s)
> to call the actual function.


I think it's allowed to #define strlen _builtin_strlen, provided that
_builtin_strlen is itself a real function
 
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




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57