By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,330 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,330 IT Pros & Developers. It's quick & easy.

Do you have to delete a function pointer ?

P: n/a
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 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

<wo*********@yahoo.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
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
Jun 24 '06 #2

P: n/a

<wo*********@yahoo.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
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;
}

Jun 24 '06 #3

P: n/a
wo*********@yahoo.com 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.

Jun 24 '06 #4

P: n/a
> 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
Jun 24 '06 #5

P: n/a

"benben" <be******@yahoo.com.au> wrote in message
news:44***********************@news.optusnet.com.a u...
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.
Jun 24 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.