<p>In C the memory address of a statically allocated object or a function is considered compile-time constant. For example this is valid code:</p>
<pre class="lang-c prettyprint-override"><code>static int x;
static int *const p = &amp;x;
static const char *const s = &quot;The address of this string will not necessarily be the same across executions&quot;;
</code></pre>
<p>But since there is no way all implementations can guarantee that the actual address of even statically allocated objects can be known at compile time, how can they be <strong>treated</strong> as compile-time constants and used in places where a constant expression is required such as an initializer for other statically allocated objects? But they are more similar to <strong>runtime constants.</strong> What techniques do compilers use to initialize statically allocated pointers to addresses of other statically allocated objects?</p>

<c><constant-expressions>
How can memory addresses be compile-time constants?
CC BY-SA 4.0
2023-12-03T00:56:24.873Z
Adresses of objects are generally link-time constants, so as long as the linker can resolve them it is fine.  The C spec calls these "address constants" and allows them in constant expressions.
Chris Dodd
CC BY-SA 4.0
2023-12-03T01:17:57.863Z
The title to me asks a different question than the body of the post, since `static void *const p = (void *)0xdeadbeef;` is valid code on 32-bit targets.
Bbrk24
CC BY-SA 4.0
2023-12-03T01:37:26.323Z
On top of what @ChrisDodd said, it's worth noting that "linker" includes "dynamic linker". While using link-time constants with dynamic shared libraries may appear to the programmer as if they are constants, in practice they may need to be resolved at run-time. On a Unix-like platform, this might be done in `dlopen` or by `ld.so`.
Pseudonym
<blockquote>
<p>What techniques do compilers use to initialize statically allocated pointers to addresses of other statically allocated objects?</p>
</blockquote>
<p>Broadly speaking it is not much different from a function call or access to a global variable. The compiler will insert a symbolic reference and the exact memory location will be filled in later.</p>
<p>Logically a program goes through a number of steps between source code and execution.</p>
<ul>
<li>Compilation.</li>
<li>Assembly (optional, the compilation process may generate object files directly)</li>
<li>Linking</li>
<li>Loading/Dynamic linking (optional, the code may be burned into a rom).</li>
</ul>
<p>At some stage in the process, the symbolic references in the code must be resolved to become actual addresses. There are broadly two stages where this can happen, if the code is built targetting a fixed address, then the linker can complete the resolution process. If the code is built to be relocatable at load time then the resolution process must be completed by the loader/dynamic linker.</p>
<p>I'm going to use the following code for my tests, it is similar to yours but I have removed the &quot;static&quot;, this means that the compiler will not be able to optimise the unused variables away. I'm going to use arm32 as it's the platform whose assembler I find easy to read, and linux as the target OS. That said, the assembler I'm going to present isn't going to contain any actual code.</p>
<pre><code>int x;
int *const p = &amp;x;
const char *const s = &quot;The address of this string will not necessarily be the same across executions&quot;;
</code></pre>
<p>On most linux ports* linux we can control whether the code is built to be relocatable or not with the -fPIC switch.</p>
<p>When looking at godbolt be aware that the output is heavily filtered by default. We need to turn off some of the filtering here or we will miss important stuff. I have manually skipped some stuff that is not relavent to the question.</p>
<p>First lets build it with no compiler flags. In this configuration the compiler will produce code that can be relocated at link time but cannot be relocated during the loading process.</p>
<pre><code>        .global x
        .bss
        .align  2
        .type   x, %object
        .size   x, 4
x:
        .space  4
</code></pre>
<p>x is not initialised, so it is placed in the bss section and no initial valid is provides.</p>
<pre><code>        .global p
        .section        .rodata
        .align  2
        .type   p, %object
        .size   p, 4
p:
        .word   x
</code></pre>
<p>x declared const, so it's placed in the rodata section. However the compiler does not provide a numeric value but merely a symbolic reference. The linker will fill in that symbolic reference later.</p>
<pre><code>        .global s
        .align  2
.LC0:
        .ascii  &quot;The address of this string will not necessarily be &quot;
        .ascii  &quot;the same across executions\000&quot;
        .align  2
        .type   s, %object
        .size   s, 4
s:
        .word   .LC0
</code></pre>
<p>The compiler has put both the string literal and the constant s in the rodata section and like it has used a symbolic reference to allow s to refer to the string constant. The section is not explicitly selected in this block of code because it was already set to .rodata from the previous block.</p>
<p>So now lets try adding -fPIC how does the code change.</p>
<pre><code>        .bss
        .align  2
        .type   x, %object
        .size   x, 4
