I'm trying to make a simple program to improve my use of pointers (because I suck very badly at that), and I was doing fine, until I found this problem:

-- HEAP CORRUPTION DETECTED: after Normal block (#155) at 0x007D18F0.

CRT found that the application wrote to memory after end of heap buffer. --

Here is my code:

#include <iostream>

int main(int argc, char** argv){

	//Pointer to an array in the stack, but lets remember its elements are in the heap.

	int Flowers;
	std::cout << "How many Flowers do you want? ";
	std::cin >> Flowers;
	std::cout << "Now processing your flowers." << std::endl;
	int* FlowerInHeap;
	FlowerInHeap = new int[Flowers];

	for(int i = 0; i <= Flowers; i++){

		FlowersInHeap[i] = i + 1;

	for(int i = 0; i <= Flowers; i++){

		std::cout << FlowerInHeap[i] << std::endl;


	delete[] FlowersInHeap;

	std::cout << std::endl << "Process completed." << std::endl;
	return 0;


Now, I have tried to make the pointer point to a null value after deleting it, and that didn't fix the problem, and I don't know what else to try. It seems I can't delete the array when I assign values to the elements. I have tested by commenting all my for loops, and it didn't give me any errors.

Right now, its not a big thing, but what will I do if I try to make a big program and I find this error again? Thanks for any help you may give :).

7 Years
Discussion Span
Last Post by firstPerson
for(int i = 0; i <= Flowers; i++){

		Flowers[i] = i + 1;

Don't you mean FlowersInHeap[i] here?

And, the big problem, is using <= as the test comparison. For Flowers = 5, you are trying to access elements 0 through 5, which is a total of 6 elements. But you only have 5 elements!

Normal for loop for full array access for ( i = 0; i < arr_size; i++ )


Whoops yes. I am actually writing a code which I don't want people to see with the real names and text, so I edited it and I forgot to edit that part. So that's not causing the problem. Thanks for pointing it out thought ^^. I will fix it on my first post.

Just noticed your edit. It works like a charm. Thanks a lot!


for(int i = 0; i <= Flowers; i++){

change that to i < Flowers.

Also notice the error : "application wrote to memory after end of
heap buffer"

It says that you wrote into memory where you were not
supposed to. If thats the case, then look in your code where
you write into heap, and most likely, thats where you will find
your problem.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.