0

I'm currently writing a program that uses a binary search tree to store names and phone numbers (phonebook basically). I've done this before with an AVL tree, and it works fine. I have decided to switch my methods up for this implementation and not just copy/paste the logic and format of the last one. In doing so I've run into a weird error and I have no idea why it's happening. At first I thought my problem was in the way I returned a struct pointer, but it is actually in my string copying.

I've written a very basic program that only shows the copy function that returns the structure, which are then used to recursively fill the BST with the data (which is read in from a file).

Here is the shortened example:

#include <iostream>
#include <string>

using namespace std;

struct Node
{
    std::string first;
    std::string last;
    std::string phone;
};

Node* copyfunc(std::string first, std::string last, std::string phone)
{
    Node* temp = NULL;

    temp->first = first;
    temp->last = last;
    temp->phone = phone;

    return temp;
}

int main()
{
    std::string first, last, phone;
    first = "Jenny";
    last = "Something";
    phone = "8675309";

    Node* newStruct = NULL;

    newStruct = copyfunc(first, last, phone);

    cout << newStruct->first << endl;
    cout << newStruct->last << endl;
    cout << newStruct->phone << endl;

    cout << "Never to be seen again..." << endl;

    return 0;
}

Now, I've tried using VS2013 debugger to find out where the issue is, and it happens on the first copy: "temp->first = first;". It breaks with an access violation warning and then opens the xstrings (header?) and points to the section: (line 2245)

if (this->_Myres < _Newsize)
    _Copy(_Newsize, this->_Mysize); // reallocate to grow"

I'm just guessing, but from what I can gather it seems to me that it's failing in creating the new string to fit the old strings length.

The program (both the example and real one) will compile, they just hang at the point of reaching the copy function.

All input is appreciated, thanks!

EDIT: The reason I use pointers for my structures is due to the way the algorithms I'm using are written. The functions that actually link the nodes together in the BST accept a Node* type rather than a Node object. Ex: recursiveInsert(Node* root, Node* newNodeToAdd);

1 Answer 1

2

You are not initializing temp to anything useful before you attempt to use it.

Node* temp = NULL;

temp->first = first; // oops! temp is NULL!

It would be easier to drop the pointers entirely:

Node copyfunc(std::string first, std::string last, std::string phone)
{
    Node temp = {first, last, phone};
    return temp;
}

You should also consider passing the parameters by const reference instead of value. Or just drop the function completely and initialize the Node where needed:

Node newStruct = {first, last, phone};
cout << newStruct.first << endl;
Sign up to request clarification or add additional context in comments.

4 Comments

The issue with dropping the pointer is that I need to pass a Node* type to the recursive function that starts at the root of the BST and just links the new node where needed. Also, the function that reads in the data and the recursive function are inside of a class, one public and one private. I could do that, but I'd have to rewrite/restructure a lot of my existing code.
@user3857017 Well, there's nothing in your post to suggest you need to use pointers.
Fair, I guess I shouldn't assume that is known. The algorithm(s) I'm using for the BST are from a book of mine that implements the BST using a lot of pointers for adding, deleting, etc. Rather than just doing so by explicit assignments as data is read in. I'll edit the OP to reflect the "need" for pointers.
Actually, it wasn't as much rewriting as I thought it would be. Thanks for the solid answer and explanation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.