🔒 Locked
Problem with STL linked list
Advertisement
Original Post
Hi, i'm using vc++ 6 . i created a linked list using the STL list. std::list LPolygon Using the push_back command i stored 6 values in the list. while executing the program i'm getting an unahndled exception i checked in the debug mode the linked list _head value is showing 0xcdcdcdcd and the _Next is showing my first value(that i added) in the list. Please help me to solve this error
It sounds to me you are adding uninitialised pointers to the list.
when debugging stl, never trust intellisense to know whats in your list. Intellisense is like have a blind driving instructor.
I agree with the uninitialized pointer, or you are using a buffer pointer to insert and are deleting the pointer after calling new....which would also delete the memory in your list. If you use new and insert into that list, dont delete till program end, or whenever the list is no longer used.
I agree with the uninitialized pointer, or you are using a buffer pointer to insert and are deleting the pointer after calling new....which would also delete the memory in your list. If you use new and insert into that list, dont delete till program end, or whenever the list is no longer used.
Quote:
Original post by Tang of the Mountain
when debugging stl, never trust intellisense to know whats in your list. Intellisense is like have a blind driving instructor.
As a debugging tool, Intellisense does indeed fail quite miserably...so it is a good thing it isn't a debugging tool at all.
If you are suggesting that you should not rely on the debugger to know what's inside your list, then you're wrong. It isn't always easy to use, but once you get the hang of it it is quite reliable.
CM
Could any one please tell me how to initialize a STL linked list to NULL.
Rasmadrak's code has problems. This line of code (which is your code) will produce an empty linked list:
std::list<CPolygon*> LPolygon; Your problem is probably caused by inserting an uninitialized pointer into the list. Something like this: std::list<CPolygon*> LPolygon; ... CPolygon * p; // note: uninitialized LPolygon.push_back( p );
i've initialised the pointer. I've created a binary tree and stored the values using STL list. But the problem arises when traversing the node. While checking for a node, it is traversing to a memory location (which is not a node) which contains a value 0xcdcdcdcd. the complier assumes this value as true and it shows an "memory could not be read" error
Please read at least some of the following link http://www.microsoft.com/msj/1198/c/c1198.aspx
Now, although the VC6 debugger shows that the list head is 0xCDCDCDCD, the debugger is quite possibly wrong. I have seen this myself sometimes, so believe me, it does happen. If the program appears to be working regardless then it is simply the debugger lying to you. If not, then there could be some other reason, but we'd need to see more code to help furthur.
Another thing worth mentioning is that in most cases you don't need to write code for binary trees in C++. containers such as std::set actually already internally store items in a very smart kind of binary tree for you. All you have to do is define a less-than operator for the types of items in the list, and it does the rest. How cool is that![cool]
If you want help deciding whether you should use a std::set instead of your own binary tree, or how to use a std::set, then let us know and we'll gladly help.
Now, although the VC6 debugger shows that the list head is 0xCDCDCDCD, the debugger is quite possibly wrong. I have seen this myself sometimes, so believe me, it does happen. If the program appears to be working regardless then it is simply the debugger lying to you. If not, then there could be some other reason, but we'd need to see more code to help furthur.
Another thing worth mentioning is that in most cases you don't need to write code for binary trees in C++. containers such as std::set actually already internally store items in a very smart kind of binary tree for you. All you have to do is define a less-than operator for the types of items in the list, and it does the rest. How cool is that![cool]
If you want help deciding whether you should use a std::set instead of your own binary tree, or how to use a std::set, then let us know and we'll gladly help.
Quote:
Original post by saravananps
i've initialised the pointer. I've created a binary tree and stored the values using STL list. But the problem arises when traversing the node. While checking for a node, it is traversing to a memory location (which is not a node) which contains a value 0xcdcdcdcd. the complier assumes this value as true and it shows an "memory could not be read" error
In a debug configuration, the C library initializes "raw" memory allocations to 0xcdcdcdcd. So, if you don't initialize a variable, it will have that value by default. That's why we are saying that your problem is with an uninitialized variable.
Now, despite what some are saying, and while VC6 has a few problems every once in a while, it is very usuable and it is not the cause of your crash. You need to find out why that particular value is uninitialized. It is also possible that the variable was initialized with the value from an uninitialized variable.
You might also show us some relevant code and maybe we could spot the error.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement