Author Topic: Help with C language macro stringification  (Read 18992 times)

0 Members and 2 Guests are viewing this topic.

Offline MarkS

  • Supporter
  • ****
  • Posts: 885
  • Country: us
Re: Help with C language macro stringification
« Reply #25 on: July 30, 2017, 08:43:44 pm »
Let's flip it around... What advantages does a macro offer over a proper C constant?

A macro defines a constant, i.e., the value doesn't change. To say this isn't a proper C constant is disingenuous.

These are both functionally identical, at least to the compiler, although potentially producing slightly different (and irrelevant) machine code:

Code: [Select]
#define some_constant 100
.
.
.
int array[some_constant];

Code: [Select]
enum{
some_constant = 100
};
.
.
.
int array[some_constant];

The difference is that one is evaluated during the preprocessor stage and one at compile time. Enums are more powerful yes, but that doesn't mean that the macros have no value. Ultimately, it comes down to familiarity. I started programming in C just shortly after enums came into the C vernacular, so #define is more familiar to me for this purpose. At the time all code examples used #define for constants; It's how I learned. One is different than the other, not one is good and the other bad. To be clear, most modern day compilers would produce exactly the same machine code from the above examples.

Now, when you get into C++, the preprocessor is evil and to be avoided lest your computer go up in flames. In C++, you use "const".
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Help with C language macro stringification
« Reply #26 on: July 30, 2017, 09:00:50 pm »
A macro defines a constant

it doesn't define anything
it replaces occurrences in the text.
It does the same job as SED.
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Help with C language macro stringification
« Reply #27 on: July 30, 2017, 09:02:31 pm »
we can agree the preprocessor is both well defined and standardized

it's not.
e.g. sierra cpp vs gnu cpp
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Help with C language macro stringification
« Reply #28 on: July 30, 2017, 09:24:44 pm »
Quote
The C standard is pretty specific about how expressions are evaluated

False. It's implementation dependent.

e.g. if you put functions inside an expression, the order in which functions are evaluated is known that might be different depending on the used C compiler.

The same happens with the macro processor for complex macros, typically when you try to define a function with a macro.

The C language is affected by "nasal demons". Undefined behaviors where the semantics of certain operations is described as undefined on the executing computer code whose behavior is not prescribed by the language specification to which the code adheres. This happens when the translator of the source code makes certain assumptions, but these assumptions are not satisfied during execution.

Since all the assumptions made by cpp come from a simple "text replacement", there is no information between these two modules, and, as consequence, nasal demons can take place without a single line of warning.

“When the compiler encounters [a given undefined construct] it is legal for it to make demons fly out of your nose”

Be warned about that.
 

Offline MarkS

  • Supporter
  • ****
  • Posts: 885
  • Country: us
Re: Help with C language macro stringification
« Reply #29 on: July 30, 2017, 09:34:32 pm »
A macro defines a constant

it doesn't define anything
it replaces occurrences in the text.
It does the same job as SED.

Don't be pedantic.
 

Offline MarkS

  • Supporter
  • ****
  • Posts: 885
  • Country: us
Re: Help with C language macro stringification
« Reply #30 on: July 30, 2017, 09:35:34 pm »
we can agree the preprocessor is both well defined and standardized

it's not.
e.g. sierra cpp vs gnu cpp

We're not talking about C++.
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Help with C language macro stringification
« Reply #31 on: July 30, 2017, 09:41:58 pm »
Don't be pedantic.

I assume you don't know what "define" implies!

type? size? kind? ... they are not optional since they can cause problems.

None of them are defined by #define. It's a good practice to force the size of a constant, but it's not a must. Thus people do what they want do to, and the source code is usually I mess.

 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3583
  • Country: ca
Re: Help with C language macro stringification
« Reply #32 on: July 30, 2017, 09:45:57 pm »
The C standard is also very specific about how the pre-processor work. In fact, it is more specific about the pre-processor than it is about typed expressions.

OK, so we can agree the preprocessor is both well defined and standardized. That doesn't mean it's a good idea to use macro substitutions for constants, although one certainly can.

Let's flip it around... What advantages does a macro offer over a proper C constant?

Pre-processor lets you do lots of different things. What you mean by "defining constants" is just a very small part of it. You can use macros as you see fit. It is nothing wrong with this. Same with "enum" - you can use it as you see fit and this is nothing wrong with this neither.

The purpose of macros and enums are different. Sometimes there could be a situation where you can use a macro or enum. Well, use either. It doesn't matter. Your programs won't get better if you always use enums in these situations. Nor are they going to get better if you decide not to use enums. These choices have nothing to do with the quality of the software you write. But, if you concentrate on algorithms and other tangible things instead of worrying about choices between macros and enums, your software will get better.

 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Help with C language macro stringification
