-2

I was doing a test and the online test engine showing segmentation error, which is confusing because with no further details, and I checked the pointer no NULL and they work pretty fine, but don't how array here works. Because when debugging, everything is fine, until I try to cout/print out the array. it's reporting a is crushed here and break. I can do nothing here if it break, and I hit break or continue. if I continue, it runs just fine. so I was really confused.

My computer is windows 7, I run code in visual studio 2010 c++. Debugging is not that clear to solve the problem, and I am learning c++ not very efficient. Solve with Array need dynamic allocation.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void reverseArray(int size, int num[]) {
    if(size>1) {
        int *p = &num[size-1];
        int *f = num;
        for(int i = 0;i < size/2; i++){
            swap(*p, *f);
            p--;
            f++;
        } 
    }
}

int main() {
    int len;
    int a[len];/This is the bug, can't use uninitialized var assign array/
    cin >> len;
    for(int i = 0; i < len; i++){
        cin >> a[i];
    }
    reverseArray(len, a);
    for(int i = 0; i < len; i++){
        cout << a[i] << " ";
    }
    return 0;
}

This has something to with dynamic allocation, when I work in java, I create a new array. I have to

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

or

int[] newArray = new int[] {2,4,1,2,3};

Finally, this problem is solved, which makes me very happy. Reading and learning is very important, coding is also important. Thanks all,

And using vector instead of using array. It would be easier.

#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
int main() {
    int a;
    int len;
    vector<int> myvector;
    cin >> len;
    for(int i = 0; i < len; i++){
        cin >> a;
        myvector.push_back(a);
    }
    reverse(myvector.begin(), myvector.end());
    for(int i = 0; i < len; i++){
        cout << myvector[i] << " ";
    }
    return 0;
}

Using Array again(I doubt the following code):

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

void reverseArray(int size, int nums[]){
    if(size > 1){
        int *p = &nums[size-1];
        int *q = nums;
        for(int i = 0; i< size/2; i++){
            swap(*p, *q);
            p--;
            q++;
        }
    }
}

int main(){
    int len;
    cin >> len;
    int *a = new int[len];//a point to the first ele.

    for(int i = 0; i< len; i++){
        cin >> a[i];
    }
    reverseArray(len, a);
    for(int i = 0; i < len; i++){
        cout << a[i] << " ";
    }
    delete [] a;
    return 0;

}

It worked perfect on my laptop, which is confusing because a is pointer, but I use it like an array. It shouldn't be working......

Final Array version: http://ideone.com/ZMsD35 Done perfectly.

#include<iostream>
using namespace std;

int main(){
    int len;
    cin >> len;
    int *a = new int[len];
    for(int i = 0; i< len; i++){
        cin >> a[i];
    }
    reverse(a, a+len);
    for(int i = 0; i< len; i++){
        cout << a[i];
    }
    delete [] a;
    system("pause");
    return 0;

}
flowera
  • 799
  • 8
  • 11
  • 1
    Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/129268/discussion-on-question-by-r-mia-segmentation-fault-in-a-program-that-reverses-a). – Bhargav Rao Nov 28 '16 at 19:26

1 Answers1

0

The most likely reason for a segfault is the input. When the testing software passes len of size sufficient to overflow the automatic storage area, your program crashes on this line:

int a[len];

The exact value of len is system-dependent, but an input of 1,000,000 should do it on most common systems.

The fix is really straightforward - replace the declaration with

int a* = new int[len];

This will place the data in dynamic memory, rather than the automatic memory. It will also make your program standard-compliant, because variable-length arrays in C++ are an extension to standards.

Don't forget to delete a once you are done to avoid memory leak:

delete[] a;
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Hi, Thanks a lot! It's now working, may I know the difference between the key two lines? Do I need to delete every pointer? – flowera Nov 27 '16 at 02:34
  • @R.mia This changes the placement of your data from automatic memory (stack) to dynamic memory (heap). – Sergey Kalinichenko Nov 27 '16 at 02:36
  • May I know what is the related document or link I can refer to / – flowera Nov 27 '16 at 02:37
  • @R.mia This should be covered in your C++ textbook. Look up "dynamic allocation" and "operator new". Pay attention to the difference between operator `delete` and operator `delete[]` with square brackets. – Sergey Kalinichenko Nov 27 '16 at 02:39
  • but we need to delete every dynamic allocation pointer? and new int[ ] is pretty much the same as java, could you tell me some relationship between java and c++ , which one would be more easier and less trouble. Since c++ is always giving me trouble..... – flowera Nov 27 '16 at 02:46
  • 1
    @R.mia Yes, you need to delete everything that you allocate with `new`. Java's syntax borrowed a few things from C++. Other than that, the two languages are very different. – Sergey Kalinichenko Nov 27 '16 at 02:49
  • I looked up the new operator, when I assign *p point to the first type of the array, and then operate p[i] like p is an array. and it works......I copied the code above. – flowera Nov 27 '16 at 07:42