Compiling the example g++ generates wrong code. I tried 5 other compiler from
different vendors and they generated an correct executable.

I tried different versions of the GNU Compiler, different Linux distributions
and plattforms.
All GNU compiler gave the same wrong results.
Using a debugger one can see the the arguments given to the function dimRand are
wrong.

The Output of the program should be:
9  CPs in this Block
CP added at 27 16 12
CP added at 16 19 19
CP added at 17 27 29
CP added at 13 35 0
CP added at 15 37 32
CP added at 24 27 44
CP added at 6 5 3
CP added at 20 18 5
CP added at 14 20 23

Compiled with the GNU g++ compiler the output is:
9  CPs in this Block
CP added at 7 16 45
CP added at 11 19 27
CP added at 17 27 30
CP added at 0 35 22
CP added at 19 37 26
CP added at 26 27 42
CP added at 2 5 10
CP added at 3 18 34
CP added at 14 20 24

The used command line was:

$ g++ -v -save-temps FlowFieldGenerator.cpp  -o test.exe -DDEBUG -Wall -g
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-libgcj-multifile
--enable-languages=c,c++,objc,java,f95,ada --enable-java-awt=gtk
--with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre 
--host=i386-redhat-linux
Thread model: posix
gcc version 4.0.1 20050727 (Red Hat 4.0.1-5)
 /usr/libexec/gcc/i386-redhat-linux/4.0.1/cc1plus -E -quiet -v -D_GNU_SOURCE
-DDEBUG FlowFieldGenerator.cpp -Wall -fworking-directory -fpch-preprocess -o
FlowFieldGenerator.ii
ignoring nonexistent directory
"/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1
 
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/i386-redhat-linux
 /usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/backward
 /usr/local/include
 /usr/lib/gcc/i386-redhat-linux/4.0.1/include
 /usr/include
End of search list.
 /usr/libexec/gcc/i386-redhat-linux/4.0.1/cc1plus -fpreprocessed
FlowFieldGenerator.ii -quiet -dumpbase FlowFieldGenerator.cpp -auxbase
FlowFieldGenerator -g -Wall -version -o FlowFieldGenerator.s
GNU C++ version 4.0.1 20050727 (Red Hat 4.0.1-5) (i386-redhat-linux)
 compiled by GNU C version 4.0.1 20050727 (Red Hat 4.0.1-5).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
 as -V -Qy -o FlowFieldGenerator.o FlowFieldGenerator.s
GNU assembler version 2.15.94.0.2.2 (i386-redhat-linux) using BFD version
2.15.94.0.2.2 20041220
 /usr/libexec/gcc/i386-redhat-linux/4.0.1/collect2 --eh-frame-hdr -m elf_i386
-dynamic-linker /lib/ld-linux.so.2 -o test.exe
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../crt1.o
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../crti.o
/usr/lib/gcc/i386-redhat-linux/4.0.1/crtbegin.o
-L/usr/lib/gcc/i386-redhat-linux/4.0.1 -L/usr/lib/gcc/i386-redhat-linux/4.0.1
-L/usr/lib/gcc/i386-redhat-linux/4.0.1/../../.. FlowFieldGenerator.o -lstdc++
-lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/usr/lib/gcc/i386-redhat-linux/4.0.1/crtend.o
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../crtn.o

Since I am not sure where to append the soure I will put in in here.
The Source Code of FlowFieldGenerator.cpp:


/*============================================================================*/
/*  INCLUDES                                                                  */
/*============================================================================*/
#include "FlowFieldGenerator.h"

//#include <vector>
#include <iostream>

/*============================================================================*/
// MAKROS / DEFINES
/*============================================================================*/
using namespace std;
#define SRANDOM_MAX 16777216
/*============================================================================*/
/*  CONSTRUCTORS / DESTRUCTOR                                                 */
/*============================================================================*/
/*============================================================================*/
/*============================================================================*/
static unsigned long long nextrandom = 1;

long long simplerandom(void) {
        nextrandom = nextrandom * 1103515245 + 12345;
        return((unsigned)(nextrandom/64) % SRANDOM_MAX);
}
void seedsimplerandom(unsigned seed) {
        nextrandom = seed;
}


int main ()
{
        CStrangeFieldFunction field(30,40,50);

}

CStrangeFieldFunction::CStrangeFieldFunction()
{
        walkx = 0.0;
        walky = 0.0;
        walkz = 0.0;
        stepsize = 0.01;
        xDim=1;
        yDim=1;
        zDim=1;
}
inline int CStrangeFieldFunction::dimRand(int dim)
{
        unsigned long long tmp = simplerandom();
        tmp *= dim-1;
        tmp /= SRANDOM_MAX;
        return tmp;
}
CStrangeFieldFunction::CStrangeFieldFunction(int x, int y, int z)
{
        array = new double [x*y*z*3];
        xDim=x;
        yDim=y;
        zDim=z;

// init array
        for (long int i =0; i < x*y*z*3; ++i)
        {
                array[i] = ((double)simplerandom())/SRANDOM_MAX;
        }
// add critical points to random positions
        long long tmpint = y*z * simplerandom() / SRANDOM_MAX / 100;
        std::cout << tmpint << "  CPs in this Block\n";
        for (int j = 0; j < tmpint; ++j) // don't depend linear on the number 
of cells,
so no x
        {
// gcc 3.3.5 3.4.4 4.0.0 4.0.1 has a bug here. dimRand is called with wrong 
value 
                GenCP (dimRand(xDim), dimRand(yDim), dimRand(zDim));
        }
        //GenCP (1, 1 , 1);
}
CStrangeFieldFunction::~CStrangeFieldFunction()
{
        if (array)
        {
                delete [] array;
        }
}
void CStrangeFieldFunction::GenCP (int x, int y, int z)
{
#ifdef DEBUG
        cout << "CP added at " << x << " " << y << " " << z << "\n";
#endif
//id 0
        array [3*yDim*xDim* z + 3*xDim*y + 3*x + 0] += 1;
        array [3*yDim*xDim* z + 3*xDim*y + 3*x + 1] += 1;
        array [3*yDim*xDim* z + 3*xDim*y + 3*x + 2] += 1;
//id 1
        array [3*yDim*xDim* z + 3*xDim*y + 3*(x+1) + 0] += 1;
        array [3*yDim*xDim* z + 3*xDim*y + 3*(x+1) + 1] += -1;
        array [3*yDim*xDim* z + 3*xDim*y + 3*(x+1) + 2] += 1;
//id 2
        array [3*yDim*xDim* z + 3*xDim*(y+1) + 3*(x+1) + 0] += 1;
        array [3*yDim*xDim* z + 3*xDim*(y+1) + 3*(x+1) + 1] += -1;
        array [3*yDim*xDim* z + 3*xDim*(y+1) + 3*(x+1) + 2] += -1;
//id 3
        array [3*yDim*xDim* z + 3*xDim*(y+1) + 3*x + 0] += 1;
        array [3*yDim*xDim* z + 3*xDim*(y+1) + 3*x + 1] += 1;
        array [3*yDim*xDim* z + 3*xDim*(y+1) + 3*x + 2] += -1;
//id 4
        array [3*yDim*xDim* (z+1) + 3*xDim*y + 3*x + 0] += -1;
        array [3*yDim*xDim* (z+1) + 3*xDim*y + 3*x + 1] += 1;
        array [3*yDim*xDim* (z+1) + 3*xDim*y + 3*x + 2] += 1;
//id 5
        array [3*yDim*xDim* (z+1) + 3*xDim*y + 3*(x+1) + 0] += -1;
        array [3*yDim*xDim* (z+1) + 3*xDim*y + 3*(x+1) + 1] += -1;
        array [3*yDim*xDim* (z+1) + 3*xDim*y + 3*(x+1) + 2] += 1;
//id 6
        array [3*yDim*xDim* (z+1) + 3*xDim*(y+1) + 3*(x+1) + 0] += -1;
        array [3*yDim*xDim* (z+1) + 3*xDim*(y+1) + 3*(x+1) + 1] += -1;
        array [3*yDim*xDim* (z+1) + 3*xDim*(y+1) + 3*(x+1) + 2] += -1;
//id 7
        array [3*yDim*xDim* (z+1) + 3*xDim*(y+1) + 3*x + 0] += -1;
        array [3*yDim*xDim* (z+1) + 3*xDim*(y+1) + 3*x + 1] += 1;
        array [3*yDim*xDim* (z+1) + 3*xDim*(y+1) + 3*x + 2] += -1;
}
/*============================================================================*/
/*  NAME      :   RandomWalk                                                  */
/*============================================================================*/
inline void CStrangeFieldFunction::RandomWalk (double * walk)
{
        double x = simplerandom()*2/SRANDOM_MAX ; // Random number 0 to 1
        if (x > 0.5)
        {
                (*walk) += stepsize;
        }
        else 
        {
                (*walk) -= stepsize;
        }
        return;
}
/*============================================================================*/
/*  NAME      :   ComputeValueX                                               */
/*============================================================================*/
double CStrangeFieldFunction::ComputeValueX (double xPos, double yPos, double 
zPos)
{       
        RandomWalk (&walkx);
        int x = (int) xPos;
        int y = (int) yPos;
        int z = (int) zPos;
        return array [3*yDim*xDim* z + 3*xDim*y + 3*x + 0];
}
/*============================================================================*/
/*  NAME      :   ComputeValueY                                               */
/*============================================================================*/
double CStrangeFieldFunction::ComputeValueY (double xPos, double yPos, double 
zPos)
{
        int x = (int) xPos;
        int y = (int) yPos;
        int z = (int) zPos;
    return array [3*yDim*xDim* z + 3*xDim*y + 3*x + 1];
}
/*============================================================================*/
/*  NAME      :   ComputeValueZ                                               */
/*============================================================================*/
double CStrangeFieldFunction::ComputeValueZ (double xPos, double yPos, double 
zPos)
{
        int x = (int) xPos;
        int y = (int) yPos;
        int z = (int) zPos;
    return  array [3*yDim*xDim* z + 3*xDim*y + 3*x + 2];
}
/*============================================================================*/
/*  NAME      :   ComputeScalar                                               */
/*============================================================================*/
double CStrangeFieldFunction::ComputeScalar (double xPos, double yPos, double 
zPos)
{
        int x = (int)  xPos;
        int y = (int)  yPos;
        int z = (int)  zPos;
    return  array [3*yDim*xDim* z + 3*xDim*y + 3*x + 0] + array [3*yDim*xDim* z
+ 3*xDim*y + 3*x + 1] + array [3*yDim*xDim* z + 3*xDim*y + 3*x + 2];
}
/*============================================================================*/

