• Re: relearning C: why does an in-place change to a char* segfault?

    From Janis Papanagnou@janis_papanagnou+ng@hotmail.com to comp.lang.c on Sat Sep 28 07:22:01 2024
    From Newsgroup: comp.lang.c

    On 28.09.2024 05:34, Keith Thompson wrote:
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
    Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
    The more C is changed to resemble C++ the worse it becomes. It
    isn't surprising that you like it.

    For context, since the parent article is from a month and a half
    ago, I was discussing a proposal to change a future C standard to
    refer to "constants" as "literals". I mentioned that I think it's
    a good idea.

    I've heard of and seen various forms to name such entities...
    - in a Pascal and an Eiffel book I find all these named "constants"
    - in an Algol 68 book I read about "standard designations"
    - in a book about languages and programming in general I find
    "literals" ("abc"), "numerals" (42), "word-symbols" (false),
    "graphemes" (©), etc., differentiated
    - I've also have heard about "standard representations [for the
    values of a respective type]"; also a type-independent term

    I also think (for various reasons) that "constants" is not a good
    term. (Personally I like terms like the Algol 68 term, that seems
    to "operate" on another [more conceptual] abstraction level.)

    But you'll certainly have to expect a lot of anger if the terminology
    of some standards documents get changed from one version to another.

    Janis

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Phillip Frabott@nntp@fulltermprivacy.com to comp.lang.c on Sat Sep 28 17:57:56 2024
    From Newsgroup: comp.lang.c

    In reply to "Janis Papanagnou" who wrote the following:

    On 28.09.2024 05:34, Keith Thompson wrote:
    Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
    Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
    The more C is changed to resemble C++ the worse it becomes. It
    isn't surprising that you like it.

    For context, since the parent article is from a month and a half
    ago, I was discussing a proposal to change a future C standard to
    refer to "constants" as "literals". I mentioned that I think it's
    a good idea.

    I've heard of and seen various forms to name such entities...
    - in a Pascal and an Eiffel book I find all these named "constants"
    - in an Algol 68 book I read about "standard designations"
    - in a book about languages and programming in general I find
    "literals" ("abc"), "numerals" (42), "word-symbols" (false),
    "graphemes" (©), etc., differentiated
    - I've also have heard about "standard representations [for the
    values of a respective type]"; also a type-independent term

    I also think (for various reasons) that "constants" is not a good
    term. (Personally I like terms like the Algol 68 term, that seems
    to "operate" on another [more conceptual] abstraction level.)

    But you'll certainly have to expect a lot of anger if the terminology
    of some standards documents get changed from one version to another.

    Janis


    The only gripe I would have if we synonymized constants and literals is that not
    every const is initialized with a literal. There have been times where I have initialized a const from the value of a variable. I don't think that const and literals are the same thing because of this.

    To me a const is permanently set at initialization. That being runtime while a literal is a hardcoded value that gets set at compile time.

    There are cases where it does in fact matter, especially when a const is not initialized with a literal but a var. It can also make a bigger difference when
    someone actually needs to know when something is being set at compile time and when it is being set at runtime. It can have a huge impact especially in edge cases.

    But thats just my 2 cents in the mix.

    Have a good one!

    Phillip Frabott
    {Adam: Is a void really a void if it returns? - Jack: No, it's just nullspace at
    that point.}
    Phillip Frabott
    {Adam: Is a void really a void if it returns? - Jack: No, it's just nullspace at
    that point.}
    --
    ----------------------------------------- --- -- -
    Posted with NewsLeecher v7.0 Final
    Free Newsreader @ http://www.newsleecher.com/
    ------------------------------- ----- ---- -- -

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Keith Thompson@Keith.S.Thompson+u@gmail.com to comp.lang.c on Sat Sep 28 13:42:31 2024
    From Newsgroup: comp.lang.c

    Phillip Frabott <nntp@fulltermprivacy.com> writes:
    In reply to "Janis Papanagnou" who wrote the following:
    [...]
    I also think (for various reasons) that "constants" is not a good
    term. (Personally I like terms like the Algol 68 term, that seems
    to "operate" on another [more conceptual] abstraction level.)

    But you'll certainly have to expect a lot of anger if the terminology
    of some standards documents get changed from one version to another.

    The only gripe I would have if we synonymized constants and literals
    is that not every const is initialized with a literal. There have been
    times where I have initialized a const from the value of a variable. I
    don't think that const and literals are the same thing because of
    this.

    Though the word "const" is obviously derived from the English word
    "constant", in C "const" and "constant" are very different things.

    The "const" keyword really means "read-only" (and perhaps would have
    been clearer if it had been spelled "readonly").

    A "constant" is what some languages call a "literal", and a "constant expression" is an expression that can be evaluated at compile time.

    For example, this:

    const int r = rand();

    is perfectly valid.

    Incidentally, the N3301 draft of the C2Y standard has this change
    applied to it:

    Syntax
    constant:
    integer-literal
    floating-literal
    enumeration-constant
    character-literal
    predefined-constant

    The predefined constants are false, true, and nullptr.

    (I find it a bit odd that enumeration and predefined constants are still
    called constants, not literals.)

    Compare C17:

    Syntax
    constant:
    integer-constant
    floating-constant
    enumeration-constant
    character-constant


    https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3301.pdf
    (open-std.org seems to be down at the moment.)
    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    void Void(void) { Void(); } /* The recursive call of the void */
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Phillip Frabott@nntp@fulltermprivacy.com to comp.lang.c on Sat Sep 28 22:05:30 2024
    From Newsgroup: comp.lang.c

    In reply to "Keith Thompson" who wrote the following:

    Phillip Frabott <nntp@fulltermprivacy.com> writes:
    In reply to "Janis Papanagnou" who wrote the following:
    [...]
    I also think (for various reasons) that "constants" is not a good
    term. (Personally I like terms like the Algol 68 term, that seems
    to "operate" on another [more conceptual] abstraction level.)

    But you'll certainly have to expect a lot of anger if the terminology
    of some standards documents get changed from one version to another.

    The only gripe I would have if we synonymized constants and literals
    is that not every const is initialized with a literal. There have been times where I have initialized a const from the value of a variable. I don't think that const and literals are the same thing because of
    this.

    Though the word "const" is obviously derived from the English word "constant", in C "const" and "constant" are very different things.

    The "const" keyword really means "read-only" (and perhaps would have
    been clearer if it had been spelled "readonly").

    In the context of C I agree. Although I would point out that for some langauges
    const and readonly are two completely different things. (just a brevity remark,
    but I'll get back on topic now)

    A "constant" is what some languages call a "literal", and a "constant expression" is an expression that can be evaluated at compile time.

    For example, this:

    const int r = rand();

    is perfectly valid.

    Maybe the expression can be determined/evaluated at compile time but not the result. When I think of literals the resulting value has to be determined at compile time. So const int r = 15; would be to me a literal result. The compiler
    can bake that in without needing further runtime execution to get such result. But a const can be either a literal or non-literal in my view. Anything that cannot give a predetermined value at compile time is a const. So to me:

    const int r = rand();

    is not a literal only because the output of rand() is unknown until runtime. From a human-readable code perspective I get it. And fine, there can be a similarity between const and literal on the surface. But the moment you need to
    know exactly what the compiler is doing, those two things have to be separate.

    Perhaps there is a better way to do it. Or maybe there can be a literal type that is basically equal to const type for the purpose of coding or even perhaps
    a [--treat-const-as-literal] compiler parameter for code where a literal value and a const value should be treated the same. But I still think these two should
    be treated differently.

    I should note I don't have the original posting for this thread (I guess my provider doesn't have it) so I don't have the original URI that started this thread. If someone can share it in a reply I'd really appreciate it so I can be
    sure I'm on the same page with what is being discussed.
    Phillip Frabott
    {Adam: Is a void really a void if it returns? - Jack: No, it's just nullspace at
    that point.}
    --
    ----------------------------------------- --- -- -
    Posted with NewsLeecher v7.0 Final
    Free Newsreader @ http://www.newsleecher.com/
    ------------------------------- ----- ---- -- -

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Keith Thompson@Keith.S.Thompson+u@gmail.com to comp.lang.c on Sat Sep 28 15:17:10 2024
    From Newsgroup: comp.lang.c

    Phillip Frabott <nntp@fulltermprivacy.com> writes:
    In reply to "Keith Thompson" who wrote the following:
    Phillip Frabott <nntp@fulltermprivacy.com> writes:
    In reply to "Janis Papanagnou" who wrote the following:
    [...]
    I also think (for various reasons) that "constants" is not a good
    term. (Personally I like terms like the Algol 68 term, that seems
    to "operate" on another [more conceptual] abstraction level.)

    But you'll certainly have to expect a lot of anger if the terminology
    of some standards documents get changed from one version to another.

    The only gripe I would have if we synonymized constants and literals
    is that not every const is initialized with a literal. There have been
    times where I have initialized a const from the value of a variable. I
    don't think that const and literals are the same thing because of
    this.

    Though the word "const" is obviously derived from the English word
    "constant", in C "const" and "constant" are very different things.

    The "const" keyword really means "read-only" (and perhaps would have
    been clearer if it had been spelled "readonly").

    In the context of C I agree. Although I would point out that for some langauges
    const and readonly are two completely different things. (just a brevity remark,
    but I'll get back on topic now)

    A "constant" is what some languages call a "literal", and a "constant
    expression" is an expression that can be evaluated at compile time.

    For example, this:

    const int r = rand();

    is perfectly valid.

    Maybe the expression can be determined/evaluated at compile time but not the result. When I think of literals the resulting value has to be determined at compile time. So const int r = 15; would be to me a literal result. The compiler
    can bake that in without needing further runtime execution to get such result.
    But a const can be either a literal or non-literal in my view. Anything that cannot give a predetermined value at compile time is a const. So to me:

    const int r = rand();

    is not a literal only because the output of rand() is unknown until
    runtime.

    Of course that's not a literal, nor is it constant. The object r is
    *const* (its value cannot be changed after its initialization), not
    *constant*.

    From a human-readable code perspective I get it. And fine, there can be a similarity between const and literal on the surface. But the moment you need to
    know exactly what the compiler is doing, those two things have to be separate.

    Certainly "const" and "literal" are separate. They mean almost
    completely different things.

    I can't help wondering if you missed the point.

    A literal, as the term is commonly used, is a single token that
    represents a constant (compile-time) value of some type. Examples are
    42 and "foo". (C compound literals are more complicated.)

    C uses the term "constant" (as a noun) for tokens of numeric types, like
    42 and 1.5, that represent compile-time values. C2Y will likely refer
    to these as "literals". See
    https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3239.htm
    which is the proposal to use the word "literal", and
    https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3301.pdf
    particularly section 6.4.5, which is a C2Y draft that incorporates the
    proposal (when open-std.org is up and running again).

    The word "constant" is also used as an adjective in the phrase "constant expression". A constant expression, like (2+2), is one that's required
    to be evaluated at compile time in certain contexts, so it can't refer
    to the value of a variable.

    The keyword "const" is almost unrelated to either "const" or "literal".
    It means that an object cannot legally be modified after it's been
    initialized.

    The planned change for C2Y is to use the word "literal" rather than
    "constant". There's no change to the phrase "constant expression".

    [...]
    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    void Void(void) { Void(); } /* The recursive call of the void */
    --- Synchronet 3.20a-Linux NewsLink 1.114