473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Do you have to delete a function pointer ?

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
5 17237

<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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Sandeep Grover | last post by:
Hi, If I do another delete on an object which has been deleted earlier, then how is the system expected to behave ? Is it an unpredictable behavior ?? Thanks Sandeep
14
by: A | last post by:
Hi, Consider this: char* ptr = "a string"; Is ptr a pointer to a dynamically created object for which i must use the delete operator to deallocate memory? I'm sure the "a string" part...
10
by: MaxMax | last post by:
int *p; p = new int; void *q = (void*)p; delete q; Is this valid if p is guaranteed to be a POD? (don't ask why... there is a reason I need this. Clearly I will create p in function, use p...
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
6
by: Ananas | last post by:
Hi, My native C++ function creates a dynamic array. I'm marshalling it to managed code and got to delete after. How to make it: c++ code: void CreateArrayInside( pTestStruct &TestStruct,...
31
by: Anamika | last post by:
Hello friends.... can anyone tell me what will happen when we do..."delete this"...
9
by: Christopher | last post by:
If I call delete on a pointer is it guarenteed to set that pointer to NULL on all implementations? I've also seen "delete this" in a reference counting pointer implementation and am wondering...
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
4
by: mail.dsp | last post by:
Suppose in a class we overload four operators: operator new operator delete operator new operator delete class Test{ public: void * operator new (size_t t){ cout<<"\nCalling... new";
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.