Here is my code.
int main()
{
char *s;
int i = 0;
printf(\"%lu \\n\", sizeof(s));
s = malloc(sizeof(char) * 2);
printf(\"%lu \\n\", sizeof(s
In my opinion, this should segfault as I'm trying to write more than I allocated. Why is this working?
It's not "working"; your code invokes undefined behavior. "Undefined behavior" doesn't mean "your code will segfault." That would be defined behavior. UB means anything can happen.
In this case, you're stomping on memory you don't own. That will sometimes segfault, but don't count on it. C has no notion of "segfaults", that comes from your OS.