sLKz / picoc

Automatically exported from code.google.com/p/picoc
0 stars 0 forks source link

Adding native C library functions - Passing pointers as float* #155

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. add in your library host this function 

float testParams(char myChar,int myInt,float myFloat,char* myCharP,float* 
myFloatP,int* myIntP) {

    printf("myChar   = %c\n\r",myChar);
    printf("myInt    = %d\n\r",myInt);
    printf("myFloat  = %f\n\r",myFloat);
    if (myCharP)
      printf("myCharP  = %c\n\r",*myCharP);
    if (myFloatP)
      printf("myFloatP = %d\n\r",*myFloatP);
    if (myIntP)
      printf("myIntP   = %d\n\r",*myIntP);

    *myCharP = myChar;
    *myFloatP = myFloat;
    *myIntP = myInt;

    return (float)1.23456;
}

2. add also this callback function

void CTestParams(struct ParseState *Parser, struct Value *ReturnValue, struct 
Value **Param, int NumArgs)
{
        char* myChar = (char*)Param[3]->Val->Pointer;
    float* myFloat = (float*)Param[4]->Val->Pointer;
    int* myInt = (int*)Param[5]->Val->Pointer;

    ReturnValue->Val->FP = testParams(Param[0]->Val->Character,
                                      Param[1]->Val->Integer,
                                      (float)Param[2]->Val->FP,
                                      myChar,
                                      myFloat,
                                      myInt);
}

3. and in platform library add the new library function and prototype
...
    { CTestParams,  "float testParams(char,int,float,char*,float*,int*);" },
...

4. run pico and test this code
char myChar='N'; 
float myFloat=123.456; 
int myInt=654; 
testParams('A',123,456.789,&myChar,&myFloat,&myInt); 
printf("myChar= %c\n\r",myChar); 
printf("myFloat= %f\n\r",myFloat); 
printf("myInt= %d\n\r",myInt);

What is the expected output? What do you see instead?

Expected result:
myChar= A
myFloat= 456.789
myInt= 123

Result:
myChar= A
myFloat= some random value....
myInt= 123

What version of the product are you using? On what operating system?
2.2 beta

Please provide any additional information below.

Original issue reported on code.google.com by ozlbi...@gmail.com on 9 Feb 2012 at 7:29

GoogleCodeExporter commented 9 years ago
please correct in testParams function
      printf("myFloatP = %d\n\r",*myFloatP); 
with
      printf("myFloatP = %f\n\r",*myFloatP);

Anyway this is not the solution of the problem, since the issue is present in 
the pointer parameter ;-)

Original comment by ozlbi...@gmail.com on 9 Feb 2012 at 11:04

GoogleCodeExporter commented 9 years ago
this issue is solved;
instead of 
float* myFloat = (float*)Param[4]->Val->Pointer;
use
double* myFloat = (double*)Param[4]->Val->Pointer;

Float pointer params are always treated  as double pointer param.

Original comment by ozlbi...@gmail.com on 2 Apr 2013 at 6:38