0

Image of my question: https://i.sstatic.net/x5VQW.png

To summarise, in one function, readNumbers, I will make an array of size 10, and return a pointer to this array. I then have a second function, printNumbers, that takes the pointer from the above function (and the length of the array, 10) and prints the index and corresponding value in the array.

My function file:

#include <iostream>
using namespace std;

int *readNumbers() 
{
    int arr[10];
    for (int i=0; i<10; i++) //asking for 10 values
    {
        cout<<"Enter an element: ";
        cin>>arr[i];
    }

    int *arrPtr;
    arrPtr=&arr[0];

    return arrPtr;

}




 void printNumbers(int *numbers,int length) 
{


    for (int i=0; i<length; i++)
    {
        cout<<i<<" "<<*(numbers+1)<<endl;
    }
    delete numbers;

}

Main file:

int *readNumbers() ;
void printNumbers(int *numbers,int length) ;

int main (void)
{



    printNumbers(readNumbers(), 10) ;


}

The code compiles, however, the elements of the array printed are not what has been fed into the compiler.

Also, the question days to delete the array. Is this related to clearing the heap space? Is this achieved by deleting the pointer to the array, as I have done in the second function?

Thank you.

4
  • 2
    Just a note: input is not fed into the compiler. The compilation process happens before the program is ever run. Commented Aug 22, 2018 at 12:12
  • How to return local array in C++? Commented Aug 22, 2018 at 12:13
  • 1
    Use std::array<int, 10> or std::vector<int>. Commented Aug 22, 2018 at 12:16
  • The words "dynamically allocated" in the description are very important. You must have either missed that detail or didn't realise their significance. Commented Aug 22, 2018 at 12:32

2 Answers 2

2

In your function, you allocate a array of size 10 statically, hence it only exists until the function exits. After that, you get undefined behaviour since you're reading random bytes from memory that no longer belongs to you.

What you want is to allocate the array dynamically, this can easily be done using new

int* arr = new int[10];

Then you simply return arr;

Of course, don't forget to clear the memory once you're done using it, by calling delete[] (and not delete without the braces).

As a side note, calling return arrPtr is the same as calling return arr which will naturally decay to a pointer. It's only wrong because that pointer ends up pointing to something that is no longer there.

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

1 Comment

The [] after delete is very important!
1

I've modified your code slightly so that it does what was demanded in your assignment https://i.sstatic.net/x5VQW.png

My code:

#include <cstdlib>
#include <iostream>
using namespace std;

int *readNumbers() {
   int *arr = new int[10];
   for (int i = 0; i < 10; i++) //asking for 10 values
   {
      cout << "Enter an element: ";
      cin >> arr[i];
   }
   return arr;
}

void printNumbers(int *numbers, int length) {

   for (int i = 0; i < length; i++) {
      cout << i << " " << numbers[i] << endl;
   }
   delete[]numbers;
}

int main() {
   printNumbers(readNumbers(), 10);
   return 0;
}

Answer to your first question: Local arrays, such as this int array[10];, are allocated on the stack, and get destroyed when the function they're in ends. That is why you are reading random bytes from memory when you are trying to print out the content of your array later.

When we write int *arr = new int[10]; we allocate our array on a heap. The address of that array is returned and assigned to the pointer called arr. The pointer arr is allocated on the stack in turn. However, when you are allocating something (e.g. an array) on the heap using the new operator you should use delete operator when you no longer need it to deallocate your array. Otherwise, you will run out of space in your heap and your program will crash if you keep on using new without using delete later.

Answer to your second question: When you are using delete operator (or delete[] in case of arrays) you are actually deleting what arr pointer was pointing to on the heap. The pointer arr won't be deleted itself from the stack after using the delete operator. It becomes so-called 'dangling pointer' - a pointer that no longer points to something valid on the heap. It will be deleted from the stack when we terminate from the function (scope) it was declared in, along with the other local variables we may have. However, after you delete something that pointer was pointing to, you can assign a NULL to it (arr = NULL;), to indicate that it is not referencing anything anymore.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.