The source of FlowFieldGenerator.h

#if !defined(__FLOWFIELDGENERATOR_H)
#define __FLOWFIELDGENERATOR_H

#ifdef WIN32
#pragma warning( disable : 4786 )
#endif

/*============================================================================*/
// INCLUDES
/*============================================================================*/
#include <string>
#include <vector>

/*============================================================================*/
// MAKROS / DEFINES
/*============================================================================*/

/*============================================================================*/
/* FORWARD DECLARATIONS                                                       */
/*============================================================================*/


/*============================================================================*/
/* CLASS DEFINITIONS                                                          */
/*============================================================================*/

/**
 * CStrangeFieldFunction
 * 
 * @AUTHOR  Samuel Sarholz
 * @DATE    10.8.2005
 */    
class CStrangeFieldFunction
{
public:
        CStrangeFieldFunction();
        CStrangeFieldFunction(int x, int y, int z);
        ~CStrangeFieldFunction();
    double ComputeValueX (double xPos, double yPos, double zPos);
    double ComputeValueY (double xPos, double yPos, double zPos);
    double ComputeValueZ (double xPos, double yPos, double zPos);
    double ComputeScalar (double xPos, double yPos, double zPos);
private:
        double walkx;
        double walky;
        double walkz;
        double stepsize;
        double *array;
        int xDim, yDim, zDim;
        void GenCP (int x, int y, int z);
        inline void RandomWalk (double * walk);
        inline int dimRand(int dim);
};


#endif // __FLOWFIELDGENERATOR_H

/*============================================================================*/
/*  END OF FILE "FlowFieldGenerator.h"                                        */
/*============================================================================*/

-- 
           Summary: wrong arguments are given to a funtion
           Product: gcc
           Version: 4.0.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sarholz at rz dot rwth-aachen dot de
                CC: gcc-bugs at gcc dot gnu dot org


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

Reply via email to