CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2010
    Posts
    13

    Out-of-bounds access (ARRAY_VS_SINGLETON)

    Need little help..

    #include <iostream>
    int main() {
    int bit = 1;
    int init = 0xf ^ (1 << bit);
    char* c = new char(2);
    sprintf(c, "%x", init);
    std::string initVal = std::string("4'h") + c;
    std::cout << initVal << std::endl;
    }

    Above code is compiling as I expect it to be.
    Problem is when I run ******** on it, it prompts me the following message:

    Out-of-bounds access (ARRAY_VS_SINGLETON). Passing "c" to function "operator +(HSTString const &, char const *)" which uses it as an array. This might corrupt or misinterpret adjacent memory locations


    I am out of ideas for now. Can champs here figure out what I am doing wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Out-of-bounds access (ARRAY_VS_SINGLETON)

    You cannot store the result of sprintf call into the location pointed to by c in the first place because you only allocated a single char, initialising it with a value of 2. Perhaps you wanted to use new char[2] instead, but that would still be rather unnecessary. Instead, you might as well just use the standard C++-specific facilities, e.g.,
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <sstream>
    
    int main()
    {
        unsigned int bit = 1;
        unsigned int init = 0xf ^ (1u << bit);
        std::stringstream ss;
        ss << "4'h" << std::hex << init;
        std::string initVal = ss.str();
        std::cout << initVal << std::endl;
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2010
    Posts
    13

    Re: Out-of-bounds access (ARRAY_VS_SINGLETON)

    That is exactly clarification I was looking for.. I should have figured that out myself.
    Thanks for your help..

    Quote Originally Posted by laserlight View Post
    You cannot store the result of sprintf call into the location pointed to by c in the first place because you only allocated a single char, initialising it with a value of 2. Perhaps you wanted to use new char[2] instead, but that would still be rather unnecessary. Instead, you might as well just use the standard C++-specific facilities, e.g.,
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <sstream>
    
    int main()
    {
        unsigned int bit = 1;
        unsigned int init = 0xf ^ (1u << bit);
        std::stringstream ss;
        ss << "4'h" << std::hex << init;
        std::string initVal = ss.str();
        std::cout << initVal << std::endl;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured