pointer to a function return issue

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

pointer to a function return issue

989 Views
Abhi_Ec
Contributor I

I am using MC9S12XEQ384.

In my application i have used various function having function pointer as a parameter.The problem is that i am not  getting the exact memory address of the function pointed by the pointer..

 

One such function is written below:-

 

f1(char* f2(1), int p,  int j)

{

 //body of the function.

}

 

Char* f2(int i)

{

switch( i )
      {
         case 0:
           return f3;

         case 1:
            return f4;

    }        

}

 

char f3( );

char f4( );

 

 

In memory f3 is in PAGE 0xFO and f4 function is in PAGE 0XF0, with address 9832 and 9856.

So, the complete address of  function f3 anf f4 become 0xF09832 and 0XF09856.

 

But, when it is retured from function f2 the pointer is pointing to 0x9832F000 for f3 instead of 0xF09832 and 0x9856F000 for f4 function instead of 0xF09856 depending upon the value of i passed to f2 function.

 

Do you this my code is not working properly.

 

Can anybody tell me what is the exact issue and how should i fix it.

Labels (1)
0 Kudos
4 Replies

505 Views
Pedro_
Contributor III

Hi,

Have you managed to solve this problem already?

 

I believe you are missing the @far modifier Cosmic uses to qualify functions in paged memory.

In the same way, the pointers will have to be qualified @far for consistency.

Cosmic should then be able to resolve the 4 bytes addresses.

 

0 Kudos

505 Views
CompilerGuru
NXP Employee
NXP Employee

Can you provide a compilable sample?

In the provided one, f1 does not have a return type, Char is undefined, f3,f4 are used before being defined.

f2 is used as parameter name and as function name, I guess as hint that the function is passed as this parameter.

 

Functions are allocated banked, so I guess this is with the banked memory model, is this correct? Which compiler options, which compiler, which version,....

 

I'm a bit confused by "0x9856F000". The compiler is not using 4 bytes for pointers, is the code somehow also using longs for pointer values?

 

Anyhow, there is no issue if you keep the types clean. Especially for the S12, __far data and __far function pointers do not mix directly, they use a different byte ordering. So as long as the code is passing function pointers as function pointers (and __far function pointers as __far function pointers), there should be no issue. However if the code is using data pointers in between (I guess that is what the Char* as returned by f2 is), then you run into the byte ordering issues.

 

So try to return a function pointer type from f2. Also I would recommend to use typedef's for function pointers for readability.

Especially if you have function pointer types which return function pointers Smiley Happy.

 

 

 

typedef char (fX_t*)(void);typedef fX_t (fS_t*)(int);void f1(fS_t, int p,  int j){ //body of the function.} fX_t f2(int i);

 

 

Daniel

0 Kudos

505 Views
Abhi_Ec
Contributor I

I am using cosmic compiler version 4.7.9.

 

I have declared the function as suggested by you , and also typecasted where ever required.

 

I am also suprised when pointer is showing 4 bytes.Why this is happening?

 

While looking to the memory map file which is generated after compilation, i found that some of the function(say f6) which is required in the application has not been assigned any memory.

 

These functions are in a.x12. pplication is calling f5 function which is in a.x12 and f5 is calling f6.

 

I don't have any idea why it is happening.

 

I have kept all the function to PAGE area.

 

 

0 Kudos

505 Views
CompilerGuru
NXP Employee
NXP Employee

 


Abhi_Ec wrote:

I am using cosmic compiler version 4.7.9.

 

Ah cosmic, well I don't know the details of the cosmic compiler.

 

I have declared the function as suggested by you , and also typecasted where ever required.

 

All casts are suspicious. In the provided sample case I'm not sure where a cast should be required. Be careful with casts of objects to data pointers and back. Where do you need a cast? What for?

 

I am also suprised when pointer is showing 4 bytes.Why this is happening?

 

I was not aware you are using the cosmic compiler, check the manual. It is perfectly legal for a compiler to allocate 4 bytes for a (far) pointer, the CW compiler does not do that and I just don't know how the cosmic compiler handles this.

 

While looking to the memory map file which is generated after compilation, i found that some of the function(say f6) which is required in the application has not been assigned any memory.

 

That probably means that f6 is never accessed. Check the code, unreferenced functions don't need to be linked.

 

These functions are in a.x12. pplication is calling f5 function which is in a.x12 and f5 is calling f6.

 

Not sure what you want to say here.

 

I don't have any idea why it is happening.

 

I have kept all the function to PAGE area.

 

 


Daniel

 

0 Kudos