« Reply #33 on: July 30, 2017, 10:00:42 pm »
We're not talking about C++.

SierraC is not C++

I compared a C compiler with another C compiler.

If you prefer you can see the difference between
- intelC
- Gcc/C
- Watcom C
- SierraC
- ...

Now don't want to make a point of distinguishing between
- implementation-defined
- unspecified
- undefined behavior
probably, in your head, they sound irrelevant like something that will never happen. Hope so, otherwise looking at several C compilers for business job to do, you might have some exciting news about your assumptions.
 

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Re: Help with C language macro stringification
« Reply #34 on: July 30, 2017, 10:24:11 pm »
Sometimes there could be a situation where you can use a macro or enum. Well, use either. It doesn't matter. Your programs won't get better if you always use enums in these situations. Nor are they going to get better if you decide not to use enums. These choices have nothing to do with the quality of the software you write. But, if you concentrate on algorithms and other tangible things instead of worrying about choices between macros and enums, your software will get better.

Actually, understanding how to use the language well is part of what makes code "better". Using the preprocessor macros for constants was OK 40 years ago, when the compiler didn't support constants. But there's no real utility in doing that today, and lots of drawbacks. Google basically outlaws preprocessor macros for their 100M+ line C++ codebase.

EDIT: More from Google on macros.
« Last Edit: July 30, 2017, 10:31:05 pm by andyturk »
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3583
  • Country: ca
Re: Help with C language macro stringification
« Reply #35 on: July 30, 2017, 11:05:58 pm »
Actually, understanding how to use the language well is part of what makes code "better".

Of course, you need to understand the language you use. But it is actually a very small and not important part of the programming process. If you come up with a good solution, you can write it in C, or in Pascal, or Whatever, and you will get very similar result. Even though C has macros (and I would probably use them in most cases), but Pascal doesn't (and I would get by without them).

Some languages, such as C# or java involve performance penalties at the outset. IMHO, it is silly to use them because you could just do the same in C with approximately the same effort, and results would obviously be better. But many people seem to be delusional about that - they think that using certain languages instead of others give them some sort of magical advantage.

Google. They certainly like imposing their rules on the world. Doesn't mean their rules are any good.

 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2862
  • Country: nz
Re: Help with C language macro stringification
« Reply #36 on: July 31, 2017, 12:08:03 am »
Google basically outlaws preprocessor macros for their 100M+ line C++ codebase.

EDIT: More from Google on macros.

If only the could outlaw paying their fair share of taxes as easily....

One benefits of #define is that it provides a path to share information between the build environment and the resulting code - usually the -D command line switch.



Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline John Coloccia

  • Super Contributor
  • ***
  • Posts: 1625
  • Country: us
Re: Help with C language macro stringification
« Reply #37 on: July 31, 2017, 01:01:21 am »
Google is talking C++ code, not C, and there are C++ ways to do things that don't require the use of a traditional macro.

const int i = 5;
int array;

is just one example that works in C++ and doesn't work in C for normal arrays.
 

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Re: Help with C language macro stringification
« Reply #38 on: July 31, 2017, 02:10:16 am »
One benefits of #define is that it provides a path to share information between the build environment and the resulting code - usually the -D command line switch.

Yup. Very handy. :-+

Google is talking C++ code, not C, and there are C++ ways to do things that don't require the use of a traditional macro.

const int i = 5;
int array;

is just one example that works in C++ and doesn't work in C for normal arrays.

You sure?

Code: [Select]
#include <stdio.h>

const int i = 10;
int array[i];

int main(int argc, char *argv[]) {
  printf("sizeof(array) = %lu\n", sizeof(array));
}

When I compile this on a mac using the C89 standard, it works just fine:

Code: [Select]
$ cc -std=c89 foo.c
$ ./a.out
sizeof(array) = 40
$ cc --version
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$

Heck, it even allows a const in in a case with -std=c89:

Code: [Select]
void foo(int arg) {
  switch (arg) {
  case i : break;
  default : break;
  }
}

So, if the C standard of 28 years ago allows for these kinds of constants, is there still a reason to be using #define for this stuff in 2017?
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2862
  • Country: nz
Re: Help with C language macro stringification
« Reply #39 on: July 31, 2017, 02:31:36 am »
One benefits of #define is that it provides a path to share information between the build environment and the resulting code - usually the -D command line switch.

Yup. Very handy. :-+

Google is talking C++ code, not C, and there are C++ ways to do things that don't require the use of a traditional macro.

const int i = 5;
int array;

is just one example that works in C++ and doesn't work in C for normal arrays.

You sure?

Code: [Select]
#include <stdio.h>

const int i = 10;
int array[i];

int main(int argc, char *argv[]) {
  printf("sizeof(array) = %lu\n", sizeof(array));
}

When I compile this on a mac using the C89 standard, it works just fine:

