Article View: comp.std.c
Article #3905Re: Placement of string literals
From: Ian Cargill
Date: Sun, 03 Sep 1995 15:07
Date: Sun, 03 Sep 1995 15:07
58 lines
2604 bytes
2604 bytes
In article <42c4uu$9e5@sparcserver.lrz-muenchen.de> ua302aa@sun2.lrz-muenchen.de writes: >Regarding the placement of string literals, the following question >came up in de.comp.lang.c: > >Obviously, there are compilers that handle > > char *foo = "Hello world"; > >different from > > const char *bar = "Hello world"; > >as far as the placement of the string literal is concerned. String >literals are arrays of "const char". This implies that they can >be placed in read only memory. Does the definition of "foo" somehow >prevent this placement? No. The standard doesn't say that the string literals are const, merely that it is undefined behaviour to try to modify them. Two possible reasons for this are first that identical strings need not be distinct and second that they could be in read-only memory. Then again, they might not. Either way, the properties of the strings literals (char, static, treat as const) are set by the standard and are not dependant on the way in which the string is being used. There is no good reason why the two strings above should be handled any differently. (That I am aware of.) The non-constness of *foo is purely a property of foo, not the string. The second declaration is preferable, since it guards against using undefined behaviour. (i.e. attempting to modify a string litteral.) > >In other words, what is wrong with the statement: "String literals >are arrays of const char, Strictly, they are only arrays of static char, not const char. However you should treat them as though they were const. >and hence may be placed in a read only >region of storage, it doesn't matter whether they are or not, you must always assume that they are not modifiable. >even if they are used to initialize a non const >char *"? not really relevant, but yes. -- ============================================================================ Ian Cargill CEng MIEE | Find out about the Association of C and C++ Users Soliton Software Ltd. | info@accu.org OR http://bach.cis.temple.edu/accu
Technical Details
Message-ID:
<810140841snz@soliton.demon.co.uk>Path:
arc1-ca.pugleaf.net!mail2news.demon.co.uk!soliton.demon.co.ukReferences:
<42c4uu$9e5@sparcserver.lrz-muenchen.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!mail2news.demon.co.uk!soliton.demon.co.uk From: Ian Cargill <ian@soliton.demon.co.uk> Newsgroups: comp.std.c Subject: Re: Placement of string literals Date: Sun, 03 Sep 95 15:07:21 GMT Organization: Soliton Software Ltd. Distribution: world Message-ID: <810140841snz@soliton.demon.co.uk> References: <42c4uu$9e5@sparcserver.lrz-muenchen.de> Reply-To: ian@soliton.demon.co.uk X-NNTP-Posting-Host: soliton.demon.co.uk X-Newsreader: Simple NEWS 2.0 (ka9q DIS 1.24)