Pointers and scope help

08 Oct 2011

Hi mbeders,

I am working on an application where I need to control 12 servos. At the minute I'm calculating their positions in a function and need a way to return all 12 positions. At the minute I've created a structure that contains all the positions and am returning the structure from the function.

This seems a little clunky and I thought it would be a bit more elegant to create an array containing the values and return a pointer to the start of the array. Like this...

#include "mbed.h"

Serial pc(USBTX,USBRX);

int* pointer_print(void) {
    int bar[6] = {1,2,3,4,5,6};     // local array of integers
    return bar;
}

int main() {
    int* foo = pointer_print();    // get the pointer

    for (int i=0; i<6; i++) {
        pc.printf("%d ",foo[i]);
    }
}

My question is because the array 'bar' is local to the pointer_print function it will be destroyed when the pointer is returned (but the memory location content will not be overwritten or lost). I will be doing this a lot and am worried that if the mbed then uses that memory location for something else my servo position data will become corrupt? Can I minimise this risk by using the data right away? Is there a better way of doing it?

Is this right or am I worrying about nothing?

08 Oct 2011

Why dont you make the array global and access it in the function and in main. Alternatively you can pass the arraypointer to the function if you have more than one array that the function should operate on.

#include "mbed.h"

Serial pc(USBTX,USBRX);

int bar[6] = {1,2,3,4,5,6};     // global array of integers

void pointer_print(void) {
    for (int i=0; i<6; i++) {
        bar[i] = 100 + i; //Set the array values
    }

}

int main() {
    pointer_print();    // set the values

    for (int i=0; i<6; i++) {
        pc.printf("%d ",bar[i]);
    }
}

08 Oct 2011

Thanks Win, I had thought about a global array but try to avoid using to much global stuff unless I have to. maybe I'll go down that route in this instance?

08 Oct 2011

Hi Martin,

As Wim mentions, the most natural way may be to pass a pointer to your array:

#include "mbed.h"

Serial pc(USBTX,USBRX);

void pointer_print(int *bar) {
    for (int i=0; i<6; i++) {
        bar[i] = 100 + i; //Set the array values
    }

}

int main() {
    int bar[6] = {1,2,3,4,5,6};     // local array of integers
    pointer_print(bar);    // set the values

    for (int i=0; i<6; i++) {
        pc.printf("%d ",bar[i]);
    }
}

Lots of possible solutions, but the important thing in all this was you correctly identified the problem!

Simon

09 Oct 2011

Thanks Simon, I tried it as you suggested and it worked OK

04 Nov 2011

One more question. How about passing pointers to 2D arrays???

I have some code here that works..

#include "mbed.h"

Serial pc(USBTX,USBRX);

void foo(int bar[][3]) {
    for (int row=0; row<3; row++) {
        for (int col=0; col<3; col++) {
            bar[row][col] = (row+1)*(col+1);           //Set the array values
        }
    }
}

int main() {
    int bar[3][3];          // local 2D array of integers
    foo(bar);     // set the values

    pc.printf("\n\rPointer test \n\n\r");

    for (int row=0; row<3; row++) {
        for (int col=0; col<3; col++) {
            pc.printf(" %d ",bar[row][col]);
        }
        pc.printf("\n\r");
    }
}

and prints something like...

Information

Pointer Test

1 2 3

2 4 6

3 6 9

Which is fine but I want to scale it up to an 160 x 120 array. I'm not sure what I'm actually passing between foo and bar? Is it a pointer to bar[0][0] and a figure telling foo how many columns bar has (which is what I want - good) or is it some huge amount of memory and data (bad!)

Thanks in advance Martin