output of program

Discussion in 'C++' started by Rahul, Apr 11, 2008.

  1. Rahul

    Rahul Guest

    Hi Everyone,

    I have the following program,

    class A{
    private:
    int a;
    public :
    void func1();

    };

    void A::func1(){
    cout <<"hello world";

    }

    int main(){
    A *a1=NULL;
    a1->func1();
    return 0;
    }

    And it prints hello world. I think it is undefined behavior but i'm
    assuming that the memory for the function is allocated as the compiler
    parses through the class definition and hence there should be a way to
    access the memory (function)... Is there any legal way without
    creating an object of the class?

    Thanks in advance ! ! !
    Rahul, Apr 11, 2008
    #1
    1. Advertising

  2. Rahul

    asterisc Guest

    On Apr 11, 10:06 am, Rahul <> wrote:
    > Hi Everyone,
    >
    > I have the following program,
    >
    > class A{
    > private:
    > int a;
    > public :
    > void func1();
    >
    > };
    >
    > void A::func1(){
    > cout <<"hello world";
    >
    > }
    >
    > int main(){
    > A *a1=NULL;
    > a1->func1();
    > return 0;
    >
    > }
    >
    > And it prints hello world. I think it is undefined behavior but i'm
    > assuming that the memory for the function is allocated as the compiler
    > parses through the class definition and hence there should be a way to
    > access the memory (function)... Is there any legal way without
    > creating an object of the class?
    >
    > Thanks in advance ! ! !


    The code should be:
    int main(){
    A a1;
    a1.func1();
    return 0;
    }
    asterisc, Apr 11, 2008
    #2
    1. Advertising

  3. Rahul

    Kai-Uwe Bux Guest

    Rahul wrote:

    > Hi Everyone,
    >
    > I have the following program,
    >
    > class A{
    > private:
    > int a;
    > public :
    > void func1();
    >
    > };
    >
    > void A::func1(){
    > cout <<"hello world";
    >
    > }
    >
    > int main(){
    > A *a1=NULL;
    > a1->func1();
    > return 0;
    > }
    >
    > And it prints hello world. I think it is undefined behavior


    Correct. Dereferencing a null pointer is undefined behavior.


    > but i'm
    > assuming that the memory for the function is allocated as the compiler
    > parses through the class definition and hence there should be a way to
    > access the memory (function)... Is there any legal way without
    > creating an object of the class?


    That may depend on what you mean by "access the memory (function)". First,
    functions in C++ are not objects. That alone makes it tricky to "access the
    memory" that on real machines is associated with a function. In the
    abstract machine, a function is not represented in memory.

    On top of that, _calling_ a non-static member function requires an object.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Apr 11, 2008
    #3
  4. On Apr 11, 12:06 pm, Rahul <> wrote:
    > Hi Everyone,
    >
    > I have the following program,
    >
    > class A{
    > private:
    > int a;
    > public :
    > void func1();
    >
    > };
    >
    > void A::func1(){
    > cout <<"hello world";
    >
    > }
    >
    > int main(){
    > A *a1=NULL;
    > a1->func1();
    > return 0;
    >
    > }
    >
    > And it prints hello world. I think it is undefined behavior but i'm
    > assuming that the memory for the function is allocated as the compiler
    > parses through the class definition and hence there should be a way to
    > access the memory (function)... Is there any legal way without
    > creating an object of the class?
    >
    > Thanks in advance ! ! !


    I am not sure, but compiler passes
    func1(this);

    In your case this == NULL. However since func1(...) is not accessing
    'this', so things worked.
    I do not think such type of code will work, if you are accessing class
    bound memory.

    With Regards,
    Reetesh Mukul
    Reetesh Mukul, Apr 11, 2008
    #4
  5. Rahul

    Ian Collins Guest

    Rahul wrote:
    > Hi Everyone,
    >
    > I have the following program,
    >

    Make that snippet, a program compiles...

    > class A{
    > private:
    > int a;
    > public :
    > void func1();
    >
    > };
    >
    > void A::func1(){
    > cout <<"hello world";
    >
    > }
    >
    > int main(){
    > A *a1=NULL;
    > a1->func1();
    > return 0;
    > }
    >
    > And it prints hello world. I think it is undefined behavior but i'm
    > assuming that the memory for the function is allocated as the compiler
    > parses through the class definition and hence there should be a way to
    > access the memory (function)...
    >

    It is undefined, so what happens is speculation.

    The reason you "get way" with your example is func1() does not access
    any data members of the class. If it did, it would attempt to
    dereference a NULL this pointer.

    > Is there any legal way without creating an object of the class?


    Yes, make func1() a static member function.

    --
    Ian Collins.
    Ian Collins, Apr 11, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Bentley

    Output / Debug window output bug?

    John Bentley, Sep 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    591
    John Bentley
    Sep 10, 2003
  2. chuck amadi
    Replies:
    1
    Views:
    489
    Larry Bates
    Jun 23, 2004
  3. Puneet
    Replies:
    16
    Views:
    1,126
    Daniel Vallstrom
    Mar 20, 2005
  4. Paul
    Replies:
    1
    Views:
    430
  5. jmr
    Replies:
    2
    Views:
    334
Loading...

Share This Page