Debug error: stack around the variable 'cardDesc' was corrupted





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















TITLE.
The function i'm using is this:



void GetVideoCardInfo(int* memoryVar, char* cardName)
{
strcpy_s(cardName, 128, m_videoCardDescription);
*memoryVar = m_videoCardMemory;
return;
}


m_videoCardDescription is a '128 character long' character array that has the description of my video card in it. Here is where I am calling the function:



bool writeGPUnameDesc() {
char cardDesc;
int cardMem;
m_D3D->GetVideoCardInfo(&cardMem, &cardDesc);

std::ofstream myfile;
myfile.open("gpuNameAndDesc.txt");
myfile << "Graphics card name: " << cardDesc;
myfile << " - Graphics card memory: " << cardMem;
myfile.close();

return true;
}


When I run the program a message box pops up that says Runtime-check failure #2
and the title. If anyone can help thanks in advance.










share|improve this question

























  • cardDesc has room for exactly one character. You read 128 characters into that and you're gonna have a bad time.

    – user4581301
    Nov 21 '18 at 23:20











  • Is there a way I can make it able to store 128 characters but still work as a pointer?

    – Max
    Nov 21 '18 at 23:23











  • How about char cardDesc[128]; ....

    – paddy
    Nov 21 '18 at 23:24






  • 1





    Why are you messing with C-style strings in C++? What's the problem with std::string?

    – Fei Xiang
    Nov 21 '18 at 23:30


















0















TITLE.
The function i'm using is this:



void GetVideoCardInfo(int* memoryVar, char* cardName)
{
strcpy_s(cardName, 128, m_videoCardDescription);
*memoryVar = m_videoCardMemory;
return;
}


m_videoCardDescription is a '128 character long' character array that has the description of my video card in it. Here is where I am calling the function:



bool writeGPUnameDesc() {
char cardDesc;
int cardMem;
m_D3D->GetVideoCardInfo(&cardMem, &cardDesc);

std::ofstream myfile;
myfile.open("gpuNameAndDesc.txt");
myfile << "Graphics card name: " << cardDesc;
myfile << " - Graphics card memory: " << cardMem;
myfile.close();

return true;
}


When I run the program a message box pops up that says Runtime-check failure #2
and the title. If anyone can help thanks in advance.










share|improve this question

























  • cardDesc has room for exactly one character. You read 128 characters into that and you're gonna have a bad time.

    – user4581301
    Nov 21 '18 at 23:20











  • Is there a way I can make it able to store 128 characters but still work as a pointer?

    – Max
    Nov 21 '18 at 23:23











  • How about char cardDesc[128]; ....

    – paddy
    Nov 21 '18 at 23:24






  • 1





    Why are you messing with C-style strings in C++? What's the problem with std::string?

    – Fei Xiang
    Nov 21 '18 at 23:30














0












0








0








TITLE.
The function i'm using is this:



void GetVideoCardInfo(int* memoryVar, char* cardName)
{
strcpy_s(cardName, 128, m_videoCardDescription);
*memoryVar = m_videoCardMemory;
return;
}


m_videoCardDescription is a '128 character long' character array that has the description of my video card in it. Here is where I am calling the function:



bool writeGPUnameDesc() {
char cardDesc;
int cardMem;
m_D3D->GetVideoCardInfo(&cardMem, &cardDesc);

std::ofstream myfile;
myfile.open("gpuNameAndDesc.txt");
myfile << "Graphics card name: " << cardDesc;
myfile << " - Graphics card memory: " << cardMem;
myfile.close();

return true;
}


When I run the program a message box pops up that says Runtime-check failure #2
and the title. If anyone can help thanks in advance.










share|improve this question
















TITLE.
The function i'm using is this:



void GetVideoCardInfo(int* memoryVar, char* cardName)
{
strcpy_s(cardName, 128, m_videoCardDescription);
*memoryVar = m_videoCardMemory;
return;
}


m_videoCardDescription is a '128 character long' character array that has the description of my video card in it. Here is where I am calling the function:



bool writeGPUnameDesc() {
char cardDesc;
int cardMem;
m_D3D->GetVideoCardInfo(&cardMem, &cardDesc);

std::ofstream myfile;
myfile.open("gpuNameAndDesc.txt");
myfile << "Graphics card name: " << cardDesc;
myfile << " - Graphics card memory: " << cardMem;
myfile.close();

return true;
}


When I run the program a message box pops up that says Runtime-check failure #2
and the title. If anyone can help thanks in advance.







c++ pointers stack






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 23:21







Max

















asked Nov 21 '18 at 23:19









MaxMax

107




107













  • cardDesc has room for exactly one character. You read 128 characters into that and you're gonna have a bad time.

    – user4581301
    Nov 21 '18 at 23:20











  • Is there a way I can make it able to store 128 characters but still work as a pointer?

    – Max
    Nov 21 '18 at 23:23











  • How about char cardDesc[128]; ....

    – paddy
    Nov 21 '18 at 23:24






  • 1





    Why are you messing with C-style strings in C++? What's the problem with std::string?

    – Fei Xiang
    Nov 21 '18 at 23:30



















  • cardDesc has room for exactly one character. You read 128 characters into that and you're gonna have a bad time.

    – user4581301
    Nov 21 '18 at 23:20











  • Is there a way I can make it able to store 128 characters but still work as a pointer?

    – Max
    Nov 21 '18 at 23:23











  • How about char cardDesc[128]; ....

    – paddy
    Nov 21 '18 at 23:24






  • 1





    Why are you messing with C-style strings in C++? What's the problem with std::string?

    – Fei Xiang
    Nov 21 '18 at 23:30

















cardDesc has room for exactly one character. You read 128 characters into that and you're gonna have a bad time.

– user4581301
Nov 21 '18 at 23:20





cardDesc has room for exactly one character. You read 128 characters into that and you're gonna have a bad time.

– user4581301
Nov 21 '18 at 23:20













Is there a way I can make it able to store 128 characters but still work as a pointer?

– Max
Nov 21 '18 at 23:23





Is there a way I can make it able to store 128 characters but still work as a pointer?

– Max
Nov 21 '18 at 23:23













How about char cardDesc[128]; ....

– paddy
Nov 21 '18 at 23:24





How about char cardDesc[128]; ....

– paddy
Nov 21 '18 at 23:24




1




1





Why are you messing with C-style strings in C++? What's the problem with std::string?

– Fei Xiang
Nov 21 '18 at 23:30





Why are you messing with C-style strings in C++? What's the problem with std::string?

– Fei Xiang
Nov 21 '18 at 23:30












2 Answers
2






active

oldest

votes


















5














You are copying 128 characters into char cardDesc, which represents only 1 character.



You should change the type of cardDesc to a char-array:



char cardDesc[128];
// ...
m_D3D->GetVideoCardInfo(&cardMem, cardDesc);
// ^ no &





share|improve this answer
























  • Where does the null terminator go?

    – user4581301
    Nov 21 '18 at 23:35











  • @user4581301 inside cardDesc, according to the documentation of strcpy_s

    – M.M
    Nov 22 '18 at 0:10











  • Can't argue that. What I'm worried about is is the originator 128 characters plus terminator or 128 including terminator. Could be dropping a character.

    – user4581301
    Nov 22 '18 at 0:13



















0














TL;DR



std::string GetVideoCardInfo(int & memoryVar)
{
memoryVar = m_videoCardMemory;
return m_videoCardDescription;;
}


bool writeGPUnameDesc() {
int cardMem;
std::string cardDesc = m_D3D->GetVideoCardInfo(cardMem);

std::ofstream myfile;
myfile.open("gpuNameAndDesc.txt");
myfile << "Graphics card name: " << cardDesc;
myfile << " - Graphics card memory: " << cardMem;
myfile.close();

return true;
}


Explanation



strcpy_s(cardName, 128, m_videoCardDescription);


is a blatant lie. The size of cardName is exactly one character. If you lie to strcpy_s its extra checking to make sure you don't overrun the buffer cannot help you.



Inferior solutions



Replace



char cardDesc;   


with



char cardDesc[129]; 


The better approach gets rid of the magic numbers entirely.



Up near the top of the file



