C ++ Pointer Pointer Methods

I have two questions:

1) How can I create an array that points to objects of integers?

int* myName[5];  // is this correct?

      

2) If I want to return a pointer to an array that points to objects (like (1)), how can I do this in a method? i.e.) I want to implement a method:

int **getStuff() {
// what goes here?
return *(myName); // im pretty sure this is not correct
}

      

Thanks for the help!

0


source to share


6 answers


How can I create an array that points to objects?

int * myName[5]; /* correct */

      

If I want to return a pointer to an array that points to objects (like (1)) How can I do this in a method?

Technically, you write this function:



int * (* getStuff() )[5] {
    return &myName;
}

      

Returns a pointer to this array. However, you don't want to do this. You would like to return a pointer to the first element of the array:

int ** getStuff() {
    return myName; /* or return &myName[0]; */
}

      

This way, you can now access the elements however you want, getStuff()[0] = &someInteger;

+5


source


Please note that your code

int* myName[5];

      

declares an array containing 5 values, each of which is a "pointer to int" and this is what you asked for.

However, this is C ++, which is all it does. As a Python script, this can cause some surprises.

It doesn't give any of these 5 pointers to sane values, and it doesn't create any of the integers they point to.

If you put it in the body of a function, it creates an array on the stack. This means that the array will cease to exist when the current scope ends (which, to put it simply, means you end up in a closed curly, like return). So in particular the following code is bad:

int **myFunction() {
    int *myArray[5];
    return myArray;
} // <-- end of scope, and return takes us out of it

      

It can compile, but the function returns a pointer to something that no longer exists by the time the caller sees it. This leads to what we call "undefined behavior".

If you want the array to exist outside of the function in which it was created, you can create it on the heap every time you call your function and return a pointer, for example:

int **myFunction() {
    int **myArray = new int[5];
    return myArray;
}

      



The function returns a different array each time it is called. When the caller is done with it, he should destroy the array, e.g .:

delete[] myArray;

      

otherwise it will never be freed and will sit around using memory forever (or when your program exits on most OSs).

Alternatively, you can use the "static" keyword to create an array with a "global storage duration" (this means it exists as long as the program is running, but only one of them each time, not a new one). This means that the function returns the same array every time it is called. The caller can store some pointers in it, forget about it, call the function again and see the same pointers:

int **myFunction() {
    static int *myArray[5];
    return myArray;
}

      

Note how this code works with very bad code earlier.

Finally, if you just want to create an array of integers rather than an array of pointers to integers, you can do this:

int myArray[5] = { 1, 2, 3, 4, 5};

      

This actually creates 5 integers (which means it assigns a space that can store the integer values ​​themselves. This is different from an array of pointers, which stores the addresses of the space used to store the integer values).

It also stores the specified values ​​in this space: myArray [0] is now 1, myArray [1] is 2, and so on.

+2


source


1) Correct - this is an array of 5 pointers to int

s

2) You can return a pointer to an array of pointers to int

by returning a pointer to the first element of that array. This has two levels of indirection, so you need two asterisks. You can also return an array, as arrays automatically decay to pointers to their first elements.

int **getStuff() {
    return myName;       // 1
    return &myName[0];   // 2
}

      

+1


source


int **myName;

int **getStuff() {
  int **array = new int*[5];

  for (int i = 0; i < 5; i++)
  {
    int key = i;
    array[i] = &key;
  }

  return array;
}

      

+1


source


Steve Jessop, I think you meant:

int **myFunction() {
    int **myArray = new int*[5];
    return myArray;
}

      

0


source


Returns a pointer to a heap array (not a pointer to its elements), checked and removed. Leak nothing.

template <class T>
T* newarray(int len)
    {
        T *a;
        try
           {
               a = new T[len];
               memset(a,0,len*sizeof(T));
               return a;
           }
        catch (...)         
           {return 0;}
    }   

      

... ,.

void foo()
    {
        float *f=0;
        f=newarray<float>(1000000);
        if(!f) return;
        //use f
        delete [] f;
    }

      

0


source







All Articles