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 ?;
}
Pointers and references
-
- XCore Expert
- Posts: 956
- Joined: Fri Dec 11, 2009 3:53 am
- Location: Sweden, Eskilstuna
Pointers and references
Probably not the most confused programmer anymore on the XCORE forum.
-
- Respected Member
- Posts: 279
- Joined: Fri Dec 11, 2009 1:34 pm
Do you need to preserve A? If not you can save some space by writing the value back into the A array.
main.xc:
caculate.c:
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).
main.xc:
Code: Select all
int main(void)
{
int A[4] = {1,2,3,4};
calculate(A);
...
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
}
}
Last edited by Andy on Fri Feb 12, 2010 9:04 pm, edited 1 time in total.
-
- Respected Member
- Posts: 279
- Joined: Fri Dec 11, 2009 1:34 pm
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.
-
- XCore Expert
- Posts: 956
- Joined: Fri Dec 11, 2009 3:53 am
- Location: Sweden, Eskilstuna
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() ?
Can the arraysize be dynamically changed without using malloc() ?
Probably not the most confused programmer anymore on the XCORE forum.
-
- XCore Addict
- Posts: 169
- Joined: Fri Jan 08, 2010 12:13 am
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:
C header file (swap.h):
XC file:
Hope that helps, cheers,
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;
}
Code: Select all
#ifdef __XC__
void swap( int &a, int &b );
#else
void swap( int *a, int *b );
#endif
Code: Select all
#include "swap.h"
// ...
void func()
{
int a, b;
a = 1; b = 2;
swap( a, b);
}
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.
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.
-
- Junior Member
- Posts: 7
- Joined: Wed Apr 21, 2010 4:58 pm
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?Andy wrote: main.xc:caculate.c:Code: Select all
int main(void) { int A[4] = {1,2,3,4}; calculate(A); ...
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).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 } }
-
- Respected Member
- Posts: 279
- Joined: Fri Dec 11, 2009 1:34 pm
That's right. The assembly of the calculate() loop looks something like: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?
ldw r1, r0[r11]
mul r1, r1, r3
stw r1, r0[r11]
add r11, r11, 0x1