Bug#259318: pass by reference broken
Package: g++-3.3
Version: 1:3.3.4-2
Pass by reference seems to be broken in g++.
Originally I thought the problem might be in ntohl, which is why it
appears in the test below. However, the real problem seems to be in the
mechanics of passing data. A different variant of the program below
showed no problems in ntohl. I also considered casting as the source of
the problem, but that also seems to work fine.
The program below gave the following output when compiled with g++-3.3
3.3.4 (Debian 1:3.3.4-2) :
alan@1[alan]$ ./testp
cast test: tmp is 4294967295, result is -1
ntohl test: result is -1
And the following correct output when compiled with g++-3.2 3.2.3 (Debian):
alan@1[alan]$ ./testp
cast test: tmp is 4294967295, result is -1
ntohl test: result is -6
This seems to show that result might have been passed by value instead
of by reference, since the value of result does not change.
Here is the test program:
// ------------begin test program---------------
#include <stdio.h>
#include <netinet/in.h>
#include <stdint.h>
uint32_t Read (uint32_t& v, char * buf);
int32_t Read (int32_t& v, char * buf);
int
main()
{
char buffer[] = {
255, 255, 255, 250 // -6 in network byte order
};
int32_t result = -1;
uint32_t tmp = static_cast<uint32_t>(result);
result = static_cast<int32_t>(tmp);
printf("cast test: tmp is %u, result is %d\n", tmp, result);
Read(result, buffer);
printf("ntohl test: result is %d\n", result);
return 0;
}
int32_t
Read(int32_t& v, char * buf)
{
Read(static_cast<uint32_t>(v), buf);
return v;
}
uint32_t
Read(uint32_t& v, char * buf)
{
v = ntohl(*(reinterpret_cast<uint32_t*>(buf)));
return v;
}
//------------end test program------------------------------
Reply to: