So I am working on an assignment and it works for the most part but at the end I get an error message that says Heap corruption detected after normal block (#137) CRT detected that the application wrote to memory after end of heap. I have been working on this for a while and can't figure out what I am doing wrong. Here is the code:
bufferSize = (unsigned)strlen(charSeq);
buffer = newchar [bufferSize];
for(int i = 0; i <= bufferSize; i++){
buffer[i] = charSeq[i];
First why the C style cast on the first line. The strlen() function returns a size_t, which is how bufferSize should be defined. A size_t is usually an unsigned int, but could be an unsigned long or some other unsigned type, which is implementation defined. So you should be using size_t not unsigned.
Next you appear to have a possible buffer overrun in that loop. Remember arrays start at zero and stop at size - 1. Because you use the <= operator you're stopping at size. Also without seeing the rest of your code it might be possible that charSeq is not terminated, and if that's the case then your strlen() call could be giving you problems as well.
Why are you deleting the buffer? Since you fill the buffer with a character you probably don't need this operation. Also be careful, since you fill the buffer with the 'space' you can't use the C-string functions like strlen() on this array of char, since it is not a C-string because it is not terminated properly.
Lines 8 and 17: You allocate bufferSize characters.
Lines 9 and 18: Your termination conditions are faulty.
Lets assume bufferSize is 10. Your allocated array elements are [0]-[9], but the <= condition causes you to reference buffer[10], which is past the end of the array.