How can i change my const char pointer to unique pointer?










0















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?










share|improve this question

















  • 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







  • 2





    In that case, why have contentsPtr at 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















0















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?










share|improve this question

















  • 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







  • 2





    In that case, why have contentsPtr at 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













0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 '18 at 10:59









NinhowNinhow

247




247







  • 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







  • 2





    In that case, why have contentsPtr at 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





    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





    In that case, why have contentsPtr at 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












1 Answer
1






active

oldest

votes


















4














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.






share|improve this answer






















    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
    );



    );













    draft saved

    draft discarded


















    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









    4














    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.






    share|improve this answer



























      4














      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.






      share|improve this answer

























        4












        4








        4







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 '18 at 11:03









        Bo RBo R

        626110




        626110



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

            How do I collapse sections of code in Visual Studio Code for Windows?