Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Do you have to delete a function pointer ?

Reply
Thread Tools

Do you have to delete a function pointer ?

 
 
wongjoekmeu@yahoo.com
Guest
Posts: n/a
 
      06-24-2006
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;
}

 
Reply With Quote
 
 
 
 
Eric Jensen
Guest
Posts: n/a
 
      06-24-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      06-24-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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;
> }



 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-24-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      06-24-2006
> 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
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      06-24-2006

"benben" <(E-Mail Removed)> wrote in message
news:449d25c2$0$14512$(E-Mail Removed) 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE;Kontki if you delete kontiki any program you loaded with it in it 'will not work I have tried it with three programs and none work anymore (if you se it just stop download) 1-Twitch Computer Support 5 04-23-2009 02:45 PM
diffrence between "function pointer" and "pointer to a function" murgan C Programming 6 12-21-2005 06:01 AM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
function pointer and member function pointer question glen stark C++ 2 10-10-2003 01:41 PM



Advertisments