Skip to content
Advertisement

Segmentation Fault with g++ in Linux Ubuntu, but not with g++/MingW in Windows, when printing a char string in C++ [closed]

I have a program, which:

  1. Creates an array of 3 char pointers, char *z_str[3];.
  2. Allocates dynamic memory objects of type char and assign the returned pointers to these char pointers.
  3. Prompt the user for giving Input strings.
  4. Print the provided strings.

The source code:

#include <iostream>
using namespace std;

int main()
{
    char *z_str[3];
    int i;

    for(i = 0; i < 2; i++)
    {
        z_str[i] = new char [30];
        if(z_str[i] == NULL)
        {
            cout << "Memory for String " << i+1 << " could not be allocated!" << endl;
            cout << "Program terminates.";
            return 1;
        }
    }

    cout << endl << endl;

    cout << "Please input the first string [max.29 characters]:" << endl;
    cin >> z_str[0]; 
    cout << endl;

    cout << "Please input the second string [max.29 characters]:" << endl;
    cin >> z_str[1]; 
    cout << endl;

    cout << "Please input the third string [max.29 characters]:" << endl;
    cin >> z_str[2]; 
    cout << endl << endl;


    cout << "First string is:" << endl;
    cout << z_str[0] << endl << endl;

    cout << "Second string is" << endl;
    cout << z_str[1] << endl << endl;

    cout << "Third string is:" << endl;
    cout << z_str[2] << endl << endl;

    return 0;
}

When I do compile this code with g++ in Linux Ubuntu and run the code I get an segmentation fault, when the program comes to the printing of the charstrings into the CLI.

Terminal Output:

Please input the first string:
string1

Please input the second string:
string2

Please input the third string:
string3
Segmentation fault (core dumped)

Now, If I compile the same code with g++/MingW in Windows 10 everything works as it should in the PowerShell:

Please input the first string:
string1

Please input the second string:
string2

Please input the third string:
string3


First string is:
string1

Second string is
string2

Third string is:
string3

  • Why am I getting an Segmentation Fault with g++ in Linux Ubuntu, but not with g++/MingW in Windows, when printing a char string in C++?

Advertisement

Answer

The loop

for(i = 0; i < 2; i++)

will make you initialize z_str[0] and z_str[1], but not z_str[2].

Therefore you get undefined behavior (and the crash) when you use the uninitialized and indeterminate value in z_str[2].

You need to increase the loop to iterate over all elements:

for(i = 0; i < 3; i++)

There are better solutions to all of this, and the one I recommend is to use a std::array of std::string objects:

std::array<std::string, 3> z_str;

Now you don’t need to do any dynamic allocation yourself, and all three strings in the array will be default constructed which means they will be empty (but valid) strings.

If you’re not allowed to use either std::array nor std::string, there’s still other ways to improve your program, for example using range for loops:

for (auto& str_ptr : z_str)
    str_ptr = new char[30];

The above loop is guaranteed to iterate over all elements of the array.

Advertisement