I have the following C code:
char* str = (char*)malloc(sizeof(char));
int count = 0;
while ((c = getchar()) != EOF){
str[count] = c;
count++;
str = (char*)realloc(str, sizeof(str) + sizeof(char));
}
But it is throwing the error Unhandled exception at 0x77C8F94D (ntdll.dll) in algorithms.exe: 0xC0000374: A heap has been corrupted
. I have been trying to solve this for ages, but can't get it right. Interestingly, it is only an issue when the input stream has a large number of characters to be read.
Is this due to my usage of malloc and realloc?