Pointers without allocation

This is a discussion on Pointers without allocation within the C++ Programming forums, part of the General Programming Boards category; Hello, Code: class foo{ public: void fun(){ /* do something */ }; }; int main(){ foo *pF; pF->fun(); return 0; ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    16

    Pointers without allocation

    Hello,
    Code:
    class foo{
    public:
    void fun(){ /* do something */ };
    };
    
    int main(){
    
    foo *pF;
    pF->fun();
    
    return 0;
    }
    How is fun() called when nothing has been allocated to the pointer? In c++ classes we've been taught that we need to allocate memory with the 'new' keyword, so how is this legal c++? I'm just finding out about this and I don't know what it's called, so I can't search for it. I don't remember reading anything about it in the books. Does this not cause data loss when nothing has been assigned to pF? what is its use?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    The code is syntactically valid, but it is wrong to call a member function of an object that does not exist.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    so how is this legal c++?
    It's not. Just because something compiles doesn't mean it's legal.

    Also, turn up the warning level on your compiler - you definitely should get a stern warning for this code about using uninitialized variables.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    If the function does not access member data, then it will probably run (though it's still undefined). The problem is that typically functions and data are separate. Functions recieve and extra argument (the this pointer), which points to where the class data exists. So if you call a function in an unitialized pointer, the this pointer sent to the function will be garabage.

    So just because you can away with it doesn't mean you should. It's definitely not legal.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    5
    Code:
    class foo{
    public:
    void fun(){ /* do something */ };
    };
    
    int main(){
    
    foo *pF;
    pF->fun();
    
    return 0;
    }
    The code looks ok to me. Doesn't the compiler know that it's creating a pointer to a class and not a basic variable? Then you just call the member function fun() as a pointer.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    No, because the pointer doesn't point to a valid memory block with allocated memory.
    You need to read up on pointers, my friend. They aren't magical. They don't somehow, magically allocate memory for you and point to a valid address.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    5
    So why does the code work?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    As I explained. First, it's undefined behavior.
    Second, it may work if it doesn't access member data because functions are not stored at the this pointer, but member data is.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Quote Originally Posted by Caverage View Post
    So why does the code work?
    Because the way it is implemented by the compiler is the same as if it were a non-member function with a hidden parameter called 'this'.
    I.e. Instead of:
    Code:
    class foo{
    public:
    void fun(){ /* do something */ };
    };
    think of it as:
    Code:
    class foo{
    };
    void fun(foo *this){ /* do something */ };
    except that you call it like this:
    Code:
    pF->fun();
    instead of this:
    Code:
    fun(pF);
    The later would not crash because fun doesn't do anything with the 'this' pointer. The same reasoning applies to the original code, because the differences between the two are usually just superficial.

    That's not to say that this is how ALL compilers implement member functions.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what iMalc is saying (and I agree) is that the compiler can [most of the time, depending on the design of the compiler] figure out where fun() is without accessing the the pointer itself. Accessing the pointer in this case would MOST likely cause a crash, but calling a function that doesn't do anything within the object that "this" points to, then you'd be OK. Of course, most of the meaningful member functions in a class object will need to access the content of the object. So this is only going to work for member functions that doesn't actually use the content of the class [and subject to the implementation of the compiler].

    Of course, accessing an uninitialized pointer isn't GUARANTEED to crash things. The system may not have memory protection in a way that it causes a crash when a "random" address is accessed [e.g. DOS or DOS-emulation in Windows], and it may be that the memory address that happens to be in the pointer is actually a valid memory location (just not given to your object, so therefore not valid for this purpose).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    16
    My apologies everyone. I've been talking to some Qt programmers, and I've realized that I posted a really bad example. I'm reading a book on programming with Qt4, and I was trying to understand a function call in one of their examples. Here it is:
    Code:
    void MainWindow::closeEvent(QCloseEvent *event)
    {
        if (okToContinue()) {
            writeSettings();
            event->accept();
        } else {
            event->ignore();
        }
    }
    MainWindow is my class, which is derived from QMainWindow. QMainWindow is derived from QWidget. closeEvent is a virtual function in QWidget, and I never have to call it directly cause it gets called automatically. The Qt guys told me that the event parameter has already been prepared. So I guess everything makes sense now?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  2. Hey guys..need help on pointers
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2008, 02:57 PM
  3. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  4. Dynamic allocation for array of pointers to char
    By bivhitscar in forum C Programming
    Replies: 7
    Last Post: 05-20-2006, 07:04 AM
  5. New to pointers - allocation problem
    By AngKar in forum C Programming
    Replies: 9
    Last Post: 04-24-2006, 05:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21