Pointers and references

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
User avatar
lilltroll
XCore Expert
Posts: 956
Joined: Fri Dec 11, 2009 3:53 am
Location: Sweden, Eskilstuna

Pointers and references

Post by lilltroll »

So I have updated my memory on pointers in C.

What is the "correct" way to transfer somthing larger than a variable - say an array from

main.xc -> subroutine.c -> main.xc without doing a memory copy ?
Suppose that I would like to calculate the 1000*sin(A), thus I need to use the math.h



main.xc
main(){

int A[4]={1,2,3,4};
??=calcsin(A)

}

calcsin.c
? calcsin(int A[]){

for(k=...
...round(1000*sin(A[k]));

return ?;
}


Probably not the most confused programmer anymore on the XCORE forum.
User avatar
Andy
Respected Member
Posts: 279
Joined: Fri Dec 11, 2009 1:34 pm

Post by Andy »

Do you need to preserve A? If not you can save some space by writing the value back into the A array.

main.xc:

Code: Select all

int main(void)
{
	int A[4] = {1,2,3,4};
	
	calculate(A);
...
caculate.c:

Code: Select all

void calculate(int array[])
{
	int i; 
	
	for (i=0; i < 4; i++) // No bounds checking!! 4 is a magic number
	{
		array[i] = array[i] * 2; // Or whatever operation on the value
	}
}
This is essentially the same as passing a pointer to the A array (but XC doesn't support pointers anyway, so passing arrays is equivalent).
Last edited by Andy on Fri Feb 12, 2010 9:04 pm, edited 1 time in total.
User avatar
Andy
Respected Member
Posts: 279
Joined: Fri Dec 11, 2009 1:34 pm

Post by Andy »

I should point out that the calculate.c function could be written exactly the same in XC. It's also not good practice to operate on an array without doing some sort of bounds check (the size of the array could change). You'd need to pass the size as a parameter to the calculate() function using something like sizeof(A)/sizeof(A[0]) to get the number of elements.
User avatar
lilltroll
XCore Expert
Posts: 956
Joined: Fri Dec 11, 2009 3:53 am
Location: Sweden, Eskilstuna

Post by lilltroll »

Haha I tried with several combinations of & * int* , but not the one without any. Started to try to cast pointers to variables.

Can the arraysize be dynamically changed without using malloc() ?
Probably not the most confused programmer anymore on the XCORE forum.
User avatar
paul
XCore Addict
Posts: 169
Joined: Fri Jan 08, 2010 12:13 am
Contact:

Post by paul »

Not in XC. XC is currently designed to be a 'safe' language - hence fixed sized arrays and it will inserts bounds checking by default unless explicitly turn it off (#pragma unsafe arrays) - hence also no pointers to help avoid shared memory etc.

To pass by reference to/from C (i.e. give it a pointer) you can use the &var form. Example below:

C code:

Code: Select all


void swap( int *a, int *b )
{
int temp;

temp = *a;
*a = *b;
*b = temp;
}
C header file (swap.h):

Code: Select all

#ifdef __XC__
void swap( int &a, int &b );
#else
void swap( int *a, int *b );
#endif
XC file:

Code: Select all

#include "swap.h"

// ...

void func()
{
int a, b;

a = 1; b = 2;
swap( a, b);
}
Hope that helps, cheers,
Paul

On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
User avatar
N8B
Junior Member
Posts: 7
Joined: Wed Apr 21, 2010 4:58 pm

Post by N8B »

Andy wrote: main.xc:

Code: Select all

int main(void)
{
	int A[4] = {1,2,3,4};
	
	calculate(A);
...
caculate.c:

Code: Select all

void calculate(int array[])
{
	int i; 
	
	for (i=0; i < 4; i++) // No bounds checking!! 4 is a magic number
	{
		array[i] = array[i] * 2; // Or whatever operation on the value
	}
}
This is essentially the same as passing a pointer to the A array (but XC doesn't support pointers anyway, so passing arrays is equivalent).
So, in this example code, the entire contents of array A are not getting pushed onto the stack, correct? In other words, when the calculate(A) function call is made in main.xc it is not pushing four longs onto the stack, it is pushing a pointer, right?
User avatar
Andy
Respected Member
Posts: 279
Joined: Fri Dec 11, 2009 1:34 pm

Post by Andy »

N8B wrote:So, in this example code, the entire contents of array A are not getting pushed onto the stack, correct? In other words, when the calculate(A) function call is made in main.xc it is not pushing four longs onto the stack, it is pushing a pointer, right?
That's right. The assembly of the calculate() loop looks something like:

ldw r1, r0[r11]
mul r1, r1, r3
stw r1, r0[r11]
add r11, r11, 0x1
Post Reply