Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segmentation Fault (Core dumped) in c++

This code when executed displays the expected output but prints segmentation fault (core dumped) at the end :

string str[4] = {
    "Home",
    "Office",
    "Table",
    "Bar"
};

for (int i = 0; i<5; i++)
{
    cout << str[i] << "\n";
}

Output:

Home
Office
Table
Bar
Segmentation fault (core dumped)

What is the signinficance of segmentation fault (core dumped). I searched and it seems an error like that occurs when you try to access unallocated memory, so, what's wrong with the above code?

like image 341
Jatin Avatar asked Nov 19 '25 01:11

Jatin


1 Answers

you should write:

for (int i = 0; i<4; i++) //0,1,2,3 = total 4 values
{
    cout << str[i] << "\n";
}
like image 171
Roee Gavirel Avatar answered Nov 20 '25 17:11

Roee Gavirel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!