Watch elements of a list in debugger

How can i look into the element that are pushed into the list.
Something like listPointer[0].heapPointer,50

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
class CA{
public:
	CA(void);
	CA(int heapObjectLength);
	~CA(void);

private:
	int *heapPointer;
	int b;
};

CA::CA(int heapObjectLength){
	this->b = 22;
	this->heapPointer = new int[heapObjectLength];

	for (int i = 0; i < heapObjectLength; i++) {
		this->heapPointer[i] = i+10;
	}
}

CA::~CA(void){
	delete []heapPointer;
}

void putList(list<CA> *listPointer);

int main(void) {

	list<CA> *listPointer = new list<CA>;
	putList(listPointer);
	delete listPointer;

	return 0;
}

void putList(list<CA> *listPointer){
	CA aObject(50);
	listPointer->push_front(aObject);

}
> list<CA> *listPointer = new list<CA>;
quite likely, unnecessary dynamic allocation.
So I'll assume list<CA> listPointer

Using gdb here are some commands to visualize stl containers http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt
analyzing the part for std::list, it would be print *(CA*)(listPointer._M_impl._M_node->_M_next) to show the first element
then to print the array you can use the print *starting_address@num_elems form, so
print *((CA*)(listPointer._M_impl._M_node->_M_next))->heapPointer@50


Note: when you do the plist listPointer CA 0 you may receive as output
elem[0]: $1 = {
  heapPointer = 0x602010,
  b = 22
}
List size = 2
then you can access the first element by referring to $1, so you could print the array with print $1.heapPointer[0]@50


Regarding your code, you are missing a proper copy constructor and assignment operator.
When doing listPointer->push_front(aObject); a copy is created and destroyed, so your heapPointer is already invalid
Last edited on
1.Why do you think that listPointer is unnecessary as heap allocation?is it cause the delete call is near the end of main?

2.the heapPointer is invalid cause destructor is callingdelete []heapPointer; if im deleting the call the heapPointer seems valid. can you explain more detail
> Why do you think that listPointer is unnecessary as heap allocation?
you know at compile time the type of the object that you want to create.
you don't pretend to use polymorphism (there are no virtual functions in std::list)
it doesn't "survive" its scope.
So it would be equivalent to std::list<CA> listPointer;
Bjarne Stroustrup wrote:
Code that creates an object using new and then deletes it at the end of the same scope is ugly, error-prone, and inefficient.
¿what is your reason to use dynamic allocation?


2. As I said, you are missing a proper copy/move constructor.
In your function `putList()' you create `aObject', then you try to push_front() it, which would either move or copy it (same thing in your case)

When the function ends, aObject is destroyed. Its destructor is invoked and delete[] heapPointer executed.
the problem is that the copy that were in the list has the same value for `heapPointer'. It was pointing to the same location, so now it becomes invalid.

To solve this you need to provide an adequate copy/move constructor.
An easy way std::vector<int> heapPointer;
Topic archived. No new replies allowed.