Moving an array using pointers

#include <iostream> using namespace std; int* flipArray(int input[], int n) { int output[n]; int pos = 0; for (int i = n-1; i >= 0; i--) { output[pos++] = input[i]; } int* p = output; for (int k = 0; k < n; k++) cout << *pk << endl << endl; return p; } int main() { const int SIZE = 5; int firstArray[SIZE]; for (int n = 0; n < SIZE; n++) { firstArray[n] = n+1; } int* a; a = flipArray(firstArray, SIZE); for (int j = 0; j < SIZE; j++) cout << *aj << endl; cout << endl; cout << *a << '\t' << *a+1 << '\t' << *a+2; return 0; } 

I'm trying to flip firstArray with a function that returns a pointer, but I'm trying to understand how index access works with a pointer.

This is why I am confused: Inside the flipArray function, the following for loop:

 for (int k = 0; k < n; k++) cout << *pk << ' '; 

displays "5 4 3 2 1" on the console. I realized that I had to access the element of the vector with *(p+k) , not *(pk) . If I type *(p+k) , "5 6 7 8 9" is printed on the console. If I print an array without pointers and using k as the index location, "5 4 3 2 1" is printed to the console.

In my main function, however, the * a values ​​that are assigned the p pointer from the flipArray function do not get the same results:

 for (int j = 0; j < SIZE; j++) cout << *aj << endl; 

prints 5 0 -1 -2 -3 to the console and

  for (int j = 0; j < SIZE; j++) cout << *a+j << endl; 

prints 5 2 3 4 5 to the console.

In addition, I thought that the location of the *p pointer and *a location pointer should be the same! But when I print the address &p in the function, I get the location 0x28fde0, and when I print the address &a basically, I get the location 0x28fedc. Of course, this was done during the same run.

Can someone tell me where I went astray Thanks!


Thanks to everyone for the informative answers.

I updated my solution and now it returns what I expect. I have a new question about memory leak, and when to remove pointers.

 int* flipArray(int input[], int n) { int* output = new int[n]; int pos = 0; for (int i = n-1; i >= 0; i--) output[pos++] = input[i]; return output; } int main() { const int SIZE = 5; int firstArray[SIZE]; for (int n = 0; n < SIZE; n++) { firstArray[n] = n+1; } int* a; a = flipArray(firstArray, SIZE); for (int j = 0; j < SIZE; j++) cout << a[j] << " "; // can also be written as *(a+j), which is more prone to bugs delete [] a; return 0; } 

Will pointer output be deleted when the flipArray function returns? If not, how do I remove the output and also return it? Does the pointer a in my main function delete the same as deleting the output, because they point to the same location?

+6
source share
3 answers

It was pointed out that your main problem comes from the operator's priority . The * in *p - k operator evaluates to - . This means that k will be subtracted from the int value pointed to by p .

This is a huge pain, so pointer[k] braces are often used. There are situations when using pointer arithmetic *(pointer + k) makes more sense, but can be a source of errors.

Note that it is always better to use parentheses, even if you are not sure if you need them or not.


You have a second problem:

Here you declare output on the stack as a local variable, and then return output . When you return to the previous stack stack, this pointer will point to a decentralized buffer:

 int* flipArray(int input[], int n) { int output[n]; // allocated on the stack int pos = 0; for (int i = n-1; i >= 0; i--) { output[pos++] = input[i]; } int* p = output; for (int k = 0; k < n; k++) cout << *pk << endl << endl; return p; // this stack frame ends. } 

This means that the contents of the buffer can be overwritten if the space in which the buffer is used is redistributed. Use new to highlight on the heap:

 int* output = new int[n]; 

Be sure to call delete on the pointer when you are done using it.

This error may even present security vulnerabilities in your applications, so make sure you know when to allocate a bunch in C ++ .


Update:

Question: When this function returns, the array still exists in memory, and its location is stored in pointer a. Does the return value delete the value? If not, will the pointer a be deleted when I finished with it in the main function, accomplish the same goal?

When you delete pointer, the memory indicated on that pointer is freed and the pointer remains hanging. The link to the remote pointer points to technically free memory, which is bad. If the allocator library decides that it wants to reuse this space, your buffer, which is now in free space, will be redistributed. This means that your buffer will lose all data integrity, and the data inside it cannot be trusted.

It is common practice to assign pointers to NULL when you finish using them. Thus, your program will fail, and you will find out where your error is located:

 int* p = new int[10]; ... delete p; p = NULL; ... p[0] = 0; // this will now crash because you are accessing NULL. 
+4
source
 for (int k = 0; k < n; k++) cout << *pk ; 

I understood that I had to access the element of the vector with * (p + k), not * (pk).

Your understanding is correct. You are not accessing the array here. p points to the first element of 5 and every time k is deleted from it, which gives you 5-0 5-1 5-2 , and therefore on which is equivalent to filpped array.So if you want to access the array using a pointer

 for (int k = 0; k < n; k++) cout << *(p+k) ;// Note the paranthesis 
+3
source
 for (int k = 0; k < n; k++) cout << *pk << endl << endl; 

What this code does is completely different from what you think.

*p - k will be processed as

 *p = 5 - 0 = 5 *p = 5 - 1 = 4 

etc. not *(p+k) or *(pk)

For your understanding:

 int a[5] = { 1,2,6,4,5}; 

To access the 3rd element of the array, do

 a[2] = *(a+2) 

but not

 *a + 2 

*(a + 2) = 6 and *a + 2 = 1 + 2 = 3

Take care not to return a pointer to a local variable, which will lead to undefined behavior

+2
source

Source: https://habr.com/ru/post/985540/


All Articles