can anyone help me optimalize code for reading standard input. Here it is what I have now:
unsigned char *msg;
size_t msgBytes = 0;
size_t inputMsgBuffLen = 1024;
if ( (msg = (unsigned char *) malloc(sizeof(unsigned char) * inputMsgBuffLen) ) == NULL ) {
quitErr("Couldn't allocate memmory!", EXIT_FAILURE);
}
for (int c; (c = getchar()) != EOF; msgBytes++) {
if (msgBytes >= (inputMsgBuffLen)) {
inputMsgBuffLen <<= 1;
if ( ( msg = (unsigned char *)realloc(msg, sizeof(unsigned char) * inputMsgBuffLen) ) == NULL) {
free(msg);
quitErr("Couldn't allocate more memmory!", EXIT_FAILURE);
}
}
msg[msgBytes] = (unsigned char)c;
}