Liste des Groupes | Revenir à cl c |
On 02/08/2024 02:06, Kaz Kylheku wrote:The key point is that the {1} isn't the value loclated in B[0], but the source of that value when B was initialize, which if B is in the .data segement is the source of the data to initialize that .data segement, which might exist nowhere in the actual ram memory of the machine, but might exist just in the file that was loaded.On 2024-08-01, Bart <bc@freeuk.com> wrote:Why not? I haven't requested that those are 'const'. Further, gcc has no problem running this program:>>It segfaults when the string is stored in a read-only part of the binary.>
A string literal creates an array object with static storage duration.
Any attempt to modify that array object has undefined behavior.
What's the difference between such an object, and an array like one of
these:
Programming languages can have objects that have the same lifetime, yet some
of which are mutable and some of which are immutable.
>
If the compiler believes that the immutable objects are in fact
not mutated, it's a bad idea to modify them behind the compiler's
back.
>
There doesn't have to be any actual difference in the implementation of
these objects, like in what area they are stored, other than the rules
regarding their correct use, namely prohibiting modification.
>
The Racket language has both mutable and immutable cons cells.
The difference is that the immutable cons cells simply lack the
operations needed to mutate them. I'm not an expert on the Racket
internals but I don't see a reason why they couldn't be stored in the
same heap.
>static char A[100];>
static char B[100]={1};
>
Do these not also have static storage duration? Yet presumably these can
be legally modified.
That 1 which initializes B[0] cannot be modified.
>
static char A[100];
static char B[100]={1};
printf("%d %d %d\n", A[0], B[0], 1);
A[0]=55;
B[0]=89;
printf("%d %d %d\n", A[0], B[0], 1);
But it does use readonly memory for string literals.
(The point of A and B was to represent .bss and .data segments respectively. A's data is not part of the EXE image; B's is.
While the point of 'static' was to avoid having to specify whether A and B were at module scope or within a function.)
> That 1 which initializes B[0] cannot be modified.
Or do you literally mean the value of that '1'? Then it doesn' make sense; here that is a copy of the literal stored in one cell of 'B'. The value of the cell can change, then that particular copy of '1' is lost.
Here:
static char B[100] = {1, 1, 1, 1, 1, 1};
changing B[0] will not affect the 1s in B[1..5], and in my example above, that standalone '1' is not affected.
Les messages affichés proviennent d'usenet.