[Bug c++/13851] New: GCC 3.3.x mishandles pass by reference casted pointer

ladd at spiny dot com gcc-bugzilla@gcc.gnu.org
Sun Jan 25 02:38:00 GMT 2004


When passing a casted pointer by reference to a function, the called function cannot return a value 
in the pass-by-reference parameter. Instead, the pointer preserves its original value in the calling 
function. If the pointer is uncasted, the return-by-reference succeeds.

This worked as expected in GCC 2.96, but now silently fails without error or warning. I've tested on 
a fresh build of 3.3.2 on RedHat Linux and Apple's version of GCC 3.3 on Mac OS X.

Sample code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void func (unsigned char * &p);

int main (void)
{
	char *test = 0;
	
	func((unsigned char *) test);
	printf ("test is %s\n", test);
	return 0;
}

void func (unsigned char * &p)
{
	p = (unsigned char *) malloc(10);
	strcpy((char *) p, "hello!");
}

Expected result: printf should produce "hello!".
Actual result:  "(null)" is produced.

I am not a language lawyer, so the C++ spec may actually mandate this behavior. If so, a warning 
would be appreciated, as it produced a pretty puzzling bug when upgrading from 2.96 to 3.3.2

If, as I suspect, this behavior is incorrect, a fix would be in order, as it causes sneakily incorrect 
code generation.

-- 
           Summary: GCC 3.3.x mishandles pass by reference casted pointer
           Product: gcc
           Version: 3.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: ladd at spiny dot com
                CC: gcc-bugs at gcc dot gnu dot org


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



More information about the Gcc-bugs mailing list