public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/16335] New: passing non-const pointer to const pointer reference causes copy
@ 2004-07-02 19:52 cnbfxc at mizzou dot edu
  2004-07-02 20:36 ` [Bug c++/16335] " pinskia at gcc dot gnu dot org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: cnbfxc at mizzou dot edu @ 2004-07-02 19:52 UTC (permalink / raw)
  To: gcc-bugs

Here I pass a non-const int pointer to a constructor which accepts a reference
to a const-pointer to a const-int. Then it seems a new copy of the pointer is
made because changes to the original pointer address do not affect the
pointer-reference, and the addresses of the original pointer and the reference
are different.

For example, the following code give the following output:
Borland Builder 5.5: 7,7
Digital Mars: 7,7
g++ 3.3.1: 7,5

command line: \dev\gcc\bin\g++ -save-temps testrefs.cpp

version info:
Reading specs from C:/DEV/GCC/BIN/../lib/gcc-lib/mingw32/3.3.1/specs
Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as --host=
mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls --enable
-languages=c,c++,f77,objc,ada,java --disable-win32-registry --disable-shared --e
nable-sjlj-exceptions --enable-libgcj --disable-java-awt --without-x --enable-ja
va-gc=boehm --disable-libgcj-debug --enable-interpreter --enable-hash-synchroniz
ation
Thread model: win32
gcc version 3.3.1 (mingw special 20030804-1)

code:

# 1 "testrefs.cpp"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "testrefs.cpp"
# 1 "C:/DEV/GCC/include/c++/3.3.1/cstdio" 1 3
# 48 "C:/DEV/GCC/include/c++/3.3.1/cstdio" 3

# 1 "C:/DEV/GCC/include/c++/3.3.1/mingw32/bits/c++config.h" 1 3
# 35 "C:/DEV/GCC/include/c++/3.3.1/mingw32/bits/c++config.h" 3
# 1 "C:/DEV/GCC/include/c++/3.3.1/mingw32/bits/os_defines.h" 1 3
# 36 "C:/DEV/GCC/include/c++/3.3.1/mingw32/bits/c++config.h" 2 3
# 50 "C:/DEV/GCC/include/c++/3.3.1/cstdio" 2 3
# 1 "C:/DEV/GCC/include/c++/3.3.1/cstddef" 1 3
# 47 "C:/DEV/GCC/include/c++/3.3.1/cstddef" 3

# 1 "C:/DEV/GCC/include/stddef.h" 1 3





# 1 "C:/DEV/GCC/lib/gcc-lib/mingw32/3.3.1/include/stddef.h" 1 3 4
# 158 "C:/DEV/GCC/lib/gcc-lib/mingw32/3.3.1/include/stddef.h" 3 4
typedef int ptrdiff_t;
# 220 "C:/DEV/GCC/lib/gcc-lib/mingw32/3.3.1/include/stddef.h" 3 4
typedef unsigned int size_t;
# 7 "C:/DEV/GCC/include/stddef.h" 2 3
# 49 "C:/DEV/GCC/include/c++/3.3.1/cstddef" 2 3

namespace std
{
  using ::ptrdiff_t;
  using ::size_t;
}
# 51 "C:/DEV/GCC/include/c++/3.3.1/cstdio" 2 3

# 1 "C:/DEV/GCC/include/stdio.h" 1 3
# 19 "C:/DEV/GCC/include/stdio.h" 3
# 1 "C:/DEV/GCC/include/_mingw.h" 1 3
# 20 "C:/DEV/GCC/include/stdio.h" 2 3






# 1 "C:/DEV/GCC/include/stddef.h" 1 3





# 1 "C:/DEV/GCC/lib/gcc-lib/mingw32/3.3.1/include/stddef.h" 1 3 4
# 361 "C:/DEV/GCC/lib/gcc-lib/mingw32/3.3.1/include/stddef.h" 3 4
typedef short unsigned int wint_t;
# 7 "C:/DEV/GCC/include/stddef.h" 2 3
# 27 "C:/DEV/GCC/include/stdio.h" 2 3

# 1 "C:/DEV/GCC/include/stdarg.h" 1 3





# 1 "C:/DEV/GCC/lib/gcc-lib/mingw32/3.3.1/include/stdarg.h" 1 3 4
# 44 "C:/DEV/GCC/lib/gcc-lib/mingw32/3.3.1/include/stdarg.h" 3 4
typedef __builtin_va_list __gnuc_va_list;
# 7 "C:/DEV/GCC/include/stdarg.h" 2 3
# 29 "C:/DEV/GCC/include/stdio.h" 2 3
# 135 "C:/DEV/GCC/include/stdio.h" 3
typedef struct _iobuf
{
        char* _ptr;
        int _cnt;
        char* _base;
        int _flag;
        int _file;
        int _charbuf;
        int _bufsiz;
        char* _tmpfname;
} FILE;
# 160 "C:/DEV/GCC/include/stdio.h" 3
extern __attribute__((dllimport)) FILE _iob[];
# 169 "C:/DEV/GCC/include/stdio.h" 3
extern "C" {





 FILE* __attribute__((__cdecl__)) fopen (const char*, const char*);
 FILE* __attribute__((__cdecl__)) freopen (const char*, const char*, FILE*);
 int __attribute__((__cdecl__)) fflush (FILE*);
 int __attribute__((__cdecl__)) fclose (FILE*);

 int __attribute__((__cdecl__)) remove (const char*);
 int __attribute__((__cdecl__)) rename (const char*, const char*);
 FILE* __attribute__((__cdecl__)) tmpfile (void);
 char* __attribute__((__cdecl__)) tmpnam (char*);


 char* __attribute__((__cdecl__)) _tempnam (const char*, const char*);
 int __attribute__((__cdecl__)) _rmtmp(void);


 char* __attribute__((__cdecl__)) tempnam (const char*, const char*);
 int __attribute__((__cdecl__)) rmtmp(void);



 int __attribute__((__cdecl__)) setvbuf (FILE*, char*, int, size_t);

 void __attribute__((__cdecl__)) setbuf (FILE*, char*);





 int __attribute__((__cdecl__)) fprintf (FILE*, const char*, ...);
 int __attribute__((__cdecl__)) printf (const char*, ...);
 int __attribute__((__cdecl__)) sprintf (char*, const char*, ...);
 int __attribute__((__cdecl__)) _snprintf (char*, size_t, const char*, ...);
 int __attribute__((__cdecl__)) vfprintf (FILE*, const char*, __gnuc_va_list);
 int __attribute__((__cdecl__)) vprintf (const char*, __gnuc_va_list);
 int __attribute__((__cdecl__)) vsprintf (char*, const char*, __gnuc_va_list);
 int __attribute__((__cdecl__)) _vsnprintf (char*, size_t, const char*,
__gnuc_va_list);


int __attribute__((__cdecl__)) snprintf(char* s, size_t n, const char* format, ...);
inline int __attribute__((__cdecl__))
vsnprintf (char* s, size_t n, const char* format, __gnuc_va_list arg)
  { return _vsnprintf ( s, n, format, arg); }
int __attribute__((__cdecl__)) vscanf (const char * __restrict__, __gnuc_va_list);
int __attribute__((__cdecl__)) vfscanf (FILE * __restrict__, const char *
__restrict__,
                     __gnuc_va_list);
int __attribute__((__cdecl__)) vsscanf (const char * __restrict__,
                     const char * __restrict__, __gnuc_va_list);






 int __attribute__((__cdecl__)) fscanf (FILE*, const char*, ...);
 int __attribute__((__cdecl__)) scanf (const char*, ...);
 int __attribute__((__cdecl__)) sscanf (const char*, const char*, ...);




 int __attribute__((__cdecl__)) fgetc (FILE*);
 char* __attribute__((__cdecl__)) fgets (char*, int, FILE*);
 int __attribute__((__cdecl__)) fputc (int, FILE*);
 int __attribute__((__cdecl__)) fputs (const char*, FILE*);
 char* __attribute__((__cdecl__)) gets (char*);
 int __attribute__((__cdecl__)) puts (const char*);
 int __attribute__((__cdecl__)) ungetc (int, FILE*);







 int __attribute__((__cdecl__)) _filbuf (FILE*);
 int __attribute__((__cdecl__)) _flsbuf (int, FILE*);



inline int __attribute__((__cdecl__)) getc (FILE* __F)
{
  return (--__F->_cnt >= 0)
    ? (int) (unsigned char) *__F->_ptr++
    : _filbuf (__F);
}

inline int __attribute__((__cdecl__)) putc (int __c, FILE* __F)
{
  return (--__F->_cnt >= 0)
    ? (int) (unsigned char) (*__F->_ptr++ = (char)__c)
    : _flsbuf (__c, __F);
}

inline int __attribute__((__cdecl__)) getchar (void)
{
  return (--(&_iob[0])->_cnt >= 0)
    ? (int) (unsigned char) *(&_iob[0])->_ptr++
    : _filbuf ((&_iob[0]));
}

inline int __attribute__((__cdecl__)) putchar(int __c)
{
  return (--(&_iob[1])->_cnt >= 0)
    ? (int) (unsigned char) (*(&_iob[1])->_ptr++ = (char)__c)
    : _flsbuf (__c, (&_iob[1]));}
# 294 "C:/DEV/GCC/include/stdio.h" 3
 size_t __attribute__((__cdecl__)) fread (void*, size_t, size_t, FILE*);
 size_t __attribute__((__cdecl__)) fwrite (const void*, size_t, size_t, FILE*);





 int __attribute__((__cdecl__)) fseek (FILE*, long, int);
 long __attribute__((__cdecl__)) ftell (FILE*);
 void __attribute__((__cdecl__)) rewind (FILE*);
# 327 "C:/DEV/GCC/include/stdio.h" 3
typedef long long fpos_t;




 int __attribute__((__cdecl__)) fgetpos (FILE*, fpos_t*);
 int __attribute__((__cdecl__)) fsetpos (FILE*, const fpos_t*);





 int __attribute__((__cdecl__)) feof (FILE*);
 int __attribute__((__cdecl__)) ferror (FILE*);


inline int __attribute__((__cdecl__)) feof (FILE* __F)
  { return __F->_flag & 0x0010; }
inline int __attribute__((__cdecl__)) ferror (FILE* __F)
  { return __F->_flag & 0x0020; }





 void __attribute__((__cdecl__)) clearerr (FILE*);
 void __attribute__((__cdecl__)) perror (const char*);






 FILE* __attribute__((__cdecl__)) _popen (const char*, const char*);
 int __attribute__((__cdecl__)) _pclose (FILE*);


 FILE* __attribute__((__cdecl__)) popen (const char*, const char*);
 int __attribute__((__cdecl__)) pclose (FILE*);





 int __attribute__((__cdecl__)) _flushall (void);
 int __attribute__((__cdecl__)) _fgetchar (void);
 int __attribute__((__cdecl__)) _fputchar (int);
 FILE* __attribute__((__cdecl__)) _fdopen (int, const char*);
 int __attribute__((__cdecl__)) _fileno (FILE*);
 int __attribute__((__cdecl__)) _fcloseall(void);
 FILE* __attribute__((__cdecl__)) _fsopen(const char*, const char*, int);

 int __attribute__((__cdecl__)) _getmaxstdio(void);
 int __attribute__((__cdecl__)) _setmaxstdio(int);



 int __attribute__((__cdecl__)) fgetchar (void);
 int __attribute__((__cdecl__)) fputchar (int);
 FILE* __attribute__((__cdecl__)) fdopen (int, const char*);
 int __attribute__((__cdecl__)) fileno (FILE*);
# 401 "C:/DEV/GCC/include/stdio.h" 3
 int __attribute__((__cdecl__)) fwprintf (FILE*, const wchar_t*, ...);
 int __attribute__((__cdecl__)) wprintf (const wchar_t*, ...);
 int __attribute__((__cdecl__)) swprintf (wchar_t*, const wchar_t*, ...);
 int __attribute__((__cdecl__)) _snwprintf (wchar_t*, size_t, const wchar_t*, ...);
 int __attribute__((__cdecl__)) vfwprintf (FILE*, const wchar_t*, __gnuc_va_list);
 int __attribute__((__cdecl__)) vwprintf (const wchar_t*, __gnuc_va_list);
 int __attribute__((__cdecl__)) vswprintf (wchar_t*, const wchar_t*,
__gnuc_va_list);
 int __attribute__((__cdecl__)) _vsnwprintf (wchar_t*, size_t, const wchar_t*,
__gnuc_va_list);
 int __attribute__((__cdecl__)) fwscanf (FILE*, const wchar_t*, ...);
 int __attribute__((__cdecl__)) wscanf (const wchar_t*, ...);
 int __attribute__((__cdecl__)) swscanf (const wchar_t*, const wchar_t*, ...);
 wint_t __attribute__((__cdecl__)) fgetwc (FILE*);
 wint_t __attribute__((__cdecl__)) fputwc (wchar_t, FILE*);
 wint_t __attribute__((__cdecl__)) ungetwc (wchar_t, FILE*);


 wchar_t* __attribute__((__cdecl__)) fgetws (wchar_t*, int, FILE*);
 int __attribute__((__cdecl__)) fputws (const wchar_t*, FILE*);
 wint_t __attribute__((__cdecl__)) getwc (FILE*);
 wint_t __attribute__((__cdecl__)) getwchar (void);
 wchar_t* __attribute__((__cdecl__)) _getws (wchar_t*);
 wint_t __attribute__((__cdecl__)) putwc (wint_t, FILE*);
 int __attribute__((__cdecl__)) _putws (const wchar_t*);
 wint_t __attribute__((__cdecl__)) putwchar (wint_t);
 FILE* __attribute__((__cdecl__)) _wfdopen(int, wchar_t *);
 FILE* __attribute__((__cdecl__)) _wfopen (const wchar_t*, const wchar_t*);
 FILE* __attribute__((__cdecl__)) _wfreopen (const wchar_t*, const wchar_t*, FILE*);
 FILE* __attribute__((__cdecl__)) _wfsopen (const wchar_t*, const wchar_t*, int);
 wchar_t* __attribute__((__cdecl__)) _wtmpnam (wchar_t*);
 wchar_t* __attribute__((__cdecl__)) _wtempnam (const wchar_t*, const wchar_t*);
 int __attribute__((__cdecl__)) _wrename (const wchar_t*, const wchar_t*);
 int __attribute__((__cdecl__)) _wremove (const wchar_t*);
 void __attribute__((__cdecl__)) _wperror (const wchar_t*);
 FILE* __attribute__((__cdecl__)) _wpopen (const wchar_t*, const wchar_t*);



int __attribute__((__cdecl__)) snwprintf (wchar_t* s, size_t n, const wchar_t*
format, ...);
inline int __attribute__((__cdecl__))
vsnwprintf (wchar_t* s, size_t n, const wchar_t* format, __gnuc_va_list arg)
  { return _vsnwprintf ( s, n, format, arg);}
int __attribute__((__cdecl__)) vwscanf (const wchar_t * __restrict__,
__gnuc_va_list);
int __attribute__((__cdecl__)) vfwscanf (FILE * __restrict__,
                       const wchar_t * __restrict__, __gnuc_va_list);
int __attribute__((__cdecl__)) vswscanf (const wchar_t * __restrict__,
                       const wchar_t * __restrict__, __gnuc_va_list);
# 455 "C:/DEV/GCC/include/stdio.h" 3
 FILE* __attribute__((__cdecl__)) wpopen (const wchar_t*, const wchar_t*);






 wint_t __attribute__((__cdecl__)) _fgetwchar (void);
 wint_t __attribute__((__cdecl__)) _fputwchar (wint_t);
 int __attribute__((__cdecl__)) _getw (FILE*);
 int __attribute__((__cdecl__)) _putw (int, FILE*);


 wint_t __attribute__((__cdecl__)) fgetwchar (void);
 wint_t __attribute__((__cdecl__)) fputwchar (wint_t);
 int __attribute__((__cdecl__)) getw (FILE*);
 int __attribute__((__cdecl__)) putw (int, FILE*);





}
# 53 "C:/DEV/GCC/include/c++/3.3.1/cstdio" 2 3
# 97 "C:/DEV/GCC/include/c++/3.3.1/cstdio" 3
namespace std
{
  using ::FILE;
  using ::fpos_t;

  using ::clearerr;
  using ::fclose;
  using ::feof;
  using ::ferror;
  using ::fflush;
  using ::fgetc;
  using ::fgetpos;
  using ::fgets;
  using ::fopen;
  using ::fprintf;
  using ::fputc;
  using ::fputs;
  using ::fread;
  using ::freopen;
  using ::fscanf;
  using ::fseek;
  using ::fsetpos;
  using ::ftell;
  using ::fwrite;
  using ::getc;
  using ::getchar;
  using ::gets;
  using ::perror;
  using ::printf;
  using ::putc;
  using ::putchar;
  using ::puts;
  using ::remove;
  using ::rename;
  using ::rewind;
  using ::scanf;
  using ::setbuf;
  using ::setvbuf;
  using ::sprintf;
  using ::sscanf;
  using ::tmpfile;
  using ::tmpnam;
  using ::ungetc;
  using ::vfprintf;
  using ::vprintf;
  using ::vsprintf;
}
# 153 "C:/DEV/GCC/include/c++/3.3.1/cstdio" 3
namespace __gnu_cxx
{
# 167 "C:/DEV/GCC/include/c++/3.3.1/cstdio" 3
  using ::snprintf;
  using ::vfscanf;
  using ::vscanf;
  using ::vsnprintf;
  using ::vsscanf;

}

namespace std
{
  using __gnu_cxx::snprintf;
  using __gnu_cxx::vfscanf;
  using __gnu_cxx::vscanf;
  using __gnu_cxx::vsnprintf;
  using __gnu_cxx::vsscanf;
}
# 2 "testrefs.cpp" 2
using namespace std;

class tr {
public:
        const int *const &i;
        tr(const int *const &j);
};

tr::tr(const int *const &j) : i(j) {}

const int* f(const int *a) { return a; }

int main() {
        int a = 5, b=7;
        int *p = &a;

        tr mytr(p);

        p=&b;

   printf("%i, %i\n", *p, *mytr.i);
        return 0;
}

-- 
           Summary: passing non-const pointer to const pointer reference
                    causes copy
           Product: gcc
           Version: 3.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: cnbfxc at mizzou dot edu
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16335


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/16335] passing non-const pointer to const pointer reference causes copy
  2004-07-02 19:52 [Bug c++/16335] New: passing non-const pointer to const pointer reference causes copy cnbfxc at mizzou dot edu
@ 2004-07-02 20:36 ` pinskia at gcc dot gnu dot org
  2004-07-02 20:54 ` bangerth at dealii dot org
  2004-07-02 20:56 ` bangerth at dealii dot org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-07-02 20:36 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-07-02 20:36 -------