x:
        .space  4
</code></pre>
<p>No change here, x is still an uninitialised variable in the bss section.</p>
<pre><code>        .global p
        .section        .data.rel.ro,&quot;aw&quot;
        .align  2
        .type   p, %object
        .size   p, 4
p:
        .word   x
</code></pre>
<p>However, this has changed. Rather than being placed in the normal rodata section this has now been placed in a &quot;data.rel.ro&quot; section. This is a special area of memory that is writeable during the relocation phase of dynamic loading, but will become read-only once loading is complete.</p>
<pre><code>        .global s
        .section        .rodata
        .align  2
.LC0:
        .ascii  &quot;The address of this string will not necessarily be &quot;
        .ascii  &quot;the same across executions\000&quot;
        .section        .data.rel.ro.local,&quot;aw&quot;
        .align  2
        .type   s, %object
        .size   s, 4
s:
        .word   .LC0
</code></pre>
<p>The string literal is still placed in the regular rodata section, since it doesn't refer to any memory locations, but the pointer to it is once-again placed in a special section.</p>
<p>* for historic reasons the situation is a bit different on x86-32.</p>

CC BY-SA 4.0
2023-12-13T21:09:23.050Z
2023-12-15T15:55:42.973Z
2023-12-15T15:55:42.973Z
Peter Green
<p>One easy solution, used in C++ for instance, is to restrict the set of available compile-time operations on addresses. For instance, in C++ addresses generally can only be compared for equality, arrays being the exception (the addresses of array elements are ordered by position in the array).</p>
<p>So given two distinct symbols <code>a</code> and <code>b</code>, the compiler knows that <code>&amp;a != &amp;b</code>, and also knows that <code>&amp;a &lt; &amp;b</code> is an invalid comparison.</p>
<p>The question seems to assume that the compiler needs a <em>numerical</em> value for addresses at compile time, but this is not actually required.</p>

CC BY-SA 4.0
2023-12-04T11:14:38.893Z
2023-12-04T11:14:38.893Z
MSalters
CC BY-SA 4.0
2023-12-04T13:57:30.253Z
I was about to ask if, `p` and `q` being constant, then `p-q` is constant and can be used to declare arrays. :-)
Pablo H
CC BY-SA 4.0
2023-12-04T14:01:25.100Z
@PabloH: In C++, that holds if `p` and `q` are pointers into the same array, e.g. if they have the values `&base[pp]` and `&base[qq]` respectively. In that case, `p-q == pp-qq`.
MSalters
<p>There's no concept of &quot;compile-time constants&quot; in C. There is a concept of &quot;constant expressions&quot; and more specifically &quot;address constants&quot;, which are the only kind of addresses that are allowed as initializers of <code>static</code> variables (<code>const</code> or not). Presumably they restricted initializers in that way so that implementations could initialize all static-duration objects at compile time rather than runtime, but that's only an optimization. An implementation that doesn't (or for technical reasons can't) do that can still comply with the standard.</p>
<p>In many execution environments, including modern desktop and phone operating systems with dynamic linking and <a href="https://en.wikipedia.org/wiki/Address_space_layout_randomization" rel="nofollow noreferrer">ASLR</a>, the values of address constants are not really decided until run time, so you can reasonably argue that they aren't compile-time constants—although it's operating-system code, not application code generated by the compiler, that calculates the runtime values. In environments without OS support for relocation, the compiler may generate code to do it, again at run time. The C standard doesn't forbid that, or, as far as I know, discuss it at all.</p>

CC BY-SA 4.0
2023-12-04T07:23:03.760Z
2023-12-04T07:23:03.760Z
benrg
<blockquote>
<p>In C the memory address of a statically allocated object or a function is considered compile-time ... But since there is no way all implementations can guarantee that the actual address of even statically allocated objects can be known at compile time, how can they be treated as compile-time constants?</p>
</blockquote>
<p>Fundamentally, the object code contains an expression capture mechanisms.  These mechanisms allow delaying computations involving unresolved symbols until their address information is known.  These expressions can be forwarded from object code (built directly from source) to be resolved during linking, or delayed even longer to be resolved during program loading.</p>
<p>The mechanism of relocations allows for such expression to be conveyed from source code to object code to executable, and these are know as to be resolved during final program construction, or even delayed to program loading.  Any such computations that can be resolved prior to the actual start of program execution are considered as constants, since any two such computations will yield the same values for the same symbols.</p>
<p>There are address computations that cannot be computed at compile time or even build or load time, and these go to the addresses of local variables that are placed onto the runtime stack, since there's no way at build time to know what the stack pointer value might be during any activation of a function (and that as recursion is supported, multiple copies of variables may be in multiple activations)   While the addresses of these variables can only be computed during runtime, some computations about them may be possible during build (e.g. subtraction of one address from another that is in the same array).  The compiler will switch strategies for constants known at compile time vs. runtime computed addresses.</p>

