Pelles C > Bug reports

Compiler bug interpreting function call from function pointer returned by another function.

(1/2) > >>

risanchez:
Hi everyone
Sorry for the delay in including the code that gives problem, this is:

--- Code: ---#include <stdio.h>
typedef double (*f)(double);
double f1(double);
double f2(double);
double f3(double);
double func(f, double);
f gama(double, double*);


int main(int argc, char *argv[])
{
    double x;
    int i;
    printf("Ingrese valor de X= ");
    scanf("%lf", &x);
    printf("Ingrese valor de i= ");
    scanf("%d", &i);
    f beta[3] = {f1, f2, f3};
    printf("Valor de polinomio= %f\n", func(beta[i], x));
    double m = 0.0;
    double w = gama(x, &m)(m);
    printf("valor de retorno desde funcion= %f\n",w);
    return 0;
}

double f1(double x)
{
    return x*x + 2.0*x + 1.0;
}
double f2(double x)
{
    return x*x*x ;
}
double f3(double x)
{
    return x*x + 2.0;
}

double func(f y, double x)
{
    return y(x);
}

f gama(double x, double* z)
{
    *z = x*2.0;
    return f1;
}

--- End code ---
This code gives me this answer when built:
Building main.obj.
fatal error: Internal error: 'Access violation' at 0x00007ff6cdf27b6a.
*** Error code: 1 ***
Done.

However it compile and works ok in CodeBlock and Visual Studio.
Also, it worked ok in Pelles 9.

Hope someone can tell me what is the problem.
Actually I switch to CodeBlock for my teaching.
Thanks!

John Z:
Hi,
It would be helpful if you use the PellesC project Zip feature and attach it here. That way anyone that wants to investigate will have the exact code and project settings.


John Z

frankie:
It is a bug of the compiler that happens only compiling for 64bits.
The message inform of an internal crash of the compiler.
Writing your program using a temporary variable works:

--- Code: ---#include <stdio.h>
typedef double (*f)(double);
double f1(double);
double f2(double);
double f3(double);
double func(f, double);
f gama(double, double*);


int main(int argc, char *argv[])
{
    double x;
    int i;
    printf("Ingrese valor de X= ");
    scanf("%lf", &x);
    printf("Ingrese valor de i= ");
    scanf("%d", &i);
    f beta[3] = {f1, f2, f3};
    printf("Valor de polinomio= %f\n", func(beta[i], x));
    double m = 0.0;
//    double w = gama(x, &m)(m);
f f_tmp = gama(x, &m);
double w = f_tmp(m);
    printf("valor de retorno desde funcion= %f\n",w);
    return 0;
}

double f1(double x)
{
    return x*x + 2.0*x + 1.0;
}
double f2(double x)
{
    return x*x*x ;
}
double f3(double x)
{
    return x*x + 2.0;
}

double func(f y, double x)
{
    return y(x);
}

f gama(double x, double* z)
{
    *z = x*2.0;
    return f1;
}

--- End code ---

I move the last posts under bug report.

John Z:
I mentioned two things to try:

1) try to breakout into a separate variable.   Frankie has shown this one :)
2) try using explicit parenthesis - this is below

for the explicit parenthesis try changing

--- Code: --- double w = gama(x, &m)(m);
--- End code ---
into

--- Code: --- double w = (gama(x, &m))(m);
--- End code ---

John Z

frankie:
John I have already tested the parenthesized version

--- Code: ---double w = (gama(x, &m))(m);
--- End code ---
and even the alternative

--- Code: ---double w = (*(gama(x, &m)))(m);
--- End code ---
and both give the same memory access error.

Navigation

[0] Message Index

[#] Next page

Go to full version