Sign in to follow this  
bjorn88

CRT detected that the application wrote to memory after end of heap buffer

Recommended Posts

bjorn88    100
I just wrote a simple program to test my new knowledge of allocating memory and of course deleting the allocated memory. However, when I run my program I get this strange error. Here's my code:


int main(){

int a = 0;

std::cout << "Enter array size: ";
std::cin >> a;

int* myArray = new int[a];

for (int i=0;i<=a;i++){
myArray[i] = i;
std::cout << myArray[i];
}

delete[] myArray;
myArray = 0;

std::cin.get();

}


Share this post


Link to post
Share on other sites
bjorn88    100
Quote:
Original post by KulSeran
Quote:

for (int i=0;i<=a;i++)

Your array only takes up the range [0,a). You need to write your loop as
for( int i=0; i<a; i++ )

You were writing one past the end of the array.


Hey, thanks very much! I guess it was something simple after all.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this