简体   繁体   中英

c++ String copying error to struct pointer data fields

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);

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM