Sujet : Re: So You Think You Can Const?
De : jameskuyper (at) *nospam* alumni.caltech.edu (James Kuyper)
Groupes : comp.lang.cDate : 08. Jan 2025, 20:10:54
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vlmiju$2t9n2$2@dont-email.me>
References : 1 2 3 4 5
User-Agent : Mozilla Thunderbird
On 1/8/25 11:05, Julio Di Egidio wrote:
On 08/01/2025 16:16, Ben Bacarisse wrote:
Julio Di Egidio <julio@diegidio.name> writes:
On 07/01/2025 23:11, Kaz Kylheku wrote:
...
An object defined with a type that is const-qualified
could be put into write-protected storage.
>
What do you/we mean by "object" in this context? (Sorry, I do have
forgotten, the glossary to begin with.)
>
An object (in C) is a contiguous region of storage, the contents of
which can represent values.
Is that regardless of the stack/heap distinction, or is an "object"
about heap-allocated/dynamic memory only? -- Anyway, I should in fact
re-acquaint myself with the language reference instead of asking this
question.)
The standard makes no distinction between heap and stack memory. Those
are merely implementation details, outside the scope of the standard. C
can be implemented on hardware that provides no support for either a
heap or a stack.
Objects can have any one of four different storage durations: static,
thread, automatic, and allocated. On implementations with a stack,
objects with static or automatic storage can be implemented using the
stack. On implementations with a heap, objects with allocated storage
duration can be implemented using the heap. I'm not sufficiently
familiar with multi-threaded programming to comment on how objects with
thread storage duration may be implemented.
The key issue that's connected to your questions is the fact that the
relevant rule is about objects that are defined as being 'const'.
Objects with automatic storage duration are never defined, so they
cannot be defined to be 'const'. You can only obtain a void* value that
points at such objects by calling one of the memory allocation functions
(aligned_alloc, malloc, calloc, or realloc). You can convert that value
into a pointer to non-const object type and then use that pointer to
write an object of that type into that memory. Unless the object type is
a character type, doing so will give that memory that type. You can also
convert that value into a pointer to a const-qualified type, but that
doesn't make the object it points at const.