-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it safe to return string.c_str() received from get()? #2130
Labels
kind: question
solution: proposed fix
a fix for the issue has been proposed and waits for confirmation
Comments
With the current code, you make a copy of the stored string and hence return a temporary. Do this instead: const char* GetName(int id) {
return metadata[id]["name"].get_ref<std::string&>().c_str();
} |
nlohmann
added
the
solution: proposed fix
a fix for the issue has been proposed and waits for confirmation
label
May 22, 2020
Looks like
|
Ok, does this fix the issue for you? |
Yes, Thank you for your help! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
kind: question
solution: proposed fix
a fix for the issue has been proposed and waits for confirmation
What do you want to achieve?
I'm building a function
const char* GetName(id)
.Data is stored in json file, e.g.
[{"name":"N1"}, {"name":"N2"}, {"name":"N3"}]
Is it safe for my function to return c_str pointers from
std::string
received fromget()
? I'm not sure aboutstd::string
objects lifecycle insidenlohmann::json
.Example:
The text was updated successfully, but these errors were encountered: