* Re: string is const char [] ?
[not found] <orogvjbtlp.fsf@zecarneiro.lsd.dcc.unicamp.br>
@ 1998-06-24 3:01 ` Kamil Iskra
1998-06-24 4:06 ` Alexandre Oliva
0 siblings, 1 reply; 14+ messages in thread
From: Kamil Iskra @ 1998-06-24 3:01 UTC (permalink / raw)
To: Alexandre Oliva; +Cc: egcs, egcs-bugs
On 23 Jun 1998, Alexandre Oliva wrote:
> > char a1[] = "test not const";
> This line should cause egcs to flag an error. The only
> backward-compatibility provided in the standard is [conv.array]/2,
> that allows string literals to be converted to non-const `char*'.
> Hence, string literals cannot be used to initialize a char[]. At
> least a warning should be issued in this case.
Excuse me?! I missed the original mail, but do you really suggest that all
the programmers should suddenly switch to:
char a1[]={'t', 'e', 's', 't', ' ', 'n', 'o', 't', ' ', 'c', 'o', 'n',
's', 't', '\0'};
??? IMO, this is INSANE.
/ Kamil Iskra AmigaOS Linux/i386 Linux/m68k \
| GeekGadgets GCC maintainer UNIX system administrator |
| iskra@student.uci.agh.edu.pl kiskra@ernie.icslab.agh.edu.pl |
\ kamil@dwd.interkom.pl http://student.uci.agh.edu.pl/~iskra /
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: string is const char [] ?
1998-06-24 3:01 ` string is const char [] ? Kamil Iskra
@ 1998-06-24 4:06 ` Alexandre Oliva
1998-06-24 10:08 ` Kamil Iskra
` (3 more replies)
0 siblings, 4 replies; 14+ messages in thread
From: Alexandre Oliva @ 1998-06-24 4:06 UTC (permalink / raw)
To: Kamil Iskra; +Cc: egcs, egcs-bugs
Kamil Iskra <kamil@dwd.interkom.pl> writes:
> On 23 Jun 1998, Alexandre Oliva wrote:
>> > char a1[] = "test not const";
>> This line should cause egcs to flag an error.
> Excuse me?! I missed the original mail, but do you really suggest that all
> the programmers should suddenly switch to:
> char a1[]={'t', 'e', 's', 't', ' ', 'n', 'o', 't', ' ', 'c', 'o', 'n',
> 's', 't', '\0'};
How about:
const char a1[] = "test not const";
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: string is const char [] ?
1998-06-24 4:06 ` Alexandre Oliva
@ 1998-06-24 10:08 ` Kamil Iskra
1998-06-24 21:23 ` Richard Henderson
1998-06-24 10:10 ` Joe Buck
` (2 subsequent siblings)
3 siblings, 1 reply; 14+ messages in thread
From: Kamil Iskra @ 1998-06-24 10:08 UTC (permalink / raw)
To: Alexandre Oliva; +Cc: egcs, egcs-bugs
On 24 Jun 1998, Alexandre Oliva wrote:
> >> > char a1[] = "test not const";
> >> This line should cause egcs to flag an error.
> > Excuse me?! I missed the original mail, but do you really suggest that all
> > the programmers should suddenly switch to:
> > char a1[]={'t', 'e', 's', 't', ' ', 'n', 'o', 't', ' ', 'c', 'o', 'n',
> > 's', 't', '\0'};
> How about:
>
> const char a1[] = "test not const";
I didn't want a const array, I wanted a plain one, probably for a good
reason. Maybe I wanted to change some characters or append something to
it? Now, how should I do that to satisfy the standard and avoid the much
more error-prone standard array-initialisation syntax?
Don't get me wrong, I'm sure you know the standard far better than I do,
but are you SURE that it disallows this initialisation syntax? I find it
hard to believe, since first, I think this syntax is very common (at least
I use it quite often), and second, this syntax is fine in C, and C++ is
generally supposed to accept C code (I know, there are certain
incompatibilities, and, IIRC, there is even a list of them. Is this
particular incompatibility listed there?).
If the standard indeed does deliberately disallow this initialisation
syntax, I would vote that G++ allows it, preferably without any warnings
(unless compiling with -ansi, -pedantic or some such).
/ Kamil Iskra AmigaOS Linux/i386 Linux/m68k \
| GeekGadgets GCC maintainer UNIX system administrator |
| iskra@student.uci.agh.edu.pl kiskra@ernie.icslab.agh.edu.pl |
\ kamil@dwd.interkom.pl http://student.uci.agh.edu.pl/~iskra /
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: string is const char [] ?
1998-06-24 4:06 ` Alexandre Oliva
1998-06-24 10:08 ` Kamil Iskra
1998-06-24 10:10 ` Joe Buck
@ 1998-06-24 10:10 ` jeroen dobbelaere xe44 7682
1998-06-24 12:27 ` Horst von Brand
3 siblings, 0 replies; 14+ messages in thread
From: jeroen dobbelaere xe44 7682 @ 1998-06-24 10:10 UTC (permalink / raw)
To: Alexandre Oliva; +Cc: Kamil Iskra, egcs, egcs-bugs
Alexandre Oliva writes:
> Kamil Iskra <kamil@dwd.interkom.pl> writes:
>
> > On 23 Jun 1998, Alexandre Oliva wrote:
> >> > char a1[] = "test not const";
>
> >> This line should cause egcs to flag an error.
>
> > Excuse me?! I missed the original mail, but do you really suggest that all
> > the programmers should suddenly switch to:
>
> > char a1[]={'t', 'e', 's', 't', ' ', 'n', 'o', 't', ' ', 'c', 'o', 'n',
> > 's', 't', '\0'};
>
> How about:
>
> const char a1[] = "test not const";
What if you want to initialise a changeable char array with a default string ?
// stupid example
void f()
{
char counter[]="999"; // should work (normal initialization, size =4)
// char *counter="000"; // should at least give a warning, and
//should not work with this example
for(;;)
{
if(counter[2]-- == '0')
{
counter[2]='9';
if(counter[1]-- == '0')
{
counter[1]='9;
if(counter[0]-- == '0')
{
counter[0]='9';
break; // exit the for(;;)
}
}
}
// do something with the current counter
cout << counter << endl;
}
}
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: string is const char [] ?
1998-06-24 4:06 ` Alexandre Oliva
1998-06-24 10:08 ` Kamil Iskra
@ 1998-06-24 10:10 ` Joe Buck
1998-06-24 14:50 ` Alexandre Oliva
1998-06-24 10:10 ` jeroen dobbelaere xe44 7682
1998-06-24 12:27 ` Horst von Brand
3 siblings, 1 reply; 14+ messages in thread
From: Joe Buck @ 1998-06-24 10:10 UTC (permalink / raw)
To: Alexandre Oliva; +Cc: kamil, egcs, egcs-bugs
On 23 Jun 1998, Alexandre Oliva wrote:
> >> > char a1[] = "test not const";
>
> >> This line should cause egcs to flag an error.
Kamil Iskra <kamil@dwd.interkom.pl> writes:
> > Excuse me?! I missed the original mail, but do you really suggest that all
> > the programmers should suddenly switch to:
>
> > char a1[]={'t', 'e', 's', 't', ' ', 'n', 'o', 't', ' ', 'c', 'o', 'n',
> > 's', 't', '\0'};
Alexandre Oliva writes:
> How about:
> const char a1[] = "test not const";
Kamil is trying to initialize a char[], so your suggestion is irrelevant.
Programmers must sometimes initialize a writable string, and the standard
way to do this in C is exactly the way you claim is forbidden. This would
need to be documented as a language incompatibility.
I don't believe that you are correct, Alexandre. I think you are
confusing assignment (where there is a conversion) with initialization.
If the standard is unclear, this is a case where the committee should be
asked to produce a clarification.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: string is const char [] ?
1998-06-24 4:06 ` Alexandre Oliva
` (2 preceding siblings ...)
1998-06-24 10:10 ` jeroen dobbelaere xe44 7682
@ 1998-06-24 12:27 ` Horst von Brand
3 siblings, 0 replies; 14+ messages in thread
From: Horst von Brand @ 1998-06-24 12:27 UTC (permalink / raw)
To: Alexandre Oliva; +Cc: Kamil Iskra, egcs, egcs-bugs
Alexandre Oliva <oliva@dcc.unicamp.br> said:
> Kamil Iskra <kamil@dwd.interkom.pl> writes:
> > On 23 Jun 1998, Alexandre Oliva wrote:
> >> > char a1[] = "test not const";
> >> This line should cause egcs to flag an error.
I think this is ridiculous. Why should it matter that the value from which
a particular variable is _initialized_ is or not a constant? With this
reasoning, we'd have:
int i = 5; // illegal?!
int j = k; // OK, as long as k isn't 'const int'?!
> > Excuse me?! I missed the original mail, but do you really suggest that all
> > the programmers should suddenly switch to:
> > char a1[]={'t', 'e', 's', 't', ' ', 'n', 'o', 't', ' ', 'c', 'o', 'n',
> > 's', 't', '\0'};
By the above reasoning, this should be an error too.
Besides, in C doing what you state above is quite common, is compatibility
with C being thrown out the window just like that for no reason at all?
> How about:
> const char a1[] = "test not const";
That means something totally different.
Something is very broken here. Maybe my understanding of the issue ;)
--
Dr. Horst H. von Brand mailto:vonbrand@inf.utfsm.cl
Departamento de Informatica Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria +56 32 654239
Casilla 110-V, Valparaiso, Chile Fax: +56 32 797513
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: string is const char [] ?
1998-06-24 10:10 ` Joe Buck
@ 1998-06-24 14:50 ` Alexandre Oliva
0 siblings, 0 replies; 14+ messages in thread
From: Alexandre Oliva @ 1998-06-24 14:50 UTC (permalink / raw)
To: Joe Buck; +Cc: kamil, egcs, egcs-bugs
Joe Buck <jbuck@Synopsys.COM> writes:
> On 23 Jun 1998, Alexandre Oliva wrote:
>> >> > char a1[] = "test not const";
> I don't believe that you are correct, Alexandre.
Thank you all for pointing out my mistake. When I was looking for the
requirement in the FDIS that stated that strings are char const[], I
came across the following statement, that surprised me:
Example:
char array[4] = "abcd"; // valid C, invalid C++
which I took in a more general sense than it should have been taken,
because I didn't read the text just before it [diff.decl]:
4 Change: In C++, when initializing an array of character with a string,
the number of characters in the string (including the terminating
'\0') must not exceed the number of elements in the array. [...]
What I was really looking for was [diff.lex]:
4 Change: String literals made const
The type of a string literal is changed from "array of char" to "array
of const char." The type of a wide string literal is changed from
"array of wchar_t" to "array of const wchar_t."
[...]
char* p = "abc"; // valid in C, deprecated in C++
char* q = expr ? "abc" : "de"; // valid in C, invalid in C++
[conv.array]:
2 A string literal (_lex.string_) that is not a wide string literal can
be converted to an rvalue of type "pointer to char"; [...]
Note: this conversion is deprecated.
Sorry for spreading misinformation.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: string is const char [] ?
1998-06-24 10:08 ` Kamil Iskra
@ 1998-06-24 21:23 ` Richard Henderson
1998-06-25 9:19 ` Kaz Kylheku
1998-06-25 9:59 ` Kaz Kylheku
0 siblings, 2 replies; 14+ messages in thread
From: Richard Henderson @ 1998-06-24 21:23 UTC (permalink / raw)
To: Kamil Iskra; +Cc: egcs, egcs-bugs
On Wed, Jun 24, 1998 at 03:07:30PM +0200, Kamil Iskra wrote:
> Don't get me wrong, I'm sure you know the standard far better than I do,
> but are you SURE that it disallows this initialisation syntax?
For non-constant static or file-scope variables, the "" form is
legal. For automatic variables, it is not.
> I find it
> hard to believe, since first, I think this syntax is very common (at least
> I use it quite often), and second, this syntax is fine in C,
Well, gcc provides the "" form for automatic variables as an extension.
I wish it did not, for I often find folks initializing things at runtime
taking up hordes of .text space, when they weren't intending to.
The correct solution to init auto variables is this little-known
function called strcpy.
r~
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: string is const char [] ?
1998-06-24 21:23 ` Richard Henderson
@ 1998-06-25 9:19 ` Kaz Kylheku
1998-06-25 9:59 ` Kaz Kylheku
1 sibling, 0 replies; 14+ messages in thread
From: Kaz Kylheku @ 1998-06-25 9:19 UTC (permalink / raw)
To: Richard Henderson; +Cc: Kamil Iskra, egcs, egcs-bugs
On Wed, 24 Jun 1998, Richard Henderson wrote:
> On Wed, Jun 24, 1998 at 03:07:30PM +0200, Kamil Iskra wrote:
> > Don't get me wrong, I'm sure you know the standard far better than I do,
> > but are you SURE that it disallows this initialisation syntax?
>
> For non-constant static or file-scope variables, the "" form is
> legal. For automatic variables, it is not.
Could you cite the relevant sections from either the C or C++ standards?
(Or both).
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: string is const char [] ?
1998-06-24 21:23 ` Richard Henderson
1998-06-25 9:19 ` Kaz Kylheku
@ 1998-06-25 9:59 ` Kaz Kylheku
1 sibling, 0 replies; 14+ messages in thread
From: Kaz Kylheku @ 1998-06-25 9:59 UTC (permalink / raw)
To: Richard Henderson; +Cc: Kamil Iskra, egcs, egcs-bugs
On Wed, 24 Jun 1998, Richard Henderson wrote:
> On Wed, Jun 24, 1998 at 03:07:30PM +0200, Kamil Iskra wrote:
> > Don't get me wrong, I'm sure you know the standard far better than I do,
> > but are you SURE that it disallows this initialisation syntax?
>
> For non-constant static or file-scope variables, the "" form is
> legal. For automatic variables, it is not.
>
> > I find it
> > hard to believe, since first, I think this syntax is very common (at least
> > I use it quite often), and second, this syntax is fine in C,
>
> Well, gcc provides the "" form for automatic variables as an extension.
> I wish it did not, for I often find folks initializing things at runtime
> taking up hordes of .text space, when they weren't intending to.
To clarify, you mean that:
{
char x[] = "abcd";
}
is wrong? That is perfectly valid C. But it does potentially take up
.text space, since the compiler will likely store the "abcd" string
literal there and generate a copy operation.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: string is const char [] ?
1998-06-25 18:53 ` Carlo Wood
@ 1998-06-25 21:48 ` Oleg Zabluda
0 siblings, 0 replies; 14+ messages in thread
From: Oleg Zabluda @ 1998-06-25 21:48 UTC (permalink / raw)
To: egcs
In article < 199806251626.SAA05600@jolan.ppro > you wrote:
: | Here is a real bug in egcs:
: |
: | void f( char *) {}
: | voif f(const char *) {}
: |
: | f("test not const"); // error in egcs. f(const char *) must be called,
: | // because the type of string literals now is
: | // array of n const char. However egcs calls
: | // f(char*). -Wwrite-strings has no effect.
: | // And even warning is surpressed.
: Shouldn't this be added as a test in the testsuite?
I think it should. I am sorry for not submitting a patch at this
time. I'll setup everything needed for generating good patches
Real Soon Now. I've posted this hoping somebody else would.
Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: string is const char [] ?
1998-06-25 0:25 Oleg Zabluda
@ 1998-06-25 18:53 ` Carlo Wood
1998-06-25 21:48 ` Oleg Zabluda
0 siblings, 1 reply; 14+ messages in thread
From: Carlo Wood @ 1998-06-25 18:53 UTC (permalink / raw)
To: egcs
| Here is a real bug in egcs:
|
| void f( char *) {}
| voif f(const char *) {}
|
| f("test not const"); // error in egcs. f(const char *) must be called,
| // because the type of string literals now is
| // array of n const char. However egcs calls
| // f(char*). -Wwrite-strings has no effect.
| // And even warning is surpressed.
|
| Oleg.
Shouldn't this be added as a test in the testsuite?
Carlo
----
PS
~>/usr/src/egcs/egcs-cvs-objdir/gcc/g++ -B/usr/src/egcs/egcs-cvs-objdir/gcc/ -I/usr/src/egcs/egcs-cvs/libstdc++ -I/usr/src/egcs/egcs-cvs/libio -static -L/usr/src/egcs/egcs-cvs-objdir/libraries/libstdc++ -Wwrite-strings tst.cc
~>a.out
char *
~>cat tst.cc
#include <iostream>
void f( char *) { cout << " char *" << endl; }
void f(const char *) { cout << "const char *" << endl; }
int main(void) { f("test not const"); }
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: string is const char [] ?
@ 1998-06-25 3:09 Ross Smith
0 siblings, 0 replies; 14+ messages in thread
From: Ross Smith @ 1998-06-25 3:09 UTC (permalink / raw)
To: egcs, egcs-bugs
From: Kamil Iskra <kamil@dwd.interkom.pl>
>
> char a1[] = "test not const";
>
>I didn't want a const array, I wanted a plain one, probably for a good
>reason. Maybe I wanted to change some characters or append something to
>it? Now, how should I do that to satisfy the standard and avoid the
much
>more error-prone standard array-initialisation syntax?
>
>Don't get me wrong, I'm sure you know the standard far better than I
do,
>but are you SURE that it disallows this initialisation syntax? I find
it
>hard to believe, since first, I think this syntax is very common (at
least
>I use it quite often), and second, this syntax is fine in C, and C++ is
>generally supposed to accept C code (I know, there are certain
>incompatibilities, and, IIRC, there is even a list of them. Is this
>particular incompatibility listed there?).
>
>If the standard indeed does deliberately disallow this initialisation
>syntax, I would vote that G++ allows it, preferably without any
warnings
>(unless compiling with -ansi, -pedantic or some such).
The standard *does* allow the above syntax. (Or at least CD2 did; I
haven't seen the FDIS but I haven't heard about any change in this
area.) The relevant section is 8.5.2 [dcl.init.string]. It makes a
special exception to the normal initialisation rules, allowing the
initialisation of a non-const char (or wchar_t) array with a string
literal.
(There is one change from the old C rules, though: if you specify the
array size, you can't leave out the terminating null. E.g. char foo[3] =
"bar"; is legal in C (giving an unterminated string) but illegal in
C++.)
--
Ross Smith ................................... mailto:ross.s@ihug.co.nz
.............. The Internet Group, Auckland, New Zealand ..............
"Remember when we told you there was no future? Well, this is it."
-- Blank Reg
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: string is const char [] ?
@ 1998-06-25 0:25 Oleg Zabluda
1998-06-25 18:53 ` Carlo Wood
0 siblings, 1 reply; 14+ messages in thread
From: Oleg Zabluda @ 1998-06-25 0:25 UTC (permalink / raw)
To: egcs
: On 23 Jun 1998, Alexandre Oliva wrote:
: > >> > char a1[] = "test not const";
: >
: > >> This line should cause egcs to flag an error.
: Kamil Iskra <kamil@dwd.interkom.pl> writes:
: > > Excuse me?! I missed the original mail, but do you really suggest that al
: > > the programmers should suddenly switch to:
: >
: > > char a1[]={'t', 'e', 's', 't', ' ', 'n', 'o', 't', ' ', 'c', 'o', 'n',
: > > 's', 't', '\0'};
: Alexandre Oliva writes:
: > How about:
: > const char a1[] = "test not const";
: Kamil is trying to initialize a char[], so your suggestion is irrelevant.
: Programmers must sometimes initialize a writable string, and the standard
: way to do this in C is exactly the way you claim is forbidden. This would
: need to be documented as a language incompatibility.
: I don't believe that you are correct, Alexandre. I think you are
: confusing assignment (where there is a conversion) with initialization.
: If the standard is unclear, this is a case where the committee should be
: asked to produce a clarification.
I think this is the rare case when Alexandre is confusing issues,
and the standard is very clear (8.5.2)
char a1[] = "test not const"; is definitely legal. What
is also legal, but deprecated is
char *a1 = "test not const";
This generates a warning when compiled with -Wwrite-strings.
Here is a real bug in egcs:
void f( char *) {}
voif f(const char *) {}
f("test not const"); // error in egcs. f(const char *) must be called,
// because the type of string literals now is
// array of n const char. However egcs calls
// f(char*). -Wwrite-strings has no effect.
// And even warning is surpressed.
Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~1998-06-25 21:48 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <orogvjbtlp.fsf@zecarneiro.lsd.dcc.unicamp.br>
1998-06-24 3:01 ` string is const char [] ? Kamil Iskra
1998-06-24 4:06 ` Alexandre Oliva
1998-06-24 10:08 ` Kamil Iskra
1998-06-24 21:23 ` Richard Henderson
1998-06-25 9:19 ` Kaz Kylheku
1998-06-25 9:59 ` Kaz Kylheku
1998-06-24 10:10 ` Joe Buck
1998-06-24 14:50 ` Alexandre Oliva
1998-06-24 10:10 ` jeroen dobbelaere xe44 7682
1998-06-24 12:27 ` Horst von Brand
1998-06-25 0:25 Oleg Zabluda
1998-06-25 18:53 ` Carlo Wood
1998-06-25 21:48 ` Oleg Zabluda
1998-06-25 3:09 Ross Smith
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).