CC BY-SA 4.0
2023-12-03T02:21:16.023Z
2023-12-03T02:21:16.023Z
Erik Eidt
<p>These addresses could be set to not be known at compile time (although one of the pointers is known to point to the static variable x), but they are constant (not changing).</p>

CC BY-SA 4.0
2023-12-03T18:06:48.257Z
2023-12-03T18:06:48.257Z
gnasher729
<blockquote>
<p>But since there is no way <strong>all implementations</strong> can guarantee that the actual address of even statically allocated objects can be known at compile time</p>
</blockquote>
<p>(Emphasis mine.)</p>
<p>It's important to note that in C particularly, code is not necessarily portable. That is, code which works on one C compiler, for one target architecture, will not necessarily work when a different compiler is used, or when it is compiled for a different architecture. There are a few reasons for this:</p>
<ul>
<li>C should be able to target many different architectures, including very low-resource embedded architectures. In those contexts, statically-allocated memory with compile-time constant addresses makes sense, and dynamic allocation of memory might be infeasible.</li>
<li>C leaves a lot of things as &quot;<a href="https://en.wikipedia.org/wiki/Unspecified_behavior" rel="nofollow noreferrer">unspecified behaviour</a>&quot;, so that each compiler is free to define those behaviours how they wish; consequently, programmers are free to rely on that compiler's promised behaviour even where it goes beyond the C specification.</li>
<li>C is supposed to be &quot;close to the metal&quot;, allowing the programmer to take advantage of platform-specific optimisations if they want to. Loading from a constant address is presumably a bit faster (on some platforms) than loading and dereferencing a pointer, so C wants to make this available as an option for platforms where it makes sense. (There's also the basic fact that integer types in C have platform-dependent sizes.)</li>
</ul>
<p>So in summary, C has features which don't necessarily work everywhere, because a lot of C code is written without the intention of it working everywhere, and because offering only a lowest-common-denominator set of features common to all target architectures would mean not being able to offer the performance that C programmers demand.</p>
<p>So &quot;all implementations&quot; don't need to guarantee this for it to be a feature in the language.</p>

CC BY-SA 4.0
2023-12-03T13:07:48.370Z
2023-12-04T18:01:38.240Z
2023-12-04T18:01:38.240Z
kaya3
CC BY-SA 4.0
2023-12-13T22:59:13.700Z
The guiding philosophy of the Standards Committee has generally been to avoid saying anything about features or guarantees that are not required.  Unless a program continuously modifies itself during execution, static objects' addresses will almost always need to be resolved before a program starts executing, so resolving at that time the value of any addresses used to initialize static constants shouldn't pose a problem.
supercat
# How can memory addresses be compile-time constants?
Posted by: username_0 on 2023-12-03
In C the memory address of a statically allocated object or a function is considered compile-time constant. For example this is valid code:
static int x;
static int *const p = &x
static const char *const s = "The address of this string will not necessarily be the same across executions";

But since there is no way all implementations can guarantee that the actual address of even statically allocated objects can be known at compile time, how can they be treated as compile-time constants and used in places where a constant expression is required such as an initializer for other statically allocated objects? But they are more similar to runtime constants. What techniques do compilers use to initialize statically allocated pointers to addresses of other statically allocated objects?
**Tags:** c constant-expressions
> **Comments:**
> **username_1**: Adresses of objects are generally link-time constants, so as long as the linker can resolve them it is fine.  The C spec calls these "address constants" and allows them in constant expressions.  
> **username_2**: The title to me asks a different question than the body of the post, since `static void *const p = (void *)0xdeadbeef;` is valid code on 32-bit targets.  
> **username_3**: On top of what @ChrisDodd said, it's worth noting that "linker" includes "dynamic linker". While using link-time constants with dynamic shared libraries may appear to the programmer as if they are constants, in practice they may need to be resolved at run-time. On a Unix-like platform, this might be done in `dlopen` or by `ld.so`.  
---
## 6 answers
### Answer by username_4 on 2023-12-13. Score: 1 | Accepted answer
What techniques do compilers use to initialize statically allocated pointers to addresses of other statically allocated objects?

Broadly speaking it is not much different from a function call or access to a global variable. The compiler will insert a symbolic reference and the exact memory location will be filled in later.
Logically a program goes through a number of steps between source code and execution.

Compilation.
Assembly (optional, the compilation process may generate object files directly)
Linking
Loading/Dynamic linking (optional, the code may be burned into a rom).

At some stage in the process, the symbolic references in the code must be resolved to become actual addresses. There are broadly two stages where this can happen, if the code is built targetting a fixed address, then the linker can complete the resolution process. If the code is built to be relocatable at load time then the resolution process must be completed by the loader/dynamic linker.
I'm going to use the following code for my tests, it is similar to yours but I have removed the "static", this means that the compiler will not be able to optimise the unused variables away. I'm going to use arm32 as it's the platform whose assembler I find easy to read, and linux as the target OS. That said, the assembler I'm going to present isn't going to contain any actual code.
int x;
int *const p = &x
const char *const s = "The address of this string will not necessarily be the same across executions";

On most linux ports* linux we can control whether the code is built to be relocatable or not with the -fPIC switch.
When looking at godbolt be aware that the output is heavily filtered by default. We need to turn off some of the filtering here or we will miss important stuff. I have manually skipped some stuff that is not relavent to the question.
First lets build it with no compiler flags. In this configuration the compiler will produce code that can be relocated at link time but cannot be relocated during the loading process.
        .global x
        .bss
        .align  2
        .type   x, %object
        .size   x, 4
x:
        .space  4

x is not initialised, so it is placed in the bss section and no initial valid is provides.
        .global p
        .section        .rodata
        .align  2
        .type   p, %object
        .size   p, 4
p:
        .word   x

x declared const, so it's placed in the rodata section. However the compiler does not provide a numeric value but merely a symbolic reference. The linker will fill in that symbolic reference later.
        .global s
        .align  2
.LC0:
        .ascii  "The address of this string will not necessarily be "
        .ascii  "the same across executions\000"
        .align  2
        .type   s, %object
        .size   s, 4
s:
        .word   .LC0

The compiler has put both the string literal and the constant s in the rodata section and like it has used a symbolic reference to allow s to refer to the string constant. The section is not explicitly selected in this block of code because it was already set to .rodata from the previous block.
So now lets try adding -fPIC how does the code change.
        .bss
        .align  2
        .type   x, %object
        .size   x, 4
x:
        .space  4

No change here, x is still an uninitialised variable in the bss section.
        .global p
        .section        .data.rel.ro,"aw"
        .align  2
        .type   p, %object
        .size   p, 4
p:
        .word   x

However, this has changed. Rather than being placed in the normal rodata section this has now been placed in a "data.rel.ro" section. This is a special area of memory that is writeable during the relocation phase of dynamic loading, but will become read-only once loading is complete.
        .global s
        .section        .rodata
        .align  2
.LC0:
        .ascii  "The address of this string will not necessarily be "
        .ascii  "the same across executions\000"
        .section        .data.rel.ro.local,"aw"
        .align  2
        .type   s, %object
        .size   s, 4
s:
        .word   .LC0

The string literal is still placed in the regular rodata section, since it doesn't refer to any memory locations, but the pointer to it is once-again placed in a special section.
* for historic reasons the situation is a bit different on x86-32.
---
### Answer by username_5 on 2023-12-04. Score: 4
One easy solution, used in C++ for instance, is to restrict the set of available compile-time operations on addresses. For instance, in C++ addresses generally can only be compared for equality, arrays being the exception (the addresses of array elements are ordered by position in the array).

So given two distinct symbols `a` and `b`, the compiler knows that `&a != &b`, and also knows that `&a < &b` is an invalid comparison.

The question seems to assume that the compiler needs a *numerical* value for addresses at compile time, but this is not actually required.
> **Comments:**
> **username_6** (2023-12-04): I was about to ask if, `p` and `q` being constant, then `p-q` is constant and can be used to declare arrays. :-)  
> **username_5** (2023-12-04): @PabloH: In C++, that holds if `p` and `q` are pointers into the same array, e.g. if they have the values `&base[pp]` and `&base[qq]` respectively. In that case, `p-q == pp-qq`.  
---
### Answer by username_10 on 2023-12-03. Score: 3
> But since there is no way **all implementations** can guarantee that the actual address of even statically allocated objects can be known at compile time

