Re: question about linker

Liste des GroupesRevenir à cl c  
Sujet : Re: question about linker
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.c
Date : 28. Nov 2024, 21:33:21
Autres entêtes
Organisation : None to speak of
Message-ID : <87bjxzt7xq.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5
User-Agent : Gnus/5.13 (Gnus v5.13)
Thiago Adams <thiago.adams@gmail.com> writes:
On 28/11/2024 06:25, Keith Thompson wrote:
Thiago Adams <thiago.adams@gmail.com> writes:
On 27/11/2024 07:29, Waldek Hebisch wrote:
[...]
1) campilers for embedded targets care very much about const.  const
     qualified arrays go into read-only data section which is typically
     located in flash.  Other arrays go to RAM.  Embedded targets
     frequently have very small RAM and larger flash, so after
     dropping const program may no longer fit in available RAM.
>
I think your comment applies for const in declarations like
>
const int i = 1;
>
I used to find const confusing, as it sometimes meant 'read-only' and
other times 'immutable.'
I'm not sure what you mean.  My understanding is that const means
read-only, and nothing else.
>
I think my previous comment is not precise; it could be better
phrased. It also have some mistakes about init-declarator.
>
I will give samples what I was trying to say.
>
When we have this declaration we are declaring some storage (for the i
variable)
>
const int i = 0;

We are *defining* the object `i`, which means that the declaration
(which is also a definition) causes storage to be allocated.

But here
>
void f(const struct X * p);
>
We are not declaring the storage for the pointed object.

Right.  But "const" means the same thing: the object `*p` is read-only.
More precisely, the expression `*p` gives us read-only access to that
object; there might be other expressions that give read/write access to
the same object.

If an object is const because of its definition, then that object is
itself read-only, and anything that bypasses that (pointer casts, etc.)
causes undefined behavior.  If "const" appears in a declaration that
isn't a definition for the object, then the declaration provides a
read-only view of the object (if it exists).  The object itself may or
may not be read-only.

    int n = 42; // read/write
    void func(const int *param); // *param provides a read-only view
    func(&n);
    // The object can be modified via the name "n", but not via
    // the name "*param".

So, for the first case, we can think const as declaring a immutable
storage, while for the second sample const acts as "read-only" - we
don't know if the storage is const or not.

"const storage" is an implementation detail, not part of C semantics.
A conforming implementation could put everything in writable storage
(perhaps the OS doesn't provide memory protection, or the compiler
authors are lazy), relying on C semantics to prevent writes to
const objects.

Now, it seems less confusing to me. When const is used with variables
that can be initialized (init-declarator), it acts as 'immutable',
meaning the storage is constant.
What exactly do you mean by "the storage is constant"?  Are you
talking about memory that is marked as read-only by the OS?
>
Here comes another point (that I realized after I wrote that) and that
makes const more confusing.
>
When const is used in a external declaration like
>
const int i = 1;
int main(){}
>
We can think about read-only marked memory.

