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

Started by
1 comment, last by bjorn88 13 years, 7 months ago
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;		std::cout << myArray;	}	delete[] myArray;	myArray = 0;	std::cin.get();}
Advertisement
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.
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.

This topic is closed to new replies.

Advertisement