Peter N Peter N - 1 month ago 7
C Question

'new' undeclared (first use in this function)

Why does this work in C++, but not in C? And what is the alternative for a C environment?

char *Buffer;
unsigned long Size;

Buffer = new char[Size]; // Throws an error in C, but not in C++

Answer

new and delete are not part of C. Please use malloc and free.

char *Buffer;
size_t Size = 100;
Buffer = malloc(Size);
if(Buffer == NULL) {
    puts("Bad allocation");
}
else {
    free(Buffer);
}