How can i change my const char pointer to unique pointer?
So I am trying to change my game and I found a problem where I couldn't make the change:
I have the following code:
std::string fileContents = "";
const char* contentsPtr = fileContents.c_str();
I tried making it:
const std::unique_ptr<char> contentsPtr = fileContents.c_str();
It doesn't work since there is no constructor to make the conversion from char*
to unique_ptr so how can I make this change?
c++ unique-ptr
|
show 2 more comments
So I am trying to change my game and I found a problem where I couldn't make the change:
I have the following code:
std::string fileContents = "";
const char* contentsPtr = fileContents.c_str();
I tried making it:
const std::unique_ptr<char> contentsPtr = fileContents.c_str();
It doesn't work since there is no constructor to make the conversion from char*
to unique_ptr so how can I make this change?
c++ unique-ptr
4
The pointer returned byc_str()is not up for grabs, it's owned byfileContents. You can't just shove it in aunique_ptrand let it own it. Why do you even think you need aunique_ptrin your case?
– StoryTeller
Nov 11 '18 at 11:01
2
In that case, why havecontentsPtrat all? What does your code use it for?
– melpomene
Nov 11 '18 at 11:10
1
@Ninhow Explain what you really need to do. Why are you not supposed to use naked pointers? It's error-prone? It's inefficient? It looks ugly? It's hard to understand?
– user202729
Nov 11 '18 at 11:14
1
@Ninhow - That's not a bad goal in general. But one should not be going about it by way of cargo cult programming.
– StoryTeller
Nov 11 '18 at 11:15
1
The question to ask is who owns the data. In this case it's fileContents. Smart pointers own the data and you cannot have more than one owner. Multiple shared pointers can share ownership with each other, but not with something else. You may want to watch youtu.be/JfmTagWcqoE
– Michael Surette
Nov 11 '18 at 11:27
|
show 2 more comments
So I am trying to change my game and I found a problem where I couldn't make the change:
I have the following code:
std::string fileContents = "";
const char* contentsPtr = fileContents.c_str();
I tried making it:
const std::unique_ptr<char> contentsPtr = fileContents.c_str();
It doesn't work since there is no constructor to make the conversion from char*
to unique_ptr so how can I make this change?
c++ unique-ptr
So I am trying to change my game and I found a problem where I couldn't make the change:
I have the following code:
std::string fileContents = "";
const char* contentsPtr = fileContents.c_str();
I tried making it:
const std::unique_ptr<char> contentsPtr = fileContents.c_str();
It doesn't work since there is no constructor to make the conversion from char*
to unique_ptr so how can I make this change?
c++ unique-ptr
c++ unique-ptr
asked Nov 11 '18 at 10:59
NinhowNinhow
247
247
4
The pointer returned byc_str()is not up for grabs, it's owned byfileContents. You can't just shove it in aunique_ptrand let it own it. Why do you even think you need aunique_ptrin your case?
– StoryTeller
Nov 11 '18 at 11:01
2
In that case, why havecontentsPtrat all? What does your code use it for?
– melpomene
Nov 11 '18 at 11:10
1
@Ninhow Explain what you really need to do. Why are you not supposed to use naked pointers? It's error-prone? It's inefficient? It looks ugly? It's hard to understand?
– user202729
Nov 11 '18 at 11:14
1
@Ninhow - That's not a bad goal in general. But one should not be going about it by way of cargo cult programming.
– StoryTeller
Nov 11 '18 at 11:15
1
The question to ask is who owns the data. In this case it's fileContents. Smart pointers own the data and you cannot have more than one owner. Multiple shared pointers can share ownership with each other, but not with something else. You may want to watch youtu.be/JfmTagWcqoE
– Michael Surette
Nov 11 '18 at 11:27
|
show 2 more comments
4
The pointer returned byc_str()is not up for grabs, it's owned byfileContents. You can't just shove it in aunique_ptrand let it own it. Why do you even think you need aunique_ptrin your case?
– StoryTeller
Nov 11 '18 at 11:01
2
In that case, why havecontentsPtrat all? What does your code use it for?
– melpomene
Nov 11 '18 at 11:10
1
@Ninhow Explain what you really need to do. Why are you not supposed to use naked pointers? It's error-prone? It's inefficient? It looks ugly? It's hard to understand?
– user202729
Nov 11 '18 at 11:14
1
@Ninhow - That's not a bad goal in general. But one should not be going about it by way of cargo cult programming.
– StoryTeller
Nov 11 '18 at 11:15
1
The question to ask is who owns the data. In this case it's fileContents. Smart pointers own the data and you cannot have more than one owner. Multiple shared pointers can share ownership with each other, but not with something else. You may want to watch youtu.be/JfmTagWcqoE
– Michael Surette
Nov 11 '18 at 11:27
4
4
The pointer returned by
c_str() is not up for grabs, it's owned by fileContents. You can't just shove it in a unique_ptr and let it own it. Why do you even think you need a unique_ptr in your case?– StoryTeller
Nov 11 '18 at 11:01
The pointer returned by
c_str() is not up for grabs, it's owned by fileContents. You can't just shove it in a unique_ptr and let it own it. Why do you even think you need a unique_ptr in your case?– StoryTeller
Nov 11 '18 at 11:01
2
2
In that case, why have
contentsPtr at all? What does your code use it for?– melpomene
Nov 11 '18 at 11:10
In that case, why have
contentsPtr at all? What does your code use it for?– melpomene
Nov 11 '18 at 11:10
1
1
@Ninhow Explain what you really need to do. Why are you not supposed to use naked pointers? It's error-prone? It's inefficient? It looks ugly? It's hard to understand?
– user202729
Nov 11 '18 at 11:14
@Ninhow Explain what you really need to do. Why are you not supposed to use naked pointers? It's error-prone? It's inefficient? It looks ugly? It's hard to understand?
– user202729
Nov 11 '18 at 11:14
1
1
@Ninhow - That's not a bad goal in general. But one should not be going about it by way of cargo cult programming.
– StoryTeller
Nov 11 '18 at 11:15
@Ninhow - That's not a bad goal in general. But one should not be going about it by way of cargo cult programming.
– StoryTeller
Nov 11 '18 at 11:15
1
1
The question to ask is who owns the data. In this case it's fileContents. Smart pointers own the data and you cannot have more than one owner. Multiple shared pointers can share ownership with each other, but not with something else. You may want to watch youtu.be/JfmTagWcqoE
– Michael Surette
Nov 11 '18 at 11:27
The question to ask is who owns the data. In this case it's fileContents. Smart pointers own the data and you cannot have more than one owner. Multiple shared pointers can share ownership with each other, but not with something else. You may want to watch youtu.be/JfmTagWcqoE
– Michael Surette
Nov 11 '18 at 11:27
|
show 2 more comments
1 Answer
1
active
oldest
votes
You do not want a unique_ptr around any resource you do not own. In this case the pointer returned from c_str(). It still belongs to fileContents objects. If/when you get past actually getting a unique_ptr around c_str(), then you later have memory corruption.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248045%2fhow-can-i-change-my-const-char-pointer-to-unique-pointer%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You do not want a unique_ptr around any resource you do not own. In this case the pointer returned from c_str(). It still belongs to fileContents objects. If/when you get past actually getting a unique_ptr around c_str(), then you later have memory corruption.
add a comment |
You do not want a unique_ptr around any resource you do not own. In this case the pointer returned from c_str(). It still belongs to fileContents objects. If/when you get past actually getting a unique_ptr around c_str(), then you later have memory corruption.
add a comment |
You do not want a unique_ptr around any resource you do not own. In this case the pointer returned from c_str(). It still belongs to fileContents objects. If/when you get past actually getting a unique_ptr around c_str(), then you later have memory corruption.
You do not want a unique_ptr around any resource you do not own. In this case the pointer returned from c_str(). It still belongs to fileContents objects. If/when you get past actually getting a unique_ptr around c_str(), then you later have memory corruption.
answered Nov 11 '18 at 11:03
Bo RBo R
626110
626110
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248045%2fhow-can-i-change-my-const-char-pointer-to-unique-pointer%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
The pointer returned by
c_str()is not up for grabs, it's owned byfileContents. You can't just shove it in aunique_ptrand let it own it. Why do you even think you need aunique_ptrin your case?– StoryTeller
Nov 11 '18 at 11:01
2
In that case, why have
contentsPtrat all? What does your code use it for?– melpomene
Nov 11 '18 at 11:10
1
@Ninhow Explain what you really need to do. Why are you not supposed to use naked pointers? It's error-prone? It's inefficient? It looks ugly? It's hard to understand?
– user202729
Nov 11 '18 at 11:14
1
@Ninhow - That's not a bad goal in general. But one should not be going about it by way of cargo cult programming.
– StoryTeller
Nov 11 '18 at 11:15
1
The question to ask is who owns the data. In this case it's fileContents. Smart pointers own the data and you cannot have more than one owner. Multiple shared pointers can share ownership with each other, but not with something else. You may want to watch youtu.be/JfmTagWcqoE
– Michael Surette
Nov 11 '18 at 11:27