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 :).