Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Heap corruption detected ERROR
Forum Updated to NodeBB v4.3 + New Features

Heap corruption detected ERROR

Scheduled Pinned Locked Moved C++ Gurus
3 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • musimbateM Offline
    musimbateM Offline
    musimbate
    wrote on last edited by
    #1

    Hi ,
    I am trying to implement something like the standard c++ string class using cstring functions but I am getting the heap corruption error and debugging didn't help figure it out.My class is declared like this:

    @

    class String
    {
    public:
    //TYPEDEFS AND MEMBER CONSTANTS.

    typedef std::size_t sizetype;
    static const sizetype DEFAULT_CAPACITY=30;
    
    String(const char initial[]="");
    ~String();
    

    ................

    //private:
    char *data;
    sizetype usedSpace;

    };

    @

    and the relevant implementation is shown below:

    @

    //CONSTRUCTOR
    String::String(const char initial[])
    {

    data=new char[strlen(initial)];
    strcpy(data,initial);
    used=strlen(initial);

    }

    String::~String()
    {
    delete [] data;
    }

    @

    But when i instantiate the class in the main function to test ,like this:

    @
    #include <iostream>
    #include <cstring>
    using namespace std;
    int main()
    {

    String mString1("KARAMAGE");
    String mString2("KAREKEZI");

    cout << mString1.data <<endl;
    cout << mString2.data <<endl;

    return 0;
    

    }

    @
    The program crashes when the destructor is called (at least the call stack of my degugger is telling me so).I first thought it was a problem with our processor but I am experiencing the same issues on my intel windows8 machine.Am I doing something wrong here ,as I have always declared pointer arrays and deleted them like this.
    I would also appreciate it if you helped run this code through your environment and tell me what your debugger is saying.

    Thanks for your time.

    Why join the navy if you can be a pirate?-Steve Jobs

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Arnaut
      wrote on last edited by
      #2

      Hi,

      One thing I spotted is that the size of your data buffer is wrong in the String constructor, strlen() doesn't count the terminating null character.

      Hope it helps,
      H.

      1 Reply Last reply
      0
      • musimbateM Offline
        musimbateM Offline
        musimbate
        wrote on last edited by
        #3

        Thanks mate,
        I had been staring at this piece of code for hours without noticing that !It really helped.

        Dan.

        Why join the navy if you can be a pirate?-Steve Jobs

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved