0

I am trying to store a value from a

vector<vector<string>> data;

into a const char* variable - declared in the .h file - in this way:

heightmapName = data[0][1].c_str();

When I debug the program I notice that the variable heightmapName return this

heightmapName 0xcdcdcdcd <Error reading characters of string.>  const char *

However, if I declare a new const char* and initialize it like this:

    const char* what = data[0][1].c_str();
    heightmapName = data[0][1].c_str();

what variable store the data just fine, while heightmapNamedoesn't.

This is the function:

void Configuration::fileParser(string fileName)
{
    vector<vector<string>> data;
    string line;
    string delimiter = " ";
    ifstream ss(fileName);
    if (ss)
    {
        while (getline(ss, line))
        {
            vector<string> dataLine;
            string token = line.substr(0, line.find(delimiter));
            string value = line.substr(line.find(delimiter) +1);
            dataLine.push_back(token);
            dataLine.push_back(value);
            data.push_back(dataLine);
        }
        ss.close();
    }
    //storeData(data);
    const char* ahah = data[0][1].c_str();
    heightmapName =    data[0][1].c_str();
}

Why is this happening? and how can I solve this?

ps. I am using visual studio 2017

17
  • 0xcdcdcdcd .. Looks like uninitialized heap memory: stackoverflow.com/questions/127386/… Commented Aug 10, 2017 at 2:03
  • 2
    Please post a minimal reproducible example, No one knows how, when, or where data is initialized, used, filled with data, etc. Commented Aug 10, 2017 at 2:06
  • I edited the question with the code of the function. Commented Aug 10, 2017 at 2:08
  • heightmapName is undefined. It has no type. This code is not complete and probably won't compile. Is it a field within the Configuration class? Commented Aug 10, 2017 at 2:09
  • 1
    Also, you posted a member function of a class. If that is not a static member function, and if that Configuration object is not valid, no operations that you will do on it will be valid. You need to show us a minimal reproducible example, not a snippet from a class. Commented Aug 10, 2017 at 2:13

1 Answer 1

2

Regardless of the problem or implementation, assuming that the type of heightmapName is indeed const char *, that's not going to work.

The lifetime of the data is bound by the fileParser lifetime. See What is std::string::c_str() lifetime?

Therefore, at the end of that function, the data pointed to by data[0][1].c_str() will become invalid.

Consider copying the data if required. Or make heightmapName an std::string.

(Additional tips: If it's a pointer, consider applying the Rule Of Five: How to actually implement the rule of five? - another reason to avoid manual memory management)

In general, I avoid raw pointers in C++ classes by using smart pointers, or structures (such as std::string) that manages memory for me, this means I do not need to worry about the rule of 3 or rule of 5 as I won't need to manually manage those resources.

update: You mentioned that it "works" for you in a (now removed) gist.

Accessing memory like that after the lifetime has ended is undefined. One behaviour could very well be that it magically "works". Most likely, that memory just hasn't been overwritten yet.

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

Comments

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.