1

I have written the next similar example when I get a segmentation fault error

{
    const char** var = NULL;
    const char* tmp = "Hello! How are you?";
    var = (const char**)malloc(5 * sizeof(char*));

    for (int i = 0; i < 5; i++)  
    {
        var[i] = (char*)malloc(50* sizeof(char));
        strcpy((char*)var[i], tmp);
    }
    for (int i = 0; var[i]; i++)
    {
        std::cout << (long int)var[i] << std::endl;
        std::cout << var[i] << std::endl;
    }
    // Free memory
    ....
    
}  

And on the 6th iteration, for loop doesn't stop (I expected then var[i]==NULL) and I get the "Segmentation fault" error. Can you explain what I am doing wrong, please?

6
  • Where does this crash? This is where a debugger helps. Commented Dec 15, 2020 at 9:09
  • This isn't C, either. It appears to be C++, so retagged accordingly. Commented Dec 15, 2020 at 9:10
  • 1
    Why would you expect var[6] == NULL? Commented Dec 15, 2020 at 9:10
  • As pointed by Peter, out of your belongings memory can be anything, not necessarily NULL. Commented Dec 15, 2020 at 9:11
  • Is the intent of (long int)var[i] to perform a string to integer conversion, or to represent that pointer as a number just to see what it happens to be? Commented Dec 15, 2020 at 9:15

1 Answer 1

4

This walks off the end of the array:

for (int i = 0; var[i]; i++)

Accessing var out of bounds is undefined behaviour, which can lead to a crash. You won't get a nice, neat value when you exceed the bounds like you might in JavaScript, Python or Ruby, you get problems. Unfortunately there's no checks for this, so you must be vigilant about not doing it inadvertently, the responsibility is on you.

Since you're apparently using C++ there's a lot of things you can fix here:

  • Don't use C arrays if you can avoid it, use std::vector
  • Don't use C strings if you can avoid it, use std::string

Combining this advice you get:

const std::string tmp = "Hello! How are you?";

std::vector<std::string> var;

for (int i = 0; i < 5; i++) {
  var.push_back(tmp);
}

for (auto& v : var) {
  std::cout << v << std::endl;
}

// No need to free memory, it's already handled for you.

When you're writing C++, try and enjoy the Standard Library. It will make your life significantly easier than leaning heavily on error-prone C techniques.

Sign up to request clarification or add additional context in comments.

3 Comments

But how I can do it in C-style?
You do it very carefully and make a point to never, ever walk off the end of an array. Always keep track of the maximum index you can use. Normally this is done by having a size variable, though there are other ways, like const values. It really depends on what the context is.
This is like driving on a mountain road with a very steep drop on the one side and absolutely no barrier. How do you not drive off the mountain? You pay very careful attention to what you're doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.