CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problem with Pointer to const function

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Michu



Joined: 05 Dec 2011
Posts: 3

View user's profile Send private message

Problem with Pointer to const function
PostPosted: Mon Dec 05, 2011 5:10 am     Reply with quote

Hi everybody

I have a look-up table with a string and a pointer-to-function. During runtime i want to call these functions. As long as the LUT is in RAM, everything works. As soon as i uncomment the "const", i can't compile it anymore. I obviously identified the problem but am not able to solve it which is kind of frustrating... Sad

This is the corresponding code:
Code:

#include <16F690.h>

#use delay(internal=8MHz)
#use rs232(UART1, baud=9600, BITS =8,STOP=1,PARITY=N)      

//Typedefs
typedef void (*_fptr)(char i[30]);

typedef struct {
   _fptr functionPntr;
   char command[30];
} transitionT;

//Prototypes
void handleText1(char i[30]);
void handleText2(char i[30]);

//TransitionTable
/*const*/ transitionT   transitionTable[] = {   
                        {&handleText1   ,"TEXT 1"},
                        {&handleText2   ,"TEXT 2"}};

//Functions
void handleText1(char i[30]){
   printf("%s\n",i);
}

void handleText2(char i[30]){
   printf("%s\n",i);
}

void main(void){
   
   unsigned int i;
   int size;

   while(1){
      size = sizeof(transitionTable)/sizeof(transitionT);

      for(i = 0 ; i < size ; i++){
         (*transitionTable[i].functionPntr)(transitionTable[i].command);
      }

      delay_ms(1000);   
   }
}


I am using Compiler Version 4.122

Are there some geniuses out there who can help me?
Thanks
Michu



Joined: 05 Dec 2011
Posts: 3

View user's profile Send private message

PostPosted: Mon Dec 05, 2011 5:57 am     Reply with quote

I forgot to post the error-message i get when i compile it:

Quote:
>>> Warning 203 "pointerToConst.c" Line 38(1,1): Condition always TRUE
>>> Warning 207 "pointerToConst.c" Line 41(1,1): Code has no effect
*** Error 76 "pointerToConst.c" Line 41(35,36): Expect ;
1 Errors, 2 Warnings.


Thanks
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Mon Dec 05, 2011 5:57 am     Reply with quote

There's a number of possibilities. All come from the fact that PIC have a Harvard architecture which has different busses and address spaces for data (RAM) and instructions. C assumes a more conventional Von Neumann architecture with a single address space for both (though some later PDP-11s used memory mapping to provide seperate I and D spaces which Unix, and maybe other OSs, exploited to give larger virtual task space).

The key here is that const requests that your table is placed in a different address space, in fact a totally different memory area, i.e. program flash. On the 16Fs that space is not even byte wide, its fourteen bit. Because of the difficulties in sorting all this out, it didn't support function pointers at all until fairly recently. It does, but there are limitations. I don't use funtion pointers very often, the sort of command parser thing you're doing is the most likely task which I#d use it for, and as I think you know, its useful to push such tables out to program memory as the strings take up a lot of space.

For the reasons above arrays in program memory are more complex for the compiler to sort out. They are slower and there are restrictions in how they are used, particularly with strings, where unrtil very recently it wasn't possible to form pointers to characters in strings except in certain special cases where CCS had provided routines specifically that dealt with them. The Microchip and Hitec compilers deal with all this differently and Microchip have a load of different versions of the C standard string & memory manipulation functions.

So, you have function pointers, which are tricky for any PIC compiler, and tables in program memory, i.e. const, which are also tricky. Combine the two, tables of function pointers (AND strings!) in program memory and perhaps it should not be surprising that something breaks :-( The key problem is that creating a pointer to a const is not allowed, except in the special string cases.

I've experimented a bit and it seems as though you can't put function pointer tables into program memory :-( I've tried taking out the strings but that doesn't help. Looks like you're stuck with RAM based tables. For that I suspect you'll be needing a bigger PIC.

RF Developer
Ttelmah



Joined: 11 Mar 2010
Posts: 19346

View user's profile Send private message

PostPosted: Mon Dec 05, 2011 8:19 am     Reply with quote

RF_Developer has already explained why this is difficult in the PIC. However the compiler can do it.
If you declare a variable as 'rom', instead of 'const', the compiler adds the extra code to allow pointers to it. In your compiler directory, you should have a 'readme.txt', explaining this.

However there is a second problem. You cannot have pointers inside a variable declared at compile time, so definately not in a const, or rom declared variable...

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group