当我使用gcc -Wall
编译时,没有任何警告,这让我感到惊讶。这是真正合法的C代码吗?编写这种代码的风险是什么?
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int a;
int b;
} MyStruct;
int main(void) {
MyStruct *s = malloc(sizeof(*s)); // as opposed to malloc(sizeof(MyStruct))
s->a = 5;
printf("%d\n", s->a);
}