This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

bug in g++ in use of string literal with template


The attached source doesn't work as expected, at least under Windows.
I would expect the runtime output to be 5; it is 0.  (In other
configurations, it can be some other arbitrary value.)  Having looked
at the source code, I suspect that somewhere, string literals are
being treated too much like other literals in the optimization, and
that in certain contexts, a copy of the string literal is used, rather
than the original.

Compiler outout:

------------------------------------------------------------------------------
> g++ -v bug03.cc
g++ -v bug03.cc
Reading specs from /Program/Cygnus/cygwin-b20/H-i586-cygwin32/bin/../lib/gcc-lib/i586-cygwin32/2.95/specs
gcc version 2.95 19990728 (release)
 /Program/Cygnus/cygwin-b20/H-i586-cygwin32/bin/../lib/gcc-lib/i586-cygwin32/2.95/cpp.exe -lang-c++ -v -iprefix /Program/Cygnus/cygwin-b20/H-i586-cygwin32/bin/../lib/gcc-lib/i586-cygwin32/2.95/ -D__GNUC__=2 -D__GNUG__=2 -D__GNUC_MINOR__=95 -D__cplusplus -Di386 -D_WIN32 -DWINNT -D_X86_=1 -D__STDC__=1 -D__stdcall=__attribute__((__stdcall__)) -D__cdecl=__attribute__((__cdecl__)) -D__declspec(x)=__attribute__((x)) -D__i386__ -D_WIN32 -D__WINNT__ -D_X86_=1 -D__STDC__=1 -D__stdcall=__attribute__((__stdcall__)) -D__cdecl=__attribute__((__cdecl__)) -D__declspec(x)=__attribute__((x)) -D__i386 -D__WINNT -Asystem(winnt) -Acpu(i386) -Amachine(i386) -D__EXCEPTIONS -remap -Acpu(i386) -Amachine(i386) -Di386 -D__i386 -D__i386__ -Di586 -Dpentium -D__i586 -D__i586__ -D__pentium -D__pentium__ -D__CYGWIN32__ -D__CYGWIN__ bug03.cc c:\tmp/ccwIaaaa.ii
GNU CPP version 2.95 19990728 (release) (80386, BSD syntax)
#include "..." search starts here:
#include <...> search starts here:
 /Program/Cygnus/cygwin-b20/H-i586-cygwin32/bin/../lib/gcc-lib/i586-cygwin32/2.95/../../../../../include/g++-3
 /Program/Cygnus/cygwin-b20/H-i586-cygwin32/bin/../lib/gcc-lib/i586-cygwin32/2.95/../../../../../include
 /Program/Cygnus/cygwin-b20/H-i586-cygwin32/bin/../lib/gcc-lib/i586-cygwin32/2.95/../../../../i586-cygwin32/include
 /Program/Cygnus/cygwin-b20/H-i586-cygwin32/bin/../lib/gcc-lib/i586-cygwin32/2.95/include
End of search list.
The following default directories have been omitted from the search path:
 /usr/include
End of omitted list.
 /Program/Cygnus/cygwin-b20/H-i586-cygwin32/bin/../lib/gcc-lib/i586-cygwin32/2.95/cc1plus.exe c:\tmp/ccwIaaaa.ii -quiet -dumpbase bug03.cc -version -o c:\tmp/ccSgbaaa.s
GNU C++ version 2.95 19990728 (release) (i586-cygwin32) compiled by GNU C version 2.95 19990728 (release).
 /Program/Cygnus/cygwin-b20/H-i586-cygwin32/bin/../lib/gcc-lib/i586-cygwin32/2.95/../../../../i586-cygwin32/bin/as.exe -o c:\tmp/ccePbaaa.o c:\tmp/ccSgbaaa.s
 /Program/Cygnus/cygwin-b20/H-i586-cygwin32/bin/../lib/gcc-lib/i586-cygwin32/2.95/collect2.exe /Program/Cygnus/cygwin-b20/H-i586-cygwin32/bin/../lib/gcc-lib/i586-cygwin32/2.95/../../../../i586-cygwin32/lib/crt0.o -L/Program/Cygnus/cygwin-b20/H-i586-cygwin32/bin/../lib/gcc-lib/i586-cygwin32/2.95 -L/Program/Cygnus/cygwin-b20/H-i586-cygwin32/bin/../lib/gcc-lib -L/Program/Cygnus/cygwin-b20/H-i586-cygwin32/bin/../lib/gcc-lib/i586-cygwin32/2.95/../../../../i586-cygwin32/lib -L/Program/Cygnus/cygwin-b20/H-i586-cygwin32/bin/../lib/gcc-lib/i586-cygwin32/2.95/../../.. c:\tmp/ccePbaaa.o -lstdc++ -lm -lgcc -lcygwin -luser32 -lkernel32 -ladvapi32 -lshell32 -lgcc
------------------------------------------------------------------------------

Source.  The error is NOT in the implementation of vector, since I
originally encountered the error in one of my own classes.  Replacing
the first parameter of the constructor of v with "text + 0" suppresses
the bug -- I think you'll agree that whatever my own misunderstandings
about C++ (I know I've pestered you unnecessarily in the past),
whether I use "text" or "text + 0", the results should be the same.

------------------------------------------------------------------------------

#include <iostream.h>
#include <vector>

int
main()
{
    static char const   text[] = "test" ;
    vector< char >      v( text , text + sizeof( text ) ) ;
    cout << v.size() << '\n' ;
    return 0 ;
}

------------------------------------------------------------------------------

--
James Kanze                               mailto:kanze@gabi-soft.de
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
Ziegelhüttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]