Welcome Guest [Log In] [Register]
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:

Username:   Password:
Add Reply
segfault in std::string::size() [SOLVED]
Topic Started: Nov 13 2013, 12:06 PM (123 Views)
Deleted User
Deleted User

The error occurs when I am doing the first insert into my BSTree. The function call in main is:
Code:
 
BinarySearchTree<string, int> bst;
bst.insert("test", 1);


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?
Quote Post Goto Top
 
Deleted User
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.
Quote Post Goto Top
 
Deleted User
Deleted User

jyun007
Nov 13 2013, 12:20 PM
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.
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?
Quote Post Goto Top
 
Deleted User
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.
Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Homework 6 · Next Topic »
Add Reply