Do you have to delete a function pointer ?

W

wongjoekmeu

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;
}
 
E

Eric Jensen

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
 
J

Jim Langston

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;
}
 
R

Rolf Magnus

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.
 
B

benben

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top