Note ICC 6.0, has the same output as GCC.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16335


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/16335] passing non-const pointer to const pointer reference causes copy
  2004-07-02 19:52 [Bug c++/16335] New: passing non-const pointer to const pointer reference causes copy cnbfxc at mizzou dot edu
  2004-07-02 20:36 ` [Bug c++/16335] " pinskia at gcc dot gnu dot org
@ 2004-07-02 20:54 ` bangerth at dealii dot org
  2004-07-02 20:56 ` bangerth at dealii dot org
  2 siblings, 0 replies; 4+ messages in thread
From: bangerth at dealii dot org @ 2004-07-02 20:54 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-07-02 20:54 -------
gccs behavior seems correct to me: the constructor expects a 
reference to const int*, whereas you provide it with an int*. 
The latter is not a const-qualified version of the former, but 
they are convertible to the former type, so the compiler  
generates a temporary of type const int* and takes the reference 
to this temporary. (Note that here you are on thin ice anyway, 
since you store the address of a temporary, which invokes undefined 
behavior.) 
 
To fix your code, present the constructor with a const int*, like so 
  const int *p = &a; 
With this, you get the desired output. 
 
W. 

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16335


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/16335] passing non-const pointer to const pointer reference causes copy
  2004-07-02 19:52 [Bug c++/16335] New: passing non-const pointer to const pointer reference causes copy cnbfxc at mizzou dot edu
  2004-07-02 20:36 ` [Bug c++/16335] " pinskia at gcc dot gnu dot org
  2004-07-02 20:54 ` bangerth at dealii dot org
@ 2004-07-02 20:56 ` bangerth at dealii dot org
  2 siblings, 0 replies; 4+ messages in thread
From: bangerth at dealii dot org @ 2004-07-02 20:56 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-07-02 20:56 -------
BTW, the name of this PR should read 
  Passing pointer-to-non-const for pointer-to-const causes copy 
Maybe that explains the matter somewhat. 
 
W. 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16335


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-07-02 20:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-07-02 19:52 [Bug c++/16335] New: passing non-const pointer to const pointer reference causes copy cnbfxc at mizzou dot edu
2004-07-02 20:36 ` [Bug c++/16335] " pinskia at gcc dot gnu dot org
2004-07-02 20:54 ` bangerth at dealii dot org
2004-07-02 20:56 ` bangerth at dealii dot org

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).