[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gameoftrees
Subject:    Re: const-ify tables
From:       Christian Weisgerber <naddy () mips ! inka ! de>
Date:       2022-02-16 19:32:29
Message-ID: Yg1RTTGiyHtkBMgd () lorvorc ! mips ! inka ! de
[Download RAW message or body]

Ted Bullock:

> #define GOT_HISTEDIT_PICK 'p'
> to
> const char GOT_HISTEDIT_PICK = 'p';

For starters, this is thoroughly unidiomatic for OpenBSD(-adjacent)
code.

> I've actually been wondering about this kind of thing for a long time.
> Obviously preprocessor directives get written directly into the
> executable code, whereas readonly const variables are stored elsewhere
> and as variables their address can be taken.

The compiler will use them as constants as well, but also initialize
(read-only) storage with them.  The latter is pointless if you do
not want to take their address.  It also precludes use in header
files that are included by several source files since you would end
up with multiple conflicting definitions of the same symbol.

$ cat t.c
const int ten = 10;

int
f(int x)
{
        return x + ten;
}
$ cc -c t.c
$ objdump -td t.o
...
0000000000000000 g     O .rodata        0000000000000004 ten
...
   b:   55                      push   %rbp
   c:   48 89 e5                mov    %rsp,%rbp
   f:   89 7d fc                mov    %edi,0xfffffffffffffffc(%rbp)
  12:   8b 45 fc                mov    0xfffffffffffffffc(%rbp),%eax
  15:   83 c0 0a                add    $0xa,%eax
  18:   5d                      pop    %rbp
...

-- 
Christian "naddy" Weisgerber                          naddy@mips.inka.de



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic