I\'m trying to learn more about memory allocation, and so I wrote some test code below to see what would happen if I tried to allocate memory of a less size than what I need
To answer the subquestion about sizeof: sizeof yields a result based on the type that you are using (in C, there is a separate case for variable length arrays). If you write
T* obj = malloc (any value);
then sizeof (*obj) just looks at the type of *obj, which is T, and yields the size of an object of type T. It doesn't matter whether the allocation failed and obj is actually NULL, or whether you allocated fewer or more bytes than the size of a T, it doesn't even matter if you didn't call malloc at all and obj is an uninitialised variable.