1

I'm currently learning C++, so sorry if I seem a little silly.

My current exercise, that I'm stuck on, requires me to write a function, IndexArray(int n) that returns a pointer to a dynamically allocated integer array with n elements, each of which is initialised to its own index. (copied from the worksheet).

I've read this several times and don't fully understand it, but they gave an example:

Assuming that intPtr is declared as

int *intPtr;

the statement

intPtr = IndexArray(10);

should produce the following memory configuration:

intPtr ->  0 1 2 3 4 5 6 7 8 9

From this example I'm guessing my function needs to create an array of size n, with values from 0 to n-1, and then another pointer needs to point to that array.

Here's their test code:

int *values1;
values1 = IndexArray(10);

I know how to easily create the array, but I don't fully understand pointers enough to know really what to do. I figured returning an array would work:

int *IndexArray(int n) {
    cout << n << endl;
    int arrayTemp[n];
    for(int i = 0; i < n; i++) {
        arrayTemp[i] = i;
    }
    return arrayTemp;
}

However when tested, values1 array doesn't contain the values from 0-9 (although arrayTemp does right before it's returned).

Any help would be amazing, and hopefully I've given everything you need to help. Thanks! :D

6
  • int arrayTemp[n]; => int* arrayTemp = new int(n); Beacuse memory allocated for int arrayTemp[n] will be released after return arrayTemp; Commented Mar 14, 2013 at 5:14
  • This is not C++. C++ does not support VLA's (variable length arrays). Tag changed to C Commented Mar 14, 2013 at 5:20
  • This is C++, and it's not variable length arrays. The length of the array is never changed. A length is sent to the function, and the size of the array is created from the length sent. It is never changed after that. Commented Mar 14, 2013 at 5:26
  • @CoreyThompson: You don't understand what a VLA is. This is a VLA: int arrayTemp[n];. In C++, array sizes must be a compile time constant. C99 introduced VLA's as you are using here. Re-tagged C again because a C++ compiler would not accept this code. Here, read up: en.wikipedia.org/wiki/Variable-length_array Commented Mar 14, 2013 at 5:54
  • @EdS. Considering that was where my problem was occuring, that's why my code wasn't working. It is a C++ problem, changing the tag again as this is definitely 100% meant for C++. Just because there is wrong code in there, doesn't mean the language should change. The piece of code you're talking about may only work in C, which explains why my code wasn't working. Commented Mar 14, 2013 at 11:04

2 Answers 2

4
int arrayTemp[n];

Notice that the statement is in a function, so when the function terminates, the arrayTemp[] will not be available any more, it coule be removed : it is a #local# variable!

So, if you want to do that , you could use :

int * arrayTemp = new int[n];

NOTICE : you must delete it any how to avoid memory leaks.

Sign up to request clarification or add additional context in comments.

Comments

2

You cannot do this:

int *IndexArray(int n) {
    cout << n << endl;
    int arrayTemp[n]; //declare a local array
    for(int i = 0; i < n; i++) {
        arrayTemp[i] = i;
    }
    return arrayTemp; //return a pointer to a local
}

You cannot return a pointer to a local. The local ceases to exist once you return. The pointer now points to garbage.

Instead, you have to use malloc (C or C++) or new (C++) to dynamically create storage for 10 ints, and since this is dynamically created on the heap it will persist after returning (and will need to be freed if malloced or delete[]d if it was a an array made with new. For just single objects made with new you just use delete )

5 Comments

I would add that you can also pass a pointer to n ints and fill it in the function
@Ed S. 'IndexArray(int n) that returns a pointer to a dynamically allocated integer array'. If it filled in an array passed to it, it is not doing dynamic allocation.
Yes... I understand that. The problem is that you said you have to use dynamic allocation, which is not true, and honestly is a bad idea to begin with. At least return a smart pointer. Another option is to take the array as an argument and return nothing.
@Ed S. It's a homework problem, which is why it needs to use dynamic allocation, regardless of what the best strategy might be :)
I'm sorry, you're 100% right. Somehow I glossed over that first sentence of the second paragraph. +1 :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.