Re: Strings in C



Bilgehan.Balban@xxxxxxxxx wrote:
Hi,

For a declaration such as:

char * mystring = "ABCDabcd123";

Is it a linker issue where such strings are stored in C, or is it
defined as part of the language definition?

It is defined as "static storage duration", which means it is available from program startup to program shutdown. The actual location in memory is not specified.


Is there any difference between an array of strings, e.g.

char mystring[10];

That's not an array of strings. It's an array of char, which may be used to hold a string. In fact, it could hold anywhere from zero to ten strings. For example,
char mystring[10] = {'m', 'y', 0, 's', 't', 'r', 'i', 'n', 'g', 0};
contains two strings: "my" at offset zero, and "string" at offset 3.


Its storage depends on where it is defined. If that definition occurs outside of any function, then it has static storage duration (available at all times) and external linkage (the symbol is visible from other translation units).

However, if that definition occurs inside a function, then it has automatic storage duration (only exists within the block it is defined in), and internal linkage (the symbol is not visible from other translation units).

and strings of type char *, in terms of where they're stored?

Any string can be pointed to by a 'char *'. The pointer type makes no difference to the storage of the string.


There are three storage types defined in C:
  static
  automatic
  allocated (ie. malloc, calloc, realloc)

String literals always have static storage, and last until the end of the program. Objects defined outside of any function, or with the 'static' keyword, have static storage, and last until the end of the program.

Objects defined within a function body, without the 'static' keyword, have automatic storage, and last until the end of the block.

A memory block allocated by malloc, calloc or realloc lasts until the base address is passed to free or realloc.

> If these
are compiler dependent, is there at least a general storage convention?

Some platforms make additional constraints on memory layout, such as dividing memory into "segments". That is not specified as part of the C language. Ask in a group devoted to your particular platform or family of platforms (for example comp.unix.programmer).


--
Simon.
.



Relevant Pages

  • Re: String questions
    ... For example I could have a string: ... The caller could provide the buffer and let the ... The called function could allocate the storage ... static storage outlasts the execution of any single ...
    (comp.lang.c)
  • Re: curious about array initialization.
    ... understanding that its through the variable declaration that we ... By "inline string", I assume you mean a string literal. ... more than that you have implicit storage. ... Its a concept from computer programming. ...
    (comp.lang.c)
  • Re: curious about array initialization.
    ... understanding that its through the variable declaration that we ... By "inline string", I assume you mean a string literal. ... more than that you have implicit storage. ...
    (comp.lang.c)
  • Re: WORKING WITH STRINGS ?! THE FINAL CHALLENGE !
    ... the string should stay alive using the 'storage' pointer ... object Button1: TButton ...
    (alt.comp.lang.borland-delphi)
  • RE: XmlSerialization Object Memory Usage
    ... As you would expect, SimpleObject and ComplexObject are ... the same string objects. ... Hashtable storage = new Hashtable; ... private static ComplexObject BuildObject() ...
    (microsoft.public.dotnet.framework.performance)