Pointer to function

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Pointer to function

501件の閲覧回数
roberto_m
Contributor III

Hi to all,

I'm using pointer to function variable. In this way I can call different functions. But i see a strange behaviour in this code:

 

#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */


typedef void (*task_t)(void);

 void hello (void);
 
task_t task = NULL;

void main(void) {

  EnableInterrupts;

  task = &hello;

  for(;:smileywink:
  {

    task();
    __RESET_WATCHDOG();
  }
}


void hello ()
{
}

 

In debug mode, Procedure window I see :

 

main()

hello()

<0202'P>

 

But hello() itsn't called anytime before!

In my opinion it is a mistake!

 

I'm using CW6.3. (i try also in CW 6.2: same result) 

ラベル(1)
0 件の賞賛
返信
2 返答(返信)

320件の閲覧回数
CompilerGuru
NXP Employee
NXP Employee

The content of the procedure chain window below main is random  (well the result of unrelated memory content).

The provided startup code does not put any starting marker on the stack as this would cost some RAM, so the debugger has no way to determine the history of who called main, or actually that there is no information about that and is instead interpreting the RAM above the stack as if it would belong to it. Apperently in your setup, it contains the address of hello, and then 0202.

 

Basically ignore anything in the call chain below main.

 

Daniel

0 件の賞賛
返信

320件の閲覧回数
Lundin
Senior Contributor IV

Anytime before what? I don't understand the problem here.

 

Apparently, it is called from the JSR instruction at address 2091. As far as the C language is concerned, the compiler is free to optimize away that whole function call, as it does nothing. However, Codewarrior doesn't do this optimization, as we can see from your disassembly. It loads the address of hello, 2097, into X, then calls it. On address 2097 there is just a RTS, so nothing fancy will happen.

 

 

0 件の賞賛
返信