Sure, you can think of it that way, but it's not what "const"
*means*.  Something like `*(int*)&i = 42;` has undefined behavior,
regardless of the implementation.  If the implementation chooses
to store i in some kind of read-only memory (perhaps enforced by
the OS, perhaps physical RAM), it's likely to crash.  If it stores
it in ordinary read/write memory, it will likely store 42 in i.
Reading i might yield 1 or 42, depending on optimizations (remember
that it's UB).

But for local variables it does not make sense to have "read-only
marked memory" because it lives on stack.
>
int main(){
  const int i = 1;
}
>

There's no fundamental reason an implementation couldn't have
"read-only marked memory" on the stack.

[...]

const is very context dependent, maybe trying to reuse the same
keyword, and I think C23 had a change to clarify it, but instead make
it more confusing with constexpr, that was the point of my previous
topic.

const and constexpr are two very different things.

Do you have an example where "const means read-only" isn't enough to
understand the C semantics (leaving aside any implementation-specific
choices)?

(But note that given :
    constexpr int n = 42;
`&n` is of type `const int*`.  Which makes sense; we don't want `&n` to
give us write access to n.)

"const" does create a lot of opportunities for optimizations, like
initializing objects at load time rather than during execution.  And a
clever compiler could implement the same optimizations if it's able to
prove that an object is never modified.

For compile that computation what matters is the guarantee that the
compiler knows the values (it knows because it always the same value
of initialization) when using the object. (It does not depend on flow
analysis)

There's no requirement for the compiler to "know" the value of a const
object.

I think const, like in here
>
const int i = 1;
>
gives the same guarantee. (The compiler knows the value of i)

That's a very common optimization, but a conforming compiler could
simply read the value stored in `i` on every reference to it.

What I think could be explored more is the usage of register keyword
as meaning "no-storage".
>
The idea of const no-storage is good because it eliminates any problem
with object lifetime and it makes the perfect constants in my
view. Unfortunately, constexpr does not mean that because we can take
the address of constexpr object.

The fact that constexpr objects still have addresses is perhaps
a bit odd, but that's how C23 defines it.  I like the idea of a
constexpr object with no associated storage, so that

    constexpr int the_answer = 42;

does nothing more than make "the_answer" a name for the value 42,
but C23 doesn't have that.  And I'm not sure it's all that important;
if we never refer to the address of the_answer, the compiler is
free to eliminate its storage.  For most purposes, we can just
ignore the fact that a constexpr object has storage.

Sample why no-storage is useful
>
void F()
{
   register const int i = 1;
   //lets way we have lanbdas in C
   f( []()
     {
         //safe to use i even in another thread, or even after exiting F
         int k = i;
     }
   );
}

Without "register", since i is const, its value will never change
(barring undefined behavior), so it should be safe to use anyway.
How is eliminating the storage for i useful?  You can just ignore
it, and the compiler may be able to optimize it away.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Date Sujet#  Auteur
26 Nov 24 * question about linker382Thiago Adams
26 Nov 24 +* Re: question about linker16Thiago Adams
26 Nov 24 i`* Re: question about linker15Bart
26 Nov 24 i `* Re: question about linker14Thiago Adams
27 Nov 24 i  +* Re: question about linker2BGB
27 Nov 24 i  i`- Re: question about linker1Bart
27 Nov 24 i  +* Re: question about linker5David Brown
27 Nov 24 i  i`* Re: question about linker4Thiago Adams
27 Nov 24 i  i +* Re: question about linker2David Brown
27 Nov 24 i  i i`- Re: question about linker1Thiago Adams
2 Dec 24 i  i `- Re: question about linker1BGB
27 Nov 24 i  `* Re: question about linker6Michael S
27 Nov 24 i   `* Re: question about linker5Thiago Adams
27 Nov 24 i    `* Re: question about linker4Michael S
27 Nov 24 i     +- Re: question about linker1David Brown
28 Nov 24 i     +- Re: question about linker1Tim Rentsch
2 Dec 24 i     `- Re: question about linker1BGB
26 Nov 24 +* Re: question about linker20Bart
26 Nov 24 i`* Re: question about linker19Thiago Adams
26 Nov 24 i `* Re: question about linker18Bart
27 Nov 24 i  +* Re: question about linker3BGB
27 Nov 24 i  i`* Re: question about linker2fir
27 Nov 24 i  i `- Re: question about linker1BGB
27 Nov 24 i  `* Re: question about linker14Bart
27 Nov 24 i   +* Re: question about linker12Thiago Adams
27 Nov 24 i   i+- Re: question about linker1Thiago Adams
27 Nov 24 i   i`* Re: question about linker10Bart
27 Nov 24 i   i +* Re: question about linker6Bart
27 Nov 24 i   i i`* Re: question about linker5Thiago Adams
27 Nov 24 i   i i +* Re: question about linker3Thiago Adams
27 Nov 24 i   i i i`* Re: question about linker2Thiago Adams
27 Nov 24 i   i i i `- Re: question about linker1Bart
27 Nov 24 i   i i `- Re: question about linker1Bart
27 Nov 24 i   i `* Re: question about linker3Thiago Adams
27 Nov 24 i   i  `* Re: question about linker2Bart
27 Nov 24 i   i   `- Re: question about linker1Thiago Adams
28 Nov 24 i   `- Re: question about linker1Tim Rentsch
27 Nov 24 `* Re: question about linker345Waldek Hebisch
27 Nov 24  `* Re: question about linker344Thiago Adams
28 Nov 24   `* Re: question about linker343Keith Thompson
28 Nov 24    `* Re: question about linker342Thiago Adams
28 Nov 24     +* Re: question about linker337Bart
28 Nov 24     i`* Re: question about linker336Keith Thompson
29 Nov 24     i `* Re: question about linker335Bart
29 Nov 24     i  `* Re: question about linker334Keith Thompson
29 Nov 24     i   `* Re: question about linker333Bart
29 Nov 24     i    +* Re: question about linker3Keith Thompson
29 Nov 24     i    i`* Re: question about linker2Bart
29 Nov 24     i    i `- Re: question about linker1Keith Thompson
29 Nov 24     i    `* Re: question about linker329David Brown
29 Nov 24     i     `* Re: question about linker328Bart
29 Nov 24     i      +- Re: question about linker1Ike Naar
29 Nov 24     i      +* Re: question about linker325Michael S
29 Nov 24     i      i+* Re: question about linker322Bart
29 Nov 24     i      ii`* Re: question about linker321Michael S
29 Nov 24     i      ii +* Re: question about linker319David Brown
29 Nov 24     i      ii i`* Re: question about linker318Bart
29 Nov 24     i      ii i +* Re: question about linker164Keith Thompson
29 Nov 24     i      ii i i`* Re: question about linker163Bart
30 Nov 24     i      ii i i `* Re: question about linker162Keith Thompson
30 Nov 24     i      ii i i  +* Re: question about linker95Waldek Hebisch
30 Nov 24     i      ii i i  i+- Re: question about linker1Keith Thompson
30 Nov 24     i      ii i i  i+* Re: question about linker3James Kuyper
30 Nov 24     i      ii i i  ii`* Re: question about linker2Michael S
3 Dec 24     i      ii i i  ii `- Re: question about linker1Tim Rentsch
1 Dec 24     i      ii i i  i`* Re: question about linker90David Brown
1 Dec 24     i      ii i i  i +* Re: question about linker88Bart
1 Dec 24     i      ii i i  i i`* Re: question about linker87David Brown
1 Dec 24     i      ii i i  i i `* Re: question about linker86Bart
2 Dec 24     i      ii i i  i i  `* Re: question about linker85David Brown
2 Dec 24     i      ii i i  i i   `* Re: question about linker84Bart
2 Dec 24     i      ii i i  i i    +* Re: question about linker78David Brown
2 Dec 24     i      ii i i  i i    i+* Re: question about linker72Janis Papanagnou
2 Dec 24     i      ii i i  i i    ii+* Re: question about linker70Bart
2 Dec 24     i      ii i i  i i    iii+* Re: question about linker68David Brown
2 Dec 24     i      ii i i  i i    iiii`* Re: question about linker67Bart
3 Dec 24     i      ii i i  i i    iiii `* Re: question about linker66David Brown
3 Dec 24     i      ii i i  i i    iiii  +* Re: question about linker53Bart
3 Dec 24     i      ii i i  i i    iiii  i`* Re: question about linker52David Brown
3 Dec 24     i      ii i i  i i    iiii  i `* Re: question about linker51Bart
4 Dec 24     i      ii i i  i i    iiii  i  `* Re: question about linker50David Brown
4 Dec 24     i      ii i i  i i    iiii  i   `* Re: question about linker49Bart
4 Dec 24     i      ii i i  i i    iiii  i    `* Re: question about linker48David Brown
4 Dec 24     i      ii i i  i i    iiii  i     +* Re: question about linker24Bart
5 Dec 24     i      ii i i  i i    iiii  i     i`* Re: question about linker23David Brown
5 Dec 24     i      ii i i  i i    iiii  i     i +- Re: question about linker1Janis Papanagnou
5 Dec 24     i      ii i i  i i    iiii  i     i `* Re: question about linker21Bart
6 Dec 24     i      ii i i  i i    iiii  i     i  `* Re: question about linker20David Brown
6 Dec 24     i      ii i i  i i    iiii  i     i   `* Re: question about linker19Bart
6 Dec 24     i      ii i i  i i    iiii  i     i    +* Re: question about linker5Ike Naar
6 Dec 24     i      ii i i  i i    iiii  i     i    i+- Re: question about linker1Bart
7 Dec 24     i      ii i i  i i    iiii  i     i    i+- Re: question about linker1Keith Thompson
7 Dec 24     i      ii i i  i i    iiii  i     i    i`* Re: question about linker2Bart
7 Dec 24     i      ii i i  i i    iiii  i     i    i `- Re: question about linker1Keith Thompson
7 Dec 24     i      ii i i  i i    iiii  i     i    +* Re: question about linker10David Brown
7 Dec 24     i      ii i i  i i    iiii  i     i    i`* Re: question about linker9Bart
7 Dec 24     i      ii i i  i i    iiii  i     i    i `* Re: question about linker8David Brown
7 Dec 24     i      ii i i  i i    iiii  i     i    i  `* Re: question about linker7Bart
7 Dec 24     i      ii i i  i i    iiii  i     i    i   `* Re: question about linker6David Brown
7 Dec 24     i      ii i i  i i    iiii  i     i    i    `* Re: question about linker5Bart
8 Dec 24     i      ii i i  i i    iiii  i     i    i     +* Re: question about linker3Ben Bacarisse
8 Dec 24     i      ii i i  i i    iiii  i     i    i     `- Re: question about linker1David Brown
8 Dec 24     i      ii i i  i i    iiii  i     i    `* Re: question about linker3Waldek Hebisch
5 Dec 24     i      ii i i  i i    iiii  i     +* Re: question about linker15Waldek Hebisch
11 Dec 24     i      ii i i  i i    iiii  i     `* Re: question about linker8James Kuyper
3 Dec 24     i      ii i i  i i    iiii  `* Re: question about linker12Bart
3 Dec 24     i      ii i i  i i    iii`- Re: question about linker1Janis Papanagnou
2 Dec 24     i      ii i i  i i    ii`- Re: question about linker1Bart
2 Dec 24     i      ii i i  i i    i`* Re: question about linker5Bart
4 Dec 24     i      ii i i  i i    `* Re: question about linker5Waldek Hebisch
1 Dec 24     i      ii i i  i `- Re: question about linker1Janis Papanagnou
30 Nov 24     i      ii i i  +* Re: question about linker44Bart
30 Nov 24     i      ii i i  +- Re: question about linker1Janis Papanagnou
1 Dec 24     i      ii i i  `* Re: question about linker21David Brown
30 Nov 24     i      ii i `* Re: question about linker153David Brown
5 Dec 24     i      ii `- Re: question about linker1Tim Rentsch
30 Nov 24     i      i`* Re: question about linker2Tim Rentsch
29 Nov 24     i      `- Re: question about linker1David Brown
28 Nov 24     `* Re: question about linker4Keith Thompson

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal