Dynamic allocation check

I have this issue for which I haven't been able to find an answer through Bing or Google. My RobotExtension class's constructor takes a container of pointers to append to its own member container. However, the derived class dynamically allocates a default set of pointers in the member container.
This means that I am deallocating memory in the destructor, but what if the pointers passed to the constructor were not pointing to dynamically allocated objects? Is there a way to check if what a pointer is pointing to is on the stack or heap?

Code where this issue arises:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// deallocation occurs on line 25 | allocation occurs on line 40
/* Clarification:
   RobotPart is a separate class
   PartsList__ is a list<RobotPart*> member object
   Link__ is a list<RobotPart::Connection_t> object
      Connection_t is an enum nested in RobotPart
  __Connection is of type Connection_t and is a member variable of RobotPart
*/
RobotExtensions::RobotExtensions(const list<RobotPart*>& newparts){
	PartsList__.resize(Partslist__.size() + newparts.size());
	for(auto part___ = newparts.begin(); part__ != newparts.end(); part___++){
      //Check if there is an available connection
		if(Link__.empty())	break;
		for(auto alink__ = Link__.begin(); alink__ != Link__.end(); alink++){
			if((*alink) == (*part__)->__Connection){
				PartsList__.pushback(*part___);
				Link__.erase(alink);
				break;
			}
		}
	}
}
RobotExtensions::~RobotExtensions(){
	for(auto part__ = PartsList__.begin(); part__ != PartsList__.end(); part__++)
		delete (*part);
}

/******************Derived Class**********************/
/* Clarification
   Frame is a derived class of RobotExtensions
   RE_Name is a member string object of RobotExtensions
   SmartBar is a derived class of RobotPart
   Male_Female and Snap are enumerations of Connection_t
*/
Frame::Frame(const list<RobotPart*>& newparts):RobotExtensions(newparts){CreateDefault();}
void Frame::CreateDefault(){
	const int minimum_bars_need(10);
	RE_Name__ = "Frame";
	for(int i = 0; i < minimum_bars_need; i++){
		PartsList__.push_back(new SmartBar());
		Link__.push_back(Male_Female);
	}
	for(int i = 0; i < minimum_bars_need/2; i++)
		Link__.push_back(Snap);
}
Last edited on
There is no portable way to do that. I would use managed pointers (shared_prt for example) to make sure that deleting it won't break anything else.
Alright, thanks.
Topic archived. No new replies allowed.