(Emphasis mine.)

It's important to note that in C particularly, code is not necessarily portable. That is, code which works on one C compiler, for one target architecture, will not necessarily work when a different compiler is used, or when it is compiled for a different architecture. There are a few reasons for this:

* C should be able to target many different architectures, including very low-resource embedded architectures. In those contexts, statically-allocated memory with compile-time constant addresses makes sense, and dynamic allocation of memory might be infeasible.
* C leaves a lot of things as "[unspecified behaviour](https://en.wikipedia.org/wiki/Unspecified_behavior)", so that each compiler is free to define those behaviours how they wish; consequently, programmers are free to rely on that compiler's promised behaviour even where it goes beyond the C specification.
* C is supposed to be "close to the metal", allowing the programmer to take advantage of platform-specific optimisations if they want to. Loading from a constant address is presumably a bit faster (on some platforms) than loading and dereferencing a pointer, so C wants to make this available as an option for platforms where it makes sense. (There's also the basic fact that integer types in C have platform-dependent sizes.)

So in summary, C has features which don't necessarily work everywhere, because a lot of C code is written without the intention of it working everywhere, and because offering only a lowest-common-denominator set of features common to all target architectures would mean not being able to offer the performance that C programmers demand.

So "all implementations" don't need to guarantee this for it to be a feature in the language.
> **Comments:**
> **username_11** (2023-12-13): The guiding philosophy of the Standards Committee has generally been to avoid saying anything about features or guarantees that are not required.  Unless a program continuously modifies itself during execution, static objects' addresses will almost always need to be resolved before a program starts executing, so resolving at that time the value of any addresses used to initialize static constants shouldn't pose a problem.  
---
### Answer by username_7 on 2023-12-04. Score: 1
There's no concept of "compile-time constants" in C. There is a concept of "constant expressions" and more specifically "address constants", which are the only kind of addresses that are allowed as initializers of `static` variables (`const` or not). Presumably they restricted initializers in that way so that implementations could initialize all static-duration objects at compile time rather than runtime, but that's only an optimization. An implementation that doesn't (or for technical reasons can't) do that can still comply with the standard.