namespace // annonymous namespace. Contents will not leak out of the file
{
constexpr int MAX_CARD_NAME_LENGTH = 129 // 128 plus room for nul terminator
}


and then



void GetVideoCardInfo(int* memoryVar, char* cardName)
{
strcpy_s(cardName, MAX_CARD_NAME_LENGTH, m_videoCardDescription);
*memoryVar = m_videoCardMemory;
return;
}


and



bool writeGPUnameDesc() {
char cardDesc[MAX_CARD_NAME_LENGTH]; // now an array
int cardMem;
m_D3D->GetVideoCardInfo(&cardMem, cardDesc); // don't need to take address of array

std::ofstream myfile;
myfile.open("gpuNameAndDesc.txt");
myfile << "Graphics card name: " << cardDesc;
myfile << " - Graphics card memory: " << cardMem;
myfile.close();

return true;
}





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%2f53421804%2fdebug-error-stack-around-the-variable-carddesc-was-corrupted%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    You are copying 128 characters into char cardDesc, which represents only 1 character.



    You should change the type of cardDesc to a char-array:



    char cardDesc[128];
    // ...
    m_D3D->GetVideoCardInfo(&cardMem, cardDesc);
    // ^ no &





    share|improve this answer
























    • Where does the null terminator go?

      – user4581301
      Nov 21 '18 at 23:35











    • @user4581301 inside cardDesc, according to the documentation of strcpy_s

      – M.M
      Nov 22 '18 at 0:10











    • Can't argue that. What I'm worried about is is the originator 128 characters plus terminator or 128 including terminator. Could be dropping a character.

      – user4581301
      Nov 22 '18 at 0:13
















    5














    You are copying 128 characters into char cardDesc, which represents only 1 character.



    You should change the type of cardDesc to a char-array:



    char cardDesc[128];
    // ...
    m_D3D->GetVideoCardInfo(&cardMem, cardDesc);
    // ^ no &





    share|improve this answer
























    • Where does the null terminator go?

      – user4581301
      Nov 21 '18 at 23:35











    • @user4581301 inside cardDesc, according to the documentation of strcpy_s

      – M.M
      Nov 22 '18 at 0:10











    • Can't argue that. What I'm worried about is is the originator 128 characters plus terminator or 128 including terminator. Could be dropping a character.

      – user4581301
      Nov 22 '18 at 0:13














    5












    5








    5







    You are copying 128 characters into char cardDesc, which represents only 1 character.



    You should change the type of cardDesc to a char-array:



    char cardDesc[128];
    // ...
    m_D3D->GetVideoCardInfo(&cardMem, cardDesc);
    // ^ no &





    share|improve this answer













    You are copying 128 characters into char cardDesc, which represents only 1 character.



    You should change the type of cardDesc to a char-array:



    char cardDesc[128];
    // ...
    m_D3D->GetVideoCardInfo(&cardMem, cardDesc);
    // ^ no &






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 23:25









    Martin HeraleckýMartin Heralecký

    3,18421135




    3,18421135













    • Where does the null terminator go?

      – user4581301
      Nov 21 '18 at 23:35











    • @user4581301 inside cardDesc, according to the documentation of strcpy_s

      – M.M
      Nov 22 '18 at 0:10











    • Can't argue that. What I'm worried about is is the originator 128 characters plus terminator or 128 including terminator. Could be dropping a character.

      – user4581301
      Nov 22 '18 at 0:13



















    • Where does the null terminator go?

      – user4581301
      Nov 21 '18 at 23:35











    • @user4581301 inside cardDesc, according to the documentation of strcpy_s

      – M.M
      Nov 22 '18 at 0:10











    • Can't argue that. What I'm worried about is is the originator 128 characters plus terminator or 128 including terminator. Could be dropping a character.

      – user4581301
      Nov 22 '18 at 0:13

















    Where does the null terminator go?

    – user4581301
    Nov 21 '18 at 23:35





    Where does the null terminator go?

    – user4581301
    Nov 21 '18 at 23:35













    @user4581301 inside cardDesc, according to the documentation of strcpy_s

    – M.M
    Nov 22 '18 at 0:10





    @user4581301 inside cardDesc, according to the documentation of strcpy_s

    – M.M
    Nov 22 '18 at 0:10













    Can't argue that. What I'm worried about is is the originator 128 characters plus terminator or 128 including terminator. Could be dropping a character.

    – user4581301
    Nov 22 '18 at 0:13





    Can't argue that. What I'm worried about is is the originator 128 characters plus terminator or 128 including terminator. Could be dropping a character.

    – user4581301
    Nov 22 '18 at 0:13













    0














    TL;DR



    std::string GetVideoCardInfo(int & memoryVar)
    {
    memoryVar = m_videoCardMemory;
    return m_videoCardDescription;;
    }


    bool writeGPUnameDesc() {
    int cardMem;
    std::string cardDesc = m_D3D->GetVideoCardInfo(cardMem);

    std::ofstream myfile;
    myfile.open("gpuNameAndDesc.txt");
    myfile << "Graphics card name: " << cardDesc;
    myfile << " - Graphics card memory: " << cardMem;
    myfile.close();

    return true;
    }


    Explanation



    strcpy_s(cardName, 128, m_videoCardDescription);


    is a blatant lie. The size of cardName is exactly one character. If you lie to strcpy_s its extra checking to make sure you don't overrun the buffer cannot help you.



    Inferior solutions



    Replace



    char cardDesc;   


    with



    char cardDesc[129]; 


    The better approach gets rid of the magic numbers entirely.



    Up near the top of the file



    namespace // annonymous namespace. Contents will not leak out of the file
    {
    constexpr int MAX_CARD_NAME_LENGTH = 129 // 128 plus room for nul terminator
    }


    and then



    void GetVideoCardInfo(int* memoryVar, char* cardName)
    {
    strcpy_s(cardName, MAX_CARD_NAME_LENGTH, m_videoCardDescription);
    *memoryVar = m_videoCardMemory;
    return;
    }


    and



    bool writeGPUnameDesc() {
    char cardDesc[MAX_CARD_NAME_LENGTH]; // now an array
    int cardMem;
    m_D3D->GetVideoCardInfo(&cardMem, cardDesc); // don't need to take address of array

    std::ofstream myfile;
    myfile.open("gpuNameAndDesc.txt");
    myfile << "Graphics card name: " << cardDesc;
    myfile << " - Graphics card memory: " << cardMem;
    myfile.close();

    return true;
    }





    share|improve this answer






























      0














      TL;DR



      std::string GetVideoCardInfo(int & memoryVar)
      {
      memoryVar = m_videoCardMemory;
      return m_videoCardDescription;;
      }


      bool writeGPUnameDesc() {
      int cardMem;
      std::string cardDesc = m_D3D->GetVideoCardInfo(cardMem);

      std::ofstream myfile;
      myfile.open("gpuNameAndDesc.txt");
      myfile << "Graphics card name: " << cardDesc;
      myfile << " - Graphics card memory: " << cardMem;
      myfile.close();

      return true;
      }


      Explanation



      strcpy_s(cardName, 128, m_videoCardDescription);


      is a blatant lie. The size of cardName is exactly one character. If you lie to strcpy_s its extra checking to make sure you don't overrun the buffer cannot help you.



      Inferior solutions



      Replace



      char cardDesc;   


      with



      char cardDesc[129]; 


      The better approach gets rid of the magic numbers entirely.



      Up near the top of the file



      namespace // annonymous namespace. Contents will not leak out of the file
      {
      constexpr int MAX_CARD_NAME_LENGTH = 129 // 128 plus room for nul terminator
      }


      and then



      void GetVideoCardInfo(int* memoryVar, char* cardName)
      {
      strcpy_s(cardName, MAX_CARD_NAME_LENGTH, m_videoCardDescription);
      *memoryVar = m_videoCardMemory;
      return;
      }


      and



      bool writeGPUnameDesc() {
      char cardDesc[MAX_CARD_NAME_LENGTH]; // now an array
      int cardMem;
      m_D3D->GetVideoCardInfo(&cardMem, cardDesc); // don't need to take address of array

      std::ofstream myfile;
      myfile.open("gpuNameAndDesc.txt");
      myfile << "Graphics card name: " << cardDesc;
      myfile << " - Graphics card memory: " << cardMem;
      myfile.close();

      return true;
      }





      share|improve this answer




























        0












        0








        0







        TL;DR



        std::string GetVideoCardInfo(int & memoryVar)
        {
        memoryVar = m_videoCardMemory;
        return m_videoCardDescription;;
        }


        bool writeGPUnameDesc() {
        int cardMem;
        std::string cardDesc = m_D3D->GetVideoCardInfo(cardMem);

        std::ofstream myfile;
        myfile.open("gpuNameAndDesc.txt");
        myfile << "Graphics card name: " << cardDesc;
        myfile << " - Graphics card memory: " << cardMem;
        myfile.close();

        return true;
        }


        Explanation



        strcpy_s(cardName, 128, m_videoCardDescription);


        is a blatant lie. The size of cardName is exactly one character. If you lie to strcpy_s its extra checking to make sure you don't overrun the buffer cannot help you.



        Inferior solutions



        Replace



        char cardDesc;   


        with



        char cardDesc[129]; 


        The better approach gets rid of the magic numbers entirely.



        Up near the top of the file



        namespace // annonymous namespace. Contents will not leak out of the file
        {
        constexpr int MAX_CARD_NAME_LENGTH = 129 // 128 plus room for nul terminator
        }


        and then



        void GetVideoCardInfo(int* memoryVar, char* cardName)
        {
        strcpy_s(cardName, MAX_CARD_NAME_LENGTH, m_videoCardDescription);
        *memoryVar = m_videoCardMemory;
        return;
        }


        and



        bool writeGPUnameDesc() {
        char cardDesc[MAX_CARD_NAME_LENGTH]; // now an array
        int cardMem;
        m_D3D->GetVideoCardInfo(&cardMem, cardDesc); // don't need to take address of array

        std::ofstream myfile;
        myfile.open("gpuNameAndDesc.txt");
        myfile << "Graphics card name: " << cardDesc;
        myfile << " - Graphics card memory: " << cardMem;
        myfile.close();

        return true;
        }





        share|improve this answer















        TL;DR



        std::string GetVideoCardInfo(int & memoryVar)
        {
        memoryVar = m_videoCardMemory;
        return m_videoCardDescription;;
        }


        bool writeGPUnameDesc() {
        int cardMem;
        std::string cardDesc = m_D3D->GetVideoCardInfo(cardMem);

        std::ofstream myfile;
        myfile.open("gpuNameAndDesc.txt");
        myfile << "Graphics card name: " << cardDesc;
        myfile << " - Graphics card memory: " << cardMem;
        myfile.close();

        return true;
        }


        Explanation



        strcpy_s(cardName, 128, m_videoCardDescription);


        is a blatant lie. The size of cardName is exactly one character. If you lie to strcpy_s its extra checking to make sure you don't overrun the buffer cannot help you.



        Inferior solutions



        Replace



        char cardDesc;   


        with



        char cardDesc[129]; 


        The better approach gets rid of the magic numbers entirely.



        Up near the top of the file



        namespace // annonymous namespace. Contents will not leak out of the file
        {
        constexpr int MAX_CARD_NAME_LENGTH = 129 // 128 plus room for nul terminator
        }


        and then



        void GetVideoCardInfo(int* memoryVar, char* cardName)
        {
        strcpy_s(cardName, MAX_CARD_NAME_LENGTH, m_videoCardDescription);
        *memoryVar = m_videoCardMemory;
        return;
        }


        and



        bool writeGPUnameDesc() {
        char cardDesc[MAX_CARD_NAME_LENGTH]; // now an array
        int cardMem;
        m_D3D->GetVideoCardInfo(&cardMem, cardDesc); // don't need to take address of array

        std::ofstream myfile;
        myfile.open("gpuNameAndDesc.txt");
        myfile << "Graphics card name: " << cardDesc;
        myfile << " - Graphics card memory: " << cardMem;
        myfile.close();

        return true;
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 23:40

























        answered Nov 21 '18 at 23:32









        user4581301user4581301

        21.1k52034




        21.1k52034






























            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%2f53421804%2fdebug-error-stack-around-the-variable-carddesc-was-corrupted%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

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)