Code: [Select]
$ cc -std=c89 foo.c
$ ./a.out
sizeof(array) = 40
$ cc --version
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$

Heck, it even allows a const in in a case with -std=c89:

Code: [Select]
void foo(int arg) {
  switch (arg) {
  case i : break;
  default : break;
  }
}

So, if the C standard of 28 years ago allows for these kinds of constants, is there still a reason to be using #define for this stuff in 2017?

Not on GCC 4.8.4:

Code: [Select]
$ gcc -o c c.c -std=c89
c.c:4:5: error: variably modified ‘array’ at file scope
 int array[i];
     ^

Didn't work with standards of c89, c99, c11, gnu99, gnu11...
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3583
  • Country: ca
Re: Help with C language macro stringification
« Reply #40 on: July 31, 2017, 03:57:28 am »
When I compile this on a mac using the C89 standard, it works just fine:

It is illegal according to C99 standard. It may work in your compiler. But if you use it with other C compiler it may fail. Therefore, if you want portability, you shouldn't use it. If portability is not needed then it is Ok, of course.

BTW, While your code is illegal, this would be perfectly legal in function scope:

Code: [Select]
[#include <stdint.h>
#include <stdio.h>

const int z = 5;

int main(void)
{
  int x[z];
  int k = sizeof(x);
  printf("sizeof = %d\r\n",k);
}

But this actually creates a variable-size array instead of a constant one. What difference does it make? A very noticeable one. Here's the result (GCC 4.5.0 on Linux):

Code: [Select]
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ebx
pushl %ecx
subl $16, %esp
movl %esp, %eax
movl %eax, %ebx
movl z, %eax
decl %eax
movl %eax, -12(%ebp)
movl z, %eax
sall $2, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
subl %eax, %esp
movl %esp, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
movl %eax, -16(%ebp)
movl z, %eax
sall $2, %eax
movl %eax, -20(%ebp)
movl $.LC0, %eax
subl $8, %esp
pushl -20(%ebp)
pushl %eax
call printf
addl $16, %esp
movl %ebx, %esp
leal -8(%ebp), %esp
addl $0, %esp
popl %ecx
popl %ebx
popl %ebp
leal -4(%ecx), %esp
ret

Now, let's try the same with a macro:

Code: [Select]
#include <stdint.h>
#include <stdio.h>

#define z 5

int main(void)
{
  int x[z];
  int k = sizeof(x);
  printf("sizeof = %d\r\n",k);
}

Here's the result:

Code: [Select]
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $36, %esp
movl $20, -12(%ebp)
movl $.LC0, %eax
subl $8, %esp
pushl -12(%ebp)
pushl %eax
call printf
addl $16, %esp
movl -4(%ebp), %ecx
leave
leal -4(%ecx), %esp
ret

Just to clarify, here's the extra bloat the "constant" version produces compared to the "macro" version of the code:

Code: [Select]
movl %esp, %eax
movl %eax, %ebx
movl z, %eax
decl %eax
movl %eax, -12(%ebp)
movl z, %eax
sall $2, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
subl %eax, %esp
movl %esp, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
movl %eax, -16(%ebp)
movl z, %eax
sall $2, %eax
movl %eax, -20(%ebp)

You didn't expect such a result, did you?

 
The following users thanked this post: newbrain

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Re: Help with C language macro stringification
« Reply #41 on: July 31, 2017, 04:45:30 am »
Didn't work with standards of c89, c99, c11, gnu99, gnu11...

Hmm. Maybe Clang/LLVM is being too flexible here.

But this actually creates a variable-size array instead of a constant one. What difference does it make? A very noticeable one.

Yuck. Definitely don't want a variable-sized array.  :--

Quote
You didn't expect such a result, did you?

To be honest, I was surprised the example compiled with -std=c89, but didn't investigate further. My mistake. Most of my work these days is with GCC 5.x and 6.x for Cortex-M, so what works (or doesn't) with other compilers and/or architectures isn't something I spend much time thinking about. I'm much more up-to-speed on the differences between std=c++11 and std=c++14 though.  8)

So it seems that if you're dealing with ancient compilers or ancient standards (e.g., C89), then yes, by all means keep using #define for constants. But the case for continuing that style with more modern compilers seems less clear.
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2862
  • Country: nz
Re: Help with C language macro stringification
« Reply #42 on: July 31, 2017, 05:11:03 am »
BTW, While your code is illegal, this would be perfectly legal in function scope:

