NO

Author Topic: Compiler bug interpreting function call from function pointer returned by another function.  (Read 1652 times)

Offline risanchez

  • Member
  • *
  • Posts: 5
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!
« Last Edit: January 09, 2021, 03:45:01 PM by frankie »

Offline John Z

  • Member
  • *
  • Posts: 790
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

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
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.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Offline John Z

  • Member
  • *
  • Posts: 790
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

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
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.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Offline risanchez

  • Member
  • *
  • Posts: 5
Hi
I tried the parenthesis, but it does not work.

Offline risanchez

  • Member
  • *
  • Posts: 5
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.

Offline John Z

  • Member
  • *
  • Posts: 790
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

Offline risanchez

  • Member
  • *
  • Posts: 5
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.