cast on variable reference in xc

Technical questions regarding the XTC tools and programming with XMOS.
pasau
Experienced Member
Posts: 77
Joined: Fri Dec 06, 2013 7:05 pm

cast on variable reference in xc

Post by pasau »

Hi, i was wondering if it was possible somehow to do something like this in xc code or else what could be an elegant way to resolve this?

Code: Select all

void function1(int& a_int)
{
//code
}

int main()
{
   unsigned a_unsigned;
   function((int&)a_unsigned);
}


User avatar
Ross
XCore Expert
Posts: 968
Joined: Thu Dec 10, 2009 9:20 pm
Location: Bristol, UK

Post by Ross »

Pass by ref in XC you mean?

Code: Select all


void function2(int &x);

function1()
{
   int y;
   function2(y)
}

pasau
Experienced Member
Posts: 77
Joined: Fri Dec 06, 2013 7:05 pm

Post by pasau »

no, no, i really mean casting the reference for the compiler to interpret the address as a reference for a different type. Let's say I am calling a function from an API that needs a reference to int. But in my code i am working with unsigned. I could copy my unsigned variable into an int:

Code: Select all

unsigned a = 1234;
int b = (int)a;
But passing b by reference and having an unknown function do something on b wouldn't apply the changes to a because they have a different reference.

Since pointers aren't supported by the XC, language, i can't just make a copy of the pointer value with a cast like this:

Code: Select all

unsigned * a;
int * b= (int*)a;
I am thinking about doing some inline assembly to get the address of a variable and then somehow declare a new variable at the same address, but the documents on inline assembly are very thin. The object xc_ptr is similar to what i wanna do, but i have yet to see an example of two variables having the same reference, or more simply, casting the reference of a variable on function call.
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

One way of doing this is using a reinterpret cast:

Code: Select all

int main()
{
   unsigned a_unsigned;
   function((a_unsigned, int));
}
Pointer are supported in the tools from version 13 so alternatively you can write:

Code: Select all

int main()
{
   unsigned a_unsigned;
   function(*(int *)&a_unsigned);
}
pasau
Experienced Member
Posts: 77
Joined: Fri Dec 06, 2013 7:05 pm

Post by pasau »

thanks richard, thats exactly what i was looking for! I haven't tried the pointers in version 13 yet though, but the reinterpret cast works
User avatar
davelacey
Experienced Member
Posts: 104
Joined: Fri Dec 11, 2009 8:29 pm

Post by davelacey »

You need to be careful here. A reinterpret cast will reinterpret memory as a different type (not do an explicit conversion). I think the best code in your case would be:

Code: Select all

int main()
{
   unsigned a_unsigned;
   int  a_signed;
   function(a_signed);
   a_unsigned = (unsigned) a_signed;
}
This makes the conversion explicit. The compiler will optimize this down to the same thing as doing the reinterpret.

The difference is subtle but if you were doing it with float and double then doing:

Code: Select all

void f(double &x);

void main() {
   float y;
   f((y, double));
}
would probably be the wrong thing. Whereas:

Code: Select all

void f(double &x);

void main() {
   float y;
   double dy;
   f(dy);
   y = dy;
}
Will explicitly truncate the double to a float.

Dave