Re: Strings in C
- From: Simon Biber <news@xxxxxxxxx>
- Date: Fri, 09 Dec 2005 10:44:08 +1100
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. .
- Follow-Ups:
- Re: Strings in C
- From: Simon Biber
- Re: Strings in C
- References:
- Strings in C
- From: Bilgehan . Balban
- Strings in C
- Prev by Date: Re: functions
- Next by Date: Re: Strings in C
- Previous by thread: Strings in C
- Next by thread: Re: Strings in C
- Index(es):
Relevant Pages
|