04-18-2005
2,351,
6
Join Date: Dec 2004
Last Activity: 18 February 2020, 9:52 AM EST
Location: UK
Posts: 2,351
Thanks Given: 0
Thanked 6 Times in 6 Posts
Hi,
Not really a C++ guy (had to dig up my C++ programming books), but memory allocation in C++ is done as follows:
class myClass { ..... blah... blah... }; // definition of the class
myClass test1;
myClass test2; /// and so on... you can have as many as your OS will allow
All of the above objects have size that is equal to the data that is present in the object. The functions are not included in the memory allocation to the object; instead the functions are allocated a seperate memory that is internally accessed by C++. So once you declare an object or a pointer to the object, the functions that are part of the class are allocated seperate memory. These functions are accessible as long as there is atleast one instance of the class in the runtime.
So even if you have the example that is given by you, memory has been seperately allocated to the function 'f'. This allows even a pointer initialised to NULL to access the function.
Now, what you can really check for is having some data present in the object and a member function of the class trying to acces the data after you initialise the object pointer to NULL. I don't know what this will do, and would really like it if you can check it and get back.
Hope this has helped.
Cheers!