0

I am writing a simple program of linear search on vscode on macOS. The code is producing an error called segmentation fault only in vscode. But the strange thing is that code is working perfectly fine on onlinegdb compiler and Xcode IDE. I have the default c++ compiler installed on my Mac which came after installing Xcode.

#include<iostream>
using namespace std;

int linearSearch(int arr[], int n, int key){

    int i = 0;
    for(i = 0; i<n;i++)
    {
        if(arr[i] == key){
            return i;
        }
        
    } return -1;
    


}

int main(){

    int n = 0;
    int arr[n];
    int key = 0;

    cout<<"Enter the length of the array"<<endl;
    cin>>n;

    cout<<"Enter the elements of the array"<<endl;
    
    int i = 0;
    for(i = 0; i<n;i++)
    {
        cin>>arr[i];
        
    }

    cout<<"Enter the element to search in array"<<endl;
    cin>>key;

    cout<<linearSearch(arr, n, key);
    
    
}[screenshot of the error in vscode][1]

[1]: https://i.stack.imgur.com/Bo3Nu.png

Akshansh Sharma
  • 23
  • 1
  • 1
  • 4
  • I am surprised `int arr[n];` even compiles. In any case, `n` is 0 when you declare the array. Where do you expect the array's storage to be, and where does the storage get allocated? – Tumbleweed53 Dec 20 '20 at 21:23
  • 2
    Aside from the fact that variable length arrays aren't part of C++, what do you think the size of `arr[n]` will be with `n=0`? – Adrian Mole Dec 20 '20 at 21:24
  • 3
    If you have a C++ compiler and STL Containers, Prefer `std::vector`, `std::list`, ... over raw C-Style array. – Ghasem Ramezani Dec 20 '20 at 21:26

3 Answers3

2

Segmentation fault isn't a vscode error, its a program error, it indicates that your program is accessing a memory adress that it hasn't reserved, thus the OS kills your program to save the system from wrong or bad memory accesses.

You first initialize n with 0 and then initialize the array arr with n ints. So it makes you an array with 0 ints. If you want to make this work, push the int arr[n] below the cin >> n. You will have to convert it first from a string to a int using stoi()

libraries:

#include <string>
#include <iostream>

code:

//Create the int to store the length of the array
int n = 0;
//A string, beacause cin returns a string
std::string s;

//Get the number
std::cout << "Length of array: ";
std::cin >> s;

//Convert the string to an int
n = stoi(s);

//Create the array
int arr[n];
Max Kofler
  • 86
  • 4
  • What is the need of stoi() ? – Neeraj-Kumar-Coder Oct 14 '22 at 18:06
  • 1
    @Neeraj-Kumar-Coder stoi() is needed to convert the std::string received from std::cin to an integer, which is needed for the array creation. You can't create an array with the length supplied as a string, it has to be a number – Max Kofler Oct 18 '22 at 16:25
0

Here you go,

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

int n = 0;
string s;

//Get the number
cout << "Length of array: ";
cin >> s;
n = stoi(s);
int arr[n];
  • Not that variable length arrays (VLAs) are not part of **Standard** C++. [Variable Length Array (VLA) in C++ compilers](https://stackoverflow.com/q/39334435/10871073) – Adrian Mole Apr 18 '23 at 10:33
-1

apart from all this just check if dll from other programs are getting loaded sometimes they too interfere in running the program and cause segmentation fault. Like once i wrote a simple c++ program program in vscode and when i compiled and ran my program it was picking up dll from some installed program on my windows which was having standard library of c++ (std::) so it was causing runtime error as segmentation fault. So I removed the path from Windows environment path variable for the program which was causing conflicts and my program ran without any errors.