1

I'm new to C++ (I'm most comfortable with node.js and Java, but have done some Python before) and am working through understanding pointers. The following example generates a runtime error when compiled with debug symbols.

#include <iostream>

void main(){
    int number = 0;
    int * numberPtr = &number;
    * numberPtr = 1;

    std::cout << number;
    delete numberPtr;
}

And the error message:

Debug Assertion Failed!

Program: C:\path\to\executable\main.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp

Expression: _BLOCK-TYPE_IS_VALID(pHead->nBlockUse)

FWIW, I've gotten the same error when I've compiled with both VS 2008 and 2013.

1 Answers1

0

As Oli Charlesworth said, u did not dynamically allocate numberPtr, therefore you can't delete it. I suggest reading about new and delete and Dynamic Memory Allocation if you intend on coding in C++.

user3175215
  • 88
  • 1
  • 5
  • 1
    I suggest you don't. There are better alternatives, such as `std::string` and `std::unique_ptr<>` – MSalters Jun 10 '14 at 21:48