I am importing some C code into a C++ compiler, specifically Borland C++ Builder 5. I seem to have a problem with this style of code:
// A structure that contain's itself
typedef struct AnObject AnObject;
struct AnObject {
AnObject *Object;
};
// A global structure to store a pointer of the AnObject structure
typedef struct {
AnObject *Page;
} _Stock;
_Stock Stock;
// A function that returns an instance of AnObject
AnObject *CreateInstance() {
return malloc(sizeof(AnObject));
}
// A function that copies the generated instance to the global pointer
void somefunct() {
// The pointer will start off NULL
Stock.Page = NULL;
// The pointer should now receive a non-NULL pointer
Stock.Page = CreateInstance();
}
In GCC, the Stock.Page pointer will be non-NULL at the end of the somerfunct() function. This is not the case for Borland C++ Builder 5. In that case Stock.Page continues to be NULL.
On the other hand, if I change somefunct() to this...
void somefunct() {
// The pointer will start off NULL
Stock.Page = NULL;
// Borland C++ Builder 5 likes this syntax. TempPage is a non-NULL.
AnObject *TempPage = CreateInstance();
// But I still cannot copy the pointer from TempPage.
Stock.Page = TempPage;
// Stock.Page is still NULL at this point!
}
Since I am better versed in the C language, I am thinking that this is some kind of incompatibility with the language itself, and that I need to do this more C++ style. On the other hand, the compiler I am using is quite old and this could be some kind of bug. Does anyone here know what I might be doing wrong?