C++ Puzzled !!


 
Thread Tools Search this Thread
Top Forums Programming C++ Puzzled !!
# 1  
Old 04-16-2005
Error C++ Puzzled !!

Code:
#include <iostream.h>
class A
{
  public:
      void f(void)
      {
           cout << "hello world \n" ;
      }
};
 
void main()
{
 
   A *a;
 
   a = 0 ;
 
   a->f(); // OOPs...Am I mad?  What am I going to do ?
}

I ran the above mentioned program in Solaris 5.8 & gcc version 2.95.3 20010315 (release)

It prints Hello world
Is this possible/allowed!!!! Smilie ????
# 2  
Old 04-18-2005
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Programming

Serioulsy puzzled here.

Facebook had a mathematics problem which was as thus:- 6/2(1+2) = ? Answer is 9. My ancient Casio FX 730P mini computer written exactly as that gives 'error' only. Now take a look at shell versions, and a python version:- Last login: Wed Sep 14 18:04:04 on ttys000 AMIGA:barrywalker~>... (6 Replies)
Discussion started by: wisecracker
6 Replies

2. OS X (Apple)

Puzzled by Find

I'm new to playing with the command line on OS X and am puzzled by the response I am getting from the find command. I have a file structure similar to the following /Volumes/ ../Drobo/ ../../Pictures/ ../../../Image 1/ ../../../../Image 1.jpg ../../../../Previews/ ../../../../../Image... (2 Replies)
Discussion started by: Denrael
2 Replies

3. Shell Programming and Scripting

Puzzled with hexdump, hd and ln

How to create a symbolic link to a command with certain argument? When I man hexdump, it is said in the man page that "-C Canonical hex+ASCII display...Calling the command hd implies this option". Actually it is. hd equals to hexdump -C. And then I examined the ln command but find it is a... (5 Replies)
Discussion started by: vistastar
5 Replies

4. Solaris

Puzzled over over the relationship between the partition and geometry of hard disk.

Not sure why solaris couldn't detect the geometry of a hard disk which has a working OS of winxp pro. Is it due to the different OS that the partition information is stored in different location? When I type '"format" it is shown as below, c3d1 < drive type unknown>... (5 Replies)
Discussion started by: just.srad
5 Replies

5. Solaris

puzzled with VxVM and iostat..

Hi all, One disk on my root disk group failed in Veritas Volume manager. I replaced it with new one, initialized it and placed it with removed one. it Synchronized plexes and everything is fine. this node was second standby node of Sun cluster. yesterday I had failure on active node with boot... (1 Reply)
Discussion started by: samar
1 Replies

6. Programming

Puzzled with user ID.

I hava been reading AUPE these days. I really am puzzled with the presentation of real user(group) ID, effective user(group) ID. How do they effect on the execution of process? What's the relationship between them? Appreciate your help. (4 Replies)
Discussion started by: lethefe
4 Replies
Login or Register to Ask a Question