I get a bad feeling about this code
widget* GetNewWidget()
{
widget* theWidget = (widget *) malloc(sizeof(widget));
return theWidget;
}
Firstly, one should never cast the result of malloc() (nor, I suspect, use it in C++ (?)).
Secondly, won't theWidget
be allocated on the stack?
If so, won't the caller trying to access after this function returns be undefined behaviour?
Can someone point to an authoritative URL explaining this?
[Update] I am thinking of this question Can a local variable's memory be accessed outside its scope?