I am able to compile this with gcc using the -Wextra -g
flags and I get no errors. Why does the compiler not catch that I don't return anything from a non-void function? Also, I've run this on a 32-bit and a 64-bit machine. It ran perfectly fine on the 32-bit, and even printed 'Apple stem length 10. Apple color r'. But it seg faults on the 64-bit machine. What is gcc doing here that gives me such unpredictable behavior and why is it not giving me any errors at compile time?
#include <stdlib.h>
#include <stdio.h>
struct apple
{
int stem_length;
char color;
};
struct apple* alloc_apple(const int stem_length, const char color)
{
struct apple* new_apple = (struct apple*)malloc(sizeof(struct apple));
new_apple->stem_length = stem_length;
new_apple->color = color;
}
int main(int argc, char* argv[])
{
struct apple* some_apple = alloc_apple(10, 'r');
printf("Apple stem length %d. Apple color %c\n", some_apple->stem_length,
some_apple->color);
free(some_apple);
return 0;
}