Skip to content
Sign UpLog In
Profile icon

Anubhav Pratap

@AnubhavPratap1
Repls
Community
AnubhavPratap1
AnubhavPratap1
published an Update
1 year ago
2
selection_sort
selection_sort
Help - *** stack smashing detected ***: terminated signal: aborted (core dumped)
I am writing a simple code to input data and store it as array and then printing it out. But i am getting error: * stack smashing detected *: terminated signal: aborted (core dumped) Please help me regarding this
Highwayman
Highwayman
@AnubhavPratap1 the array `arr` should have a constant size that can be deduced at compile time. Even if `n` was properly initialized (which it isn't in the first place), it would still not be considered to be known at compile time. that's probably what's making your compiler insane. I suggest using a [`std::vector<int>`](https://www.cplusplus.com/reference/vector/vector/) instead. for example: ```c++ #include <iostream> #include <vector> // std::vector int main() { int n; std::cin >> n; std::vector<int> arr( 0, // value that all the elements will be initialized to. n // number of elements you want. ); arr[n - 1] = 10; std::cout << arr[n - 1]; } ```1 year ago
AnubhavPratap1
AnubhavPratap1
@Highwayman Thanks for the reply. I asked my teacher about and he suggested me to initialize array "arr[n]" after you have inputted the value of n. Like this #include <iostream> using namespace std; int main() { int n; cout<<"n: "; cin>>n; // initialise arr after we have inputed the value of n. int arr[n]; cout<<"Enter elements of array:-"<<endl; for(int i=0; i<n; i++) { cin>>arr[i]; } for(int j=0; j<n; j++) { cout<<arr[j]<<" "; } } 1 year ago
Highwayman
Highwayman
@AnubhavPratap1 hmmm. are you sure they told yo to use a normal array and not even dynamic memory/new or something like that...? The only thing I can think of is VLAs, but that's a C thing...1 year ago