Sujet : Re: storage of string literals
De : schwarzb (at) *nospam* delq.com (Barry Schwarz)
Groupes : comp.lang.cDate : 11. May 2024, 13:34:21
Autres entêtes
Organisation : SunSITE.dk - Supporting Open source
Message-ID : <36pu3jd9pfttbs1u7mm81omsuvmecigstk@4ax.com>
References : 1
User-Agent : Forte Agent 4.2/32.1118
On Sat, 11 May 2024 07:57:55 -0300, Johanne Fairchild
<
jfairchild@tudado.org> 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