#1650 Seg fault on mingw / gcc 4.7 with csqrt function

closed-fixed
nobody
MinGW (56)
2012-07-07
2012-06-21
clemens
No

Dear all,
I'm running a WinXP 32 bit system on an intel dual core CPU. As a compiler, I'm using the latest gcc version installed by the "mingw-get-inst-20120426.exe". I've only installed the C and C++ cumpiler and used the option "Download the latest...". The following code produces a seg fault which I was able to track down to the csqrt function. Debugging it, it gave me no hint at all what the problem is about.
I'm also running a gcc 4.6.3 build on two different Ubuntu versions, which works fine. It also _worked_ fine when I was using the prior mingw build of gcc 4.6.2. Here's the code:

**********************************************

#include <stdio.h>
#include <math.h>
#include <complex.h>

#ifndef complex
#define complex _Complex
#endif

typedef double complex cdoub;

cdoub ccangl_out(const cdoub ni, const cdoub nt, const double cangl_i, char *tir)
{
double t2=1.-cangl_i*cangl_i;
cdoub t1=1.-ni*ni/(nt*nt)*t2;
if(creal(t1)>0.)
{
*tir=0;
return csqrt(t1);
}
else if(creal(t1)==0.)
{
*tir=1;
return csqrt(t1);
}
else
{
*tir=1;
return -1.*csqrt(t1);
}
}

void testing(void)
{
cdoub no=1.,ni=1.39+(1.1e-7*1.i),c_out;
double c_in;
char tir;

for(c_in=.1;c_in<1.;c_in+=.01)
c_out=ccangl_out(ni,no,c_in,&tir);

printf("c_out: %g + %gi\n",creal(c_out),cimag(c_out));
}

int main(void)
{
testing();

return 1;
}

**********************************************

Kind regards
Clemens

Discussion

  • clemens
    clemens
    2012-06-22

    Hey, here is a simplified one:

    **********************************************

    #include <stdio.h>
    #include <math.h>
    #include <complex.h>

    double complex csqrt_out(const double complex ni)
    {
    return csqrt(ni);
    }

    int main(void)
    {
    double complex ni=1.+1.1e-7*1.i,c_out;

    c_out=csqrt(ni);
    printf("c_out: %g + %gi\n",creal(c_out),cimag(c_out));

    c_out=csqrt_out(ni);
    printf("c_out: %g + %gi\n",creal(c_out),cimag(c_out));

    return 1;
    }

    **********************************************

    Now, I get the output:

    c_out: 1.88963e+265 + 6.72985e+268i

    Process returned -1073741819 (0xC0000005)

    Which means, that only the second call produces a seg fault, but the first one a false result. It should read 1. + 5.5*10^-8 I

     
  • clemens
    clemens
    2012-06-22

    My gcc -v:

    Using built-in specs.
    COLLECT_GCC=gcc
    COLLECT_LTO_WRAPPER=e:/programme/gcc/bin/../libexec/gcc/mingw32/4.7.0/lto-wrapper.exe
    Target: mingw32
    Configured with: ../gcc-4.7.0/configure --enable-languages=c,c++,ada,fortran,objc,obj-c++ --disable-sjlj-exceptions --wi
    th-dwarf2 --enable-shared --enable-libgomp --disable-win32-registry --enable-libstdcxx-debug --disable-build-poststage1-
    with-cxx --enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw
    Thread model: win32
    gcc version 4.7.0 (GCC)

     
  • clemens
    clemens
    2012-07-07

    After a new update with the mingw-get utility it works.

     
  • clemens
    clemens
    2012-07-07

    • status: open --> closed-fixed