Do you have to delete a function pointer ?

Discussion in 'C++' started by wongjoekmeu@yahoo.com, Jun 24, 2006.

  1. Guest

    Hiya all,
    I am trying to use function pointers. I know that usually if you use a
    pointer to an object, you have to release the memory at the end by
    calling the delete keyword. I was wondering if this is the same for
    function pointers. And if yes, how do you do that ? And how do you
    assign it to 0 or Null. Here below I have written a small program. I
    use an array of function pointer. Because the function pointer is
    located in the main() function, I know that when the program exits,
    memory is automatically released. But I what if I use it in another
    function which I call many times where the function pointer is being
    assigned many times. How then should I released the memory ? Many
    thanks in advance.

    robert

    #include <iostream>
    #include <map>
    #include <string>
    using namespace std;

    class test
    {
    public:
    void f()
    {
    cout<<"f function"<<endl;
    }

    void g()
    {
    cout<<"g function"<<endl;
    }
    };

    int main()
    {
    void (test::*fArr[2])();
    test t ;
    map<string, int> fP ;
    fP["f"] = 0;
    fP["g"] = 1;

    fArr[0] = &test::f;
    fArr[1] = &test::g;

    (t.*fArr[fP["f"]])();

    cout<<"Program finish"<<endl;
    return 0;
    }
    , Jun 24, 2006
    #1
    1. Advertising

  2. Eric Jensen Guest

    <> wrote in message
    news:...
    > Hiya all,
    > I am trying to use function pointers. I know that usually if you use a
    > pointer to an object, you have to release the memory at the end by
    > calling the delete keyword. I was wondering if this is the same for
    > function pointers. And if yes, how do you do that ? And how do you
    > assign it to 0 or Null. Here below I have written a small program. I
    > use an array of function pointer. Because the function pointer is
    > located in the main() function, I know that when the program exits,
    > memory is automatically released. But I what if I use it in another
    > function which I call many times where the function pointer is being
    > assigned many times. How then should I released the memory ? Many
    > thanks in advance.


    You should not delete function pointers. The function is located somewhere
    inside your executable. When you execute your program, the OS will load the
    executable into memory. When your program assigns a value to the function
    pointer, it will get the value that points to the location of the function
    in your executable. If you delete it in your program, your compiler will
    most likely complain about you're trying to delete a object that is not a
    pointer.

    You could try to delete a function pointer and see what will happen:

    int add(int a, int b) {
    return (a+b);
    }

    int main(int argc, char* argv[]) {
    int (*fp)(int,int);
    fp = &add;
    std::cout << fp(10,10) << std::endl;
    delete fp; // your compiler might throw an error here
    return 0;
    }

    //eric
    Eric Jensen, Jun 24, 2006
    #2
    1. Advertising

  3. Jim Langston Guest

    <> wrote in message
    news:...
    > Hiya all,
    > I am trying to use function pointers. I know that usually if you use a
    > pointer to an object, you have to release the memory at the end by
    > calling the delete keyword. I was wondering if this is the same for
    > function pointers.


    No. If you use a pointer to an object, you don't always needed to delete
    it, only if you used new on it. Maybe an example would help:

    int a;
    int* intp = a;

    intp is an integer pointer that you should not delete, because it is simply
    pointing to another object (in this case the interger a). Since you didn't
    use new, you don't use delete. Same with function pointers. You don't use
    new to declare the memory of the function, you just make the pointer point
    to it, so you don't have to delete it.

    > And if yes, how do you do that ?


    You don't.

    > And how do you assign it to 0 or Null.


    Same as any pointer.
    MyFuncPointer = NULL;

    > Here below I have written a small program. I
    > use an array of function pointer. Because the function pointer is
    > located in the main() function, I know that when the program exits,
    > memory is automatically released. But I what if I use it in another
    > function which I call many times where the function pointer is being
    > assigned many times. How then should I released the memory ? Many
    > thanks in advance.


    Like said above, you don't have to. The memory for the functions will be
    handled by the OS when it cleans up the class (functions actually exist in
    the executable files and just get copied to memory, they aren't actually
    released individually, they just go away when the program memory gets
    released).

    > robert
    >
    > #include <iostream>
    > #include <map>
    > #include <string>
    > using namespace std;
    >
    > class test
    > {
    > public:
    > void f()
    > {
    > cout<<"f function"<<endl;
    > }
    >
    > void g()
    > {
    > cout<<"g function"<<endl;
    > }
    > };
    >
    > int main()
    > {
    > void (test::*fArr[2])();
    > test t ;
    > map<string, int> fP ;
    > fP["f"] = 0;
    > fP["g"] = 1;
    >
    > fArr[0] = &test::f;
    > fArr[1] = &test::g;
    >
    > (t.*fArr[fP["f"]])();
    >
    > cout<<"Program finish"<<endl;
    > return 0;
    > }
    Jim Langston, Jun 24, 2006
    #3
  4. Rolf Magnus Guest

    wrote:

    > Hiya all,
    > I am trying to use function pointers. I know that usually if you use a
    > pointer to an object, you have to release the memory at the end by
    > calling the delete keyword.


    That's wrong. You only (and only ever!) must delete objects that were
    created with new.

    > I was wondering if this is the same for function pointers. And if yes, how
    > do you do that ?


    Since functions are not objects and can't be created with new, you must
    never use delete on them. I'm not sure, but I think your compiler should
    give you an error if you try.

    > And how do you assign it to 0 or Null.


    If p is a function pointer, write:

    p = 0;

    > Here below I have written a small program. I use an array of function
    > pointer. Because the function pointer is located in the main() function, I
    > know that when the program exits, memory is automatically released. But I
    > what if I use it in another function which I call many times where the
    > function pointer is being assigned many times. How then should I released
    > the memory ? Many thanks in advance.


    If you just define a pointer, that does not allocate any memory at all,
    except for the pointer itself. No object is created in the background. You
    let pointers point to objects that already exists. If you create an object
    dynamically (i.e. with new or malloc or consorts), you get the address of a
    new object that you then write into a pointer. Later, you have to delete
    it, but not because you had a pointer to it, but because it was created
    with new.
    Rolf Magnus, Jun 24, 2006
    #4
  5. benben Guest

    > No. If you use a pointer to an object, you don't always needed to delete
    > it, only if you used new on it. Maybe an example would help:
    >
    > int a;
    > int* intp = a;


    Perhaps you really meant:

    int* intp = &a;

    Regards,
    Ben
    benben, Jun 24, 2006
    #5
  6. Jim Langston Guest

    "benben" <> wrote in message
    news:449d25c2$0$14512$...
    >> No. If you use a pointer to an object, you don't always needed to delete
    >> it, only if you used new on it. Maybe an example would help:
    >>
    >> int a;
    >> int* intp = a;

    >
    > Perhaps you really meant:
    >
    > int* intp = &a;
    >
    > Regards,
    > Ben


    Absolutely. Nice catch.
    Jim Langston, Jun 24, 2006
    #6
    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. Roman Werpachowski

    Why can you delete a const pointer?

    Roman Werpachowski, Dec 28, 2005, in forum: C++
    Replies:
    21
    Views:
    675
  2. morz
    Replies:
    7
    Views:
    437
    Marco Wahl
    Feb 6, 2006
  3. asimorio
    Replies:
    11
    Views:
    708
    Tomás
    May 31, 2006
  4. Protoman
    Replies:
    6
    Views:
    334
    Protoman
    Jul 5, 2007
  5. shoplifes
    Replies:
    0
    Views:
    300
    shoplifes
    Nov 25, 2007
Loading...

Share This Page