Code: [Select]
[#include <stdint.h>
#include <stdio.h>

const int z = 5;

int main(void)
{
  int x[z];
  int k = sizeof(x);
  printf("sizeof = %d\r\n",k);
}

Cool! So the stack is the new heap  ;D Mentally I'm still stuck with a fixed size stack frame.

How do you trap memory exhaustion with that one? Because you will be far more likely to blow the stack than if you are holding fixed length frames.

And does that mean that all nested variable declarations in a function become computed rather than 'base_pointer+fixed_offset_in_stack_frame' addressing? For example:

Code: [Select]
int func(int a) {
  int array[a];
  /* do stuff with array */
  if(....) {
    int b;
    /* Do stuff with b, which will not be at a fixed offset in the stack frame but depends on the size of 'array' */
  }
}

Oh, and how does it handle alignment - does it round up the allocated size to ensure that things won't cross the native word boundaries?

So many questions, so little sense of what the machine will actually be doing...

But I can see how it prevent programmers from leaking memory by malloc()ing but not free()ing buffers.
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3583
  • Country: ca
Re: Help with C language macro stringification
« Reply #43 on: July 31, 2017, 03:29:21 pm »
So it seems that if you're dealing with ancient compilers or ancient standards (e.g., C89), then yes, by all means keep using #define for constants. But the case for continuing that style with more modern compilers seems less clear.

I see, magic new compilers and all that. We've heard it well before the ancient compilers which I use today ever appeared.
 

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Re: Help with C language macro stringification
« Reply #44 on: July 31, 2017, 03:50:18 pm »
I see, magic new compilers and all that. We've heard it well before the ancient compilers which I use today ever appeared.

 I'm curious, which "ancient compilers" are you using?
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3583
  • Country: ca
Re: Help with C language macro stringification
« Reply #45 on: July 31, 2017, 03:53:24 pm »
And does that mean that all nested variable declarations in a function become computed rather than 'base_pointer+fixed_offset_in_stack_frame' addressing? For example:

If there's only one variable array, the compiler still can use 'base_pointer+fixed_offset_in_stack_frame'. It just has to allocate the array at the end of all other variables. Although it depends on the architecture - stack may grow up or down, there may be parameters in the stack etc.

If there are very many of these, it gets very hard for the compiler. It has to allocate all these arrays and not to lose the track where they are. Since the stack pointer is moving during the allocation process, it's not that easy to find storage for all the corresponding. Probably the best idea is to maintain a linked list of allocated arrays in the stack, and then when everything is done you can walk through the list, extract the pointers and store them at fixed locations relative the stack frame base. Such a PITA to implement for the compiler, and then nobody uses it anyway :) I don't know how GCC does multiple arrays, but you can compile and look.

But actually it is more efficient than the heap. This may be especially useful with small devices which do not have a heap. Also stack will not fragment. Of course, it makes it difficult to analyze stack behaviour. Not a problem on PC, but may be a problem with a small embedded.
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3583
  • Country: ca
Re: Help with C language macro stringification
« Reply #46 on: July 31, 2017, 04:19:43 pm »
I'm curious, which "ancient compilers" are you using?

You wouldn't believe me if I told you :)

The assembly I posted was produced by:

Code: [Select]
[~]# gcc --version
gcc (GCC) 4.5.0
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 

Offline John Coloccia

  • Super Contributor
  • ***
  • Posts: 1625
  • Country: us
Re: Help with C language macro stringification
« Reply #47 on: July 31, 2017, 10:34:23 pm »
BTW, thanks to everyone that picked up on my typo and knew what I was trying to say without making a big deal about it!

FWIW, the coding standards I just finished writing says something to the effect of, "Avoid macros where possible...prefer to use const." I wish coding standards could simply say, "Please, just make your code look like the code around it, and otherwise just use common sense and don't make a mess," but that never seems to work.
 

Offline andyturk

  • Frequent Contributor
  • **
  • Posts: 895
  • Country: us
Re: Help with C language macro stringification
« Reply #48 on: July 31, 2017, 11:41:25 pm »
BTW, thanks to everyone that picked up on my typo and knew what I was trying to say without making a big deal about it!

FWIW, the coding standards I just finished writing says something to the effect of, "Avoid macros where possible...prefer to use const." I wish coding standards could simply say, "Please, just make your code look like the code around it, and otherwise just use common sense and don't make a mess," but that never seems to work.

Clang-format goes a long way towards eliminating time spent fussing over code formatting. The projects I work on use it as the de facto description of what code is supposed to look like. I.e., we discuss/argue over how to configure the formatter, and then the rule becomes: "Clang-format everything before you check it in. Period." It integrates with many stand-alone editors these days, and some folks even set it up as a pre commit hook.

https://clang.llvm.org/docs/ClangFormat.html

When someone hands me a bunch of ugly OEM code that wraps at the 200th column (or some such), I just clang-format the whole thing in emacs. Takes a couple of seconds.
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3583
  • Country: ca
Re: Help with C language macro stringification
« Reply #49 on: August 01, 2017, 02:38:08 am »
"Please, just make your code look like the code around it, and otherwise just use common sense and don't make a mess," but that never seems to work.

Perhaps you hire wrong people.

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->