| Welcome to UCR CS 14 Klefstad. You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free. Join our community! If you're already a member please log in to your account to access all of our features: |
| segfault in std::string::size() [SOLVED] | |
|---|---|
| Tweet Topic Started: Nov 13 2013, 12:06 PM (123 Views) | |
| Deleted User | Nov 13 2013, 12:06 PM Post #1 |
|
Deleted User
|
The error occurs when I am doing the first insert into my BSTree. The function call in main is:
In the line while(t -> key != key) of TreeNode::insert, in std::operator!=<char, std::char_traits<char>, std::allocator<char> >, in std::operator==<char>, and finally in std::string::size(), my program seg faults. gdb output: Spoiler: click to toggle The backtrace from gdb: Spoiler: click to toggle There is guaranteed to be at least one node in the tree so it couldn't be trying to call size() on something like 'NULL -> key', could it? Any suggestions? |
|
|
| Deleted User | Nov 13 2013, 12:20 PM Post #2 |
|
Deleted User
|
while (t->key != key)? Does this mean you keep having t point to its left or right nodes until it finds one that has a matching key? If you do, that will cause it to segfault. |
|
|
| Deleted User | Nov 13 2013, 02:55 PM Post #3 |
|
Deleted User
|
No, I'm not doing any finding in my insert function since we don't need to handle duplicates. The problem is not with any of the code inside the while loop. My program crashes when checking the while condition; it never actually fully evaluates it because it segfaults. Unless I'm reading the backtrace from gdb incorrectly, the operator!= function calls operator==<char>, which in turn calls std::string::size(). That is as far as my program gets. Why exactly does operator==<char> need to call size()? Or, is the size() call from operator!= to check if t -> key and key are the same size or not? |
|
|
| Deleted User | Nov 13 2013, 03:17 PM Post #4 |
|
Deleted User
|
I hadn't overwritten a default constructor for the BinarySearchTree class because I thought that the default one just initializes any pointers to NULL. Either I'm mistaken in that belief or for some reason no default constructor was created, however, writing a simple BSTree():root(NULL){} solved the problem. |
|
|
| 1 user reading this topic (1 Guest and 0 Anonymous) | |
| « Previous Topic · Homework 6 · Next Topic » |





12:16 PM Jul 11