Pelles C forum

Pelles C => Bug reports => Topic started by: risanchez on January 08, 2021, 10:01:03 PM

Title: Compiler bug interpreting function call from function pointer returned by another function.
Post by: risanchez on January 08, 2021, 10:01:03 PM
Hi everyone
Sorry for the delay in including the code that gives problem, this is:
Code: [Select]
#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;
}
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!
Title: Re: Compiler bug interpreting function call from function pointer returned by another function.
Post by: John Z on January 09, 2021, 02:55:29 AM
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
Title: Re: Compiler bug interpreting function call from function pointer returned by another function.
Post by: frankie on January 09, 2021, 04:01:18 PM
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: [Select]
#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;
}

I move the last posts under bug report.
Title: Re: Compiler bug interpreting function call from function pointer returned by another function.
Post by: John Z on January 09, 2021, 04:23:25 PM
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: [Select]
double w = gama(x, &m)(m);into
Code: [Select]
double w = (gama(x, &m))(m);
John Z
Title: Re: Compiler bug interpreting function call from function pointer returned by another function.
Post by: frankie on January 09, 2021, 05:59:42 PM
John I have already tested the parenthesized version
Code: [Select]
double w = (gama(x, &m))(m);and even the alternative
Code: [Select]
double w = (*(gama(x, &m)))(m);and both give the same memory access error.
Title: Re: Compiler bug interpreting function call from function pointer returned by another function.
Post by: risanchez on January 09, 2021, 07:03:24 PM
Hi
I tried the parenthesis, but it does not work.
Title: Re: Compiler bug interpreting function call from function pointer returned by another function.
Post by: risanchez on January 09, 2021, 07:30:50 PM
Hi
I tried the code in CLion, last version, and compile ok and execute
but it return wrong value for the return of the function.

I tried again in CodeBlock and compile and run OK! also the same in VS, no problem.
Title: Re: Compiler bug interpreting function call from function pointer returned by another function.
Post by: John Z on January 09, 2021, 09:38:36 PM
Thanks guys,

Oh well, it worked in some other situations. Thanks for testing.

Extinguishes my ideas, but at least frankie’s workaround does work.

I think Pelle is looking into a fix in the next version.


John Z
Title: Re: Compiler bug interpreting function call from function pointer returned by another function.
Post by: risanchez on January 09, 2021, 10:01:06 PM
Hi
yes, using frankie temporary variable it works!
but, it loose the nice concept of return of the function from where it is called.

Hope this can be fixed in a new version of Pelles.