• storage of string literals

    From Johanne Fairchild@21:1/5 to All on Sat May 11 07:57:55 2024
    I don't think the standard says anything about where a string literal
    would be allocated. Could a compiler allocate on the stack string
    literals defined inside a procedure? (Can I say that ``string
    literals'' are /defined/ or should use a different word?) So that, for instance, when the procedure dealWord (below) returns, token->category
    would point nowhere.

    --8<-------------------------------------------------------->8---
    typedef struct Token stoken;

    struct Token {
    char *category;
    };

    void dealWord(stoken *token) {
    token->category = "cat1";
    [...]
    }
    --8<-------------------------------------------------------->8---

    Section 6.2.4 of C99 has title ``storage durations of objects''. Point
    4 says

    --8<-------------------------------------------------------->8---
    An object whose identifier is declared with no linkage and without the
    storage-class specifier static has automatic storage duration.
    --8<-------------------------------------------------------->8---

    Are string literals objects? Thanks!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Barry Schwarz@21:1/5 to [email protected] on Sat May 11 05:34:21 2024
    On Sat, 11 May 2024 07:57:55 -0300, Johanne Fairchild
    <[email protected]> wrote:

    I don't think the standard says anything about where a string literal
    would be allocated. Could a compiler allocate on the stack string
    literals defined inside a procedure? (Can I say that ``string
    literals'' are /defined/ or should use a different word?) So that, for >instance, when the procedure dealWord (below) returns, token->category
    would point nowhere.

    --8<-------------------------------------------------------->8---
    typedef struct Token stoken;

    struct Token {
    char *category;
    };

    void dealWord(stoken *token) {
    token->category = "cat1";
    [...]
    }
    --8<-------------------------------------------------------->8---

    Section 6.2.4 of C99 has title ``storage durations of objects''. Point
    4 says

    --8<-------------------------------------------------------->8---
    An object whose identifier is declared with no linkage and without the
    storage-class specifier static has automatic storage duration.
    --8<-------------------------------------------------------->8---

    Are string literals objects? Thanks!

    Whether or not they are objects, section 6.2.5-6 of C11 states they
    have static duration. Consequently, each exists at a constant
    location for the life of the program. Whether that is on the stack or
    not is an implementation detail. The standard does not use the term
    stack.

    --
    Remove del for email

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Johanne Fairchild on Sat May 11 17:07:25 2024
    On 11/05/2024 12:57, Johanne Fairchild wrote:
    I don't think the standard says anything about where a string literal
    would be allocated. Could a compiler allocate on the stack string
    literals defined inside a procedure? (Can I say that ``string
    literals'' are /defined/ or should use a different word?) So that, for instance, when the procedure dealWord (below) returns, token->category
    would point nowhere.

    --8<-------------------------------------------------------->8---
    typedef struct Token stoken;

    struct Token {
    char *category;
    };

    void dealWord(stoken *token) {
    token->category = "cat1";
    [...]
    }
    --8<-------------------------------------------------------->8---

    Section 6.2.4 of C99 has title ``storage durations of objects''. Point
    4 says

    --8<-------------------------------------------------------->8---
    An object whose identifier is declared with no linkage and without the
    storage-class specifier static has automatic storage duration.
    --8<-------------------------------------------------------->8---

    Are string literals objects? Thanks!

    String literals result in arrays of static duration, and so I think are objects. However, they do not necessarily have unique addresses, and
    can overlap. The C standards say very little about how they are handled
    - it's all up to the implementation.

    Since they are read only and static, they are almost always implemented
    in the text section of the binary, along with other read-only data
    beside executable code. It is only on targets with distinct
    instructions for accessing code and data space that they go anywhere else.

    They are never, AFAIK, allocated on the stack. What would be the point?
    In almost all implementations they are in the text section of the
    binary and accessible directly from there - copying them to local memory
    on the stack would give no benefits. On targets where such text memory
    can't be read as conveniently (such as some old microcontrollers or
    DSPs), they would most likely be copied into data ram on program startup.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)