-2

I'm building a class and at some point I call a delete. In codeblocks it works and in Visual Studio 2013 it doesn't.

In my class I have:

    private:
    bool sign;          // 0 if positive, 1 if negative
    int NumberSize;
    int VectorSize;
    int *Number;

Then I have this function:

  void XXLint::Edit(const char* s)
{
// Get Size
this->NumberSize = strlen(s);

// Initialise Sign
if (s[0] == '-')
{
    this->sign = 1;
    s++;
}
else if (s[0] == '+') s++;
else this->sign = 0;

delete[] Number;  // Here the debugger gives me the error

//Get Vector Size

this->VectorSize = this->NumberSize / 4;

// Allocate Memory
this->Number = new int[this->VectorSize];

//Store the string into the number vector.

int location = this->VectorSize;
int current = this->NumberSize - 1;

while (location)
{
    int aux = 0;
    for (int i = 3; i >= 0 && current; i--)
    if (current - i >= 0)
        aux = aux * 10 + s[current - i] - '0';
    current -= 4;
    this->Number[location--] = aux;
}

} I did read the article and it really is interesting :D but i don't belive that's where the error comes from. Why is this error happening?

taigi tanaka
  • 299
  • 1
  • 4
  • 17
  • 3
    You probably have undefined behaviour because you have not implemented an assignment operator and copy constructor. – juanchopanza Apr 21 '14 at 20:01
  • Heap corruption either means that you double freed or overwrite the edge 9of your buffer. – Mats Petersson Apr 21 '14 at 20:03
  • 1
    You may find [**this article**](http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)) an interesting read. – WhozCraig Apr 21 '14 at 20:04
  • 1
    @taigi tanaka - `Both ways work great in codeblocks without bugs` If you're getting an error when running in Visual Studio, then even in CodeBlocks (actually g++), you have bugs and is *not* working great. When you make a mistake such as corrupting memory, anything can happen, including having things seemingly "work". So you are lucky that Visual Studio pointed out that you have a problem with your code. – PaulMcKenzie Apr 21 '14 at 20:08
  • You should post your real code, and not just a call to new[] and delete[]. There is nothing wrong with new[] or delete[] -- it is what you are doing between those two calls that is corrupting the memory. I'm sure if you wrote a program where all it did was a new[] followed by delete[], you won't see anything wrong. So it's all that code you're not showing us that is causing the issue. – PaulMcKenzie Apr 21 '14 at 20:13
  • Visual Studio evidently has mechanisms of detecting errors that CodeBlocks does not have. – Dialecticus Apr 21 '14 at 20:13

1 Answers1

1

Look here:

this->Number = new int[this->VectorSize];
int location = this->VectorSize;

Assume for argument's sake that this->VectorSize == 10. So location now has the value 10. However, later you do this in a loop:

while (location)
{
   //...
   this->Number[location--] = aux;  // out of bounds!
}

You are accessing this->Number[10]. That is a memory overwrite. And no, location doesn't get decremented before it's used, as it is post-decrement, not pre-decrement.

When you compile a program on another compiler and then run the program, if that runtime detects errors, always question your code. It doesn't matter if it "worked" on compiler X, or if it worked on your computer and your friend's computer but not the teacher or customer's computer. Always suspect there is something wrong with your code if there is a failure such as memory corruption.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45