Codewarrior Type Casting issue when passing "void *" from a function pointer

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

Codewarrior Type Casting issue when passing "void *" from a function pointer

871 Views
Charlie
Contributor III

So I have this function:

 

static void T_CALLBACK _transmit(

  void *pContext,

  TX_DATA *pTxDescriptor)

{

  CONTEXT *pLinkContext = (CONTEXT *)pContext;

}

 

That is being called via a function pointer.

 

void *pContext is being passed an address of 0x1558.

 

However, when it gets cast, it casts to 0x0AAC.  For obvious reasons, this causes problems because it is pointing to the wrong address in memory and I quickly get a long access misalign interrupt.

 

I don't fully understand what is going on or how to fix it, this is third party code and not something I do in coding, so while I see WHAT is going wrong (the address is shifted by 1, so it points to the wrong spot in memory), but I'm not sure WHY it is doing it and, more importantly, how I can fix it.

 

Labels (1)
0 Kudos
3 Replies

319 Views
stanish
NXP Employee
NXP Employee

Hello Charles,

I assume you are targeting DSCs, am I right?

The DSC compiler treats the void pointers as they are pointers to bytes by default.

This causes shifting the address to left and right back and forth according to type casting. (See more info about this feature in DSC CodeWarrior compiler manual,

section "4.6 Code and Data Storage" and "11.5.10.3.1 The Effects of Casting on Code Quality" );

http://cache.freescale.com/files/soft_dev_tools/doc/ref_manual/DSC_Compiler.pdf?fpsp=1&WT_TYPE=Refer...

I'm not sure what is CONTEXT type is on your side, but assume it's a structure or a non-char data type.

In such a case the compiler cast (shift right) the byte address (viod *) before it's assigned  *pLinkContext. This is correct behavior.

The problem probably occurs somewhere else where you are accessing *pLinkContext.


Can you possibly post  body of _transmit() function including definitions of user data types involved (CONTEXT, TX_DATA ...)

This will help to find a root cause.


regards,

Stan



0 Kudos

319 Views
Charlie
Contributor III

I'll review the documentation, but I am posting this while I do so:

You are correct, the CONTEXT is a struct.

I wasn't entirely clear.  It doesn't fail ON the cast, but when I call a function passing that pointer.

here is the rest of that function:

...

CONTEXT *pLinkContext = (CONTEXT *)pContext;

  /* Initialize context */

  pLinkContext->retryCount = 0;

  pLinkContext->pTxDescriptor = pTxDescriptor;

  /* Call function to send variable frame */

  _sendVariableFrame(pLinkContext);

}

It goes into the misalign when it goes into _sendVariableFrame.

I cheated a bit to get it to work by changing the function declaration to only accept CONTEXT * instead of void * and this solved the problem and I have not seen a problem with this yet, but the change does scare me a little bit.

0 Kudos

319 Views
jorge_plascencia
NXP TechSupport
NXP TechSupport

Hi Charles,

Regarding to your issue, could you please send your project to me?

So I can fully know what's the problem and what's your target, then, it could be effective to investigate!

Regards,

0 Kudos