0

I am trying to delete the following map:

typedef bool (myClass::*func)(std::vector<std::string> &args); typedef std::map<std::string, func> myMap; myMap map; //map of functions in the class 'myClass' void deleteMap() { for(myMap::iterator it = map.begin(); it != map.end(); ++it) { delete it->second; //compiler says it->second is non-pointer type map.erase(it); } } 

'map' is mapping a string to a function in the class 'myClass', and this function takes a vector of strings in its parameter.

In my attempt to delete this map I am trying to delete the pointer to the member function, then erase the iterator itself. The compiler says that it->second must be a pointer type. In the typdef 'func' is a pointer to myClass:: so why am I getting this error?

Is this the appropriate way to delete a map of functions?

7
  • 1
    You did new the member functions? Commented Jul 26, 2017 at 21:48
  • 2
    Why do you want to call delete on the pointers? Commented Jul 26, 2017 at 21:48
  • It appears that the correct way to delete that map is to exit the function. Commented Jul 26, 2017 at 21:49
  • 1
    Even if there was no compilation error, the shown code is buggy, and is guaranteed to result in a crash due to undefined behavior after incrementing a deleted iterator. Commented Jul 26, 2017 at 21:57
  • 2
    You are trying to delete the memory that represents a member function of myClass. Just take a short moment to think about the implications of this! delete only works for memory that has been dynamically allocated on the heap, (member) functions are all in the static read-only part of memory. Simply never try to delete a function pointer. Emptying the map using erase or clear should be enough Commented Jul 26, 2017 at 21:57

4 Answers 4

5

You misunderstand some concepts here. You use delete/delete[] to release memory which has been allocated by new/new[], respectively.

Here, you have a std::map that stores pointer-to-member-function values. There is no memory allocated on the heap with new, so you do not have to free the memory using delete at all.

In addition to that, you do not need to use std::map::erase() for every element. Instead, you might use either std::map::clear() to clear the std::map, or just let the destructor of std::map release the contents automatically.

Sign up to request clarification or add additional context in comments.

Comments

2

You cannot delete a member pointer, because it is part of a class instance.

You haven't posted code where you add an element to myMap, but such code cannot create the member pointer with a new on something of type func which indicates that the delete is wrong, because every delete must be matched by a new.

You can simply call myMap.clear() to delete myMap's content.

Comments

1

In my attempt to delete this map I am trying to delete the pointer to the member function,

That is not allowed by the language. You can call delete on an object pointer only.

Think of this.

int (*fptr)(const char *lhs, const char *rhs ) = &std::strcmp; // Does it make any sense at all? // What would you expect the run time to do with this? delete fptr; 

Your function can be very simple. You can just clear the map.

void deleteMap() { map.clear(); } 

Comments

1

Sometimes pointer might refer to different kinds of pointers as a whole but often, such as in this case in particular, it means specifically a data pointer. Your pointer is a member function pointer and as such it is not such pointer type that your compiler is referring to.

Only data pointers may be passed to the delete operator, otherwise the program is ill-formed. More specifically, you may only pass a pointer value that was returned by the new operator. In other words, delete is used to deallocate dynamic memory that you have allocated with new.

new operator can never return a (member) function pointer, so your function pointers could not possibly have been returned by new.

Is this the appropriate way to delete a map of functions?

No. You cannot "delete" the functions that the map points to.

map::erase removes the function pointer from the map, but incrementing that iterator after having it erased has undefined behaviour. So, erasing in a loop can not be achieved as simply as you've attempted.

If you want to remove all pointers, it is simpler to just call map::clear. On the third hand, all of the contents are automatically destroyed when the map itself is destroyed.

Objects with static storage duration, such as your map, are destroyed automatically at the end of the program, and you cannot delete them yourself.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.