In many execution environments, including modern desktop and phone operating systems with dynamic linking and [ASLR](https://en.wikipedia.org/wiki/Address_space_layout_randomization), the values of address constants are not really decided until run time, so you can reasonably argue that they aren't compile-time constants—although it's operating-system code, not application code generated by the compiler, that calculates the runtime values. In environments without OS support for relocation, the compiler may generate code to do it, again at run time. The C standard doesn't forbid that, or, as far as I know, discuss it at all.
---
### Answer by username_8 on 2023-12-03. Score: 1
> In C the memory address of a statically allocated object or a function is considered compile-time ... But since there is no way all implementations can guarantee that the actual address of even statically allocated objects can be known at compile time, how can they be treated as compile-time constants?

Fundamentally, the object code contains an expression capture mechanisms.  These mechanisms allow delaying computations involving unresolved symbols until their address information is known.  These expressions can be forwarded from object code (built directly from source) to be resolved during linking, or delayed even longer to be resolved during program loading.

The mechanism of relocations allows for such expression to be conveyed from source code to object code to executable, and these are know as to be resolved during final program construction, or even delayed to program loading.  Any such computations that can be resolved prior to the actual start of program execution are considered as constants, since any two such computations will yield the same values for the same symbols.

There are address computations that cannot be computed at compile time or even build or load time, and these go to the addresses of local variables that are placed onto the runtime stack, since there's no way at build time to know what the stack pointer value might be during any activation of a function (and that as recursion is supported, multiple copies of variables may be in multiple activations)   While the addresses of these variables can only be computed during runtime, some computations about them may be possible during build (e.g. subtraction of one address from another that is in the same array).  The compiler will switch strategies for constants known at compile time vs. runtime computed addresses.
---
### Answer by username_9 on 2023-12-03. Score: 0
These addresses could be set to not be known at compile time (although one of the pointers is known to point to the static variable x), but they are constant (not changing).
---
