【问题标题】:Program crashes because of string?程序因为字符串而崩溃?
【发布时间】:2014-05-04 01:45:23
【问题描述】:

我正在编写双向链表,一切正常,但在将字符串值读取到结构时遇到了崩溃(代码行在函数“struct Node* GetNewNode()”中注释):

        #include <iostream>
        #include <String>
        #include <fstream>
        #include <cstdlib>

        using namespace std;

        struct Node {
            int sv;
            double real;
            bool log;
            char simb;
            string str;
            struct Node* next;
            struct Node* prev;
        };

        struct Node* head; // global variable - pointer to head node.
        //----------------------------
        struct Node* GetNewNode();
        void Initialize(Node *stack);
        void InsertAtTail(Node *stack);
        void Print(Node *stack);
        //----------------------------
        //Creates a new Node and returns pointer to it.

        ifstream fd("duom.txt");
        struct Node* GetNewNode() {
            struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
            fd >> newNode->sv;
            fd >> newNode->real;
            string loginis;
            fd >> loginis;
            if (loginis == "TRUE")
                newNode->log = true;
            else
                newNode->log = false;
            fd >> newNode->simb;
            //fd >> newNode->str;           //uncommented code in this row crashes the program
            newNode->prev = NULL;
            newNode->next = NULL;
            return newNode;
        }

        //Inserts a Node at head of doubly linked list
        void Initialize(Node *stack) {
            stack = head;
        }

        //Inserts a Node at tail of Doubly linked list
        void InsertAtTail(Node *stack) {
            struct Node* temp = stack;
            struct Node* newNode = GetNewNode();
            if(head == NULL) {
                head = newNode;
                return;
            }
            while(temp->next != NULL)
                temp = temp->next; // Go To last Node
            temp->next = newNode;
            newNode->prev = temp;
        }

        //Prints all elements in linked list in reverse traversal order.
        void Print(Node *stack) {
            struct Node* temp = stack;
            if(temp == NULL)
                return; // empty list, exit
            // Going to last Node
            while(temp->next != NULL)
                temp = temp->next;
            // Traversing backward using prev pointer
            while(temp != NULL) {
                cout << temp->sv << " ";
                cout << temp->real << " ";
                if (temp->log == true)
                    cout << "TRUE " << " ";
                else
                    cout << "FALSE " << " ";
                cout << temp->simb << " ";
                //cout << temp->str << "\n";
                temp = temp->prev;
            }
            printf("\n");
        }

        int main() {

            /*Driver code to test the implementation*/
            head = NULL; // empty list. set head as NULL.
            // Calling an Insert and printing list both in forward as well as reverse direction.
            Initialize(head);
            InsertAtTail(head);
            Print(head);
            InsertAtTail(head);
            Print(head);
            fd.close();
        }

输入数据是:
4 5.27 真 $ asdf
6 7.3 是的 #qwer
9 8.8 错误@zxvc
7 6.35 错误! vbmn
1 0.89 真 % ghjk

有人能解释一下这里出了什么问题吗?

【问题讨论】:

  • 仅供参考,Initialize() 没用。您正在将本地指针 stack 设置为全局 head 的值(触发时为 NULL,但这不相关)。你传入的参数没有改变,因为它是按值传递的

标签: c++ string pointers constructor malloc


【解决方案1】:

而不是使用C标准函数malloc

      struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

你必须使用operator new

在这种情况下,编译器将调用类std::string的构造函数来创建数据成员str 否则 std::string 类型的对象 str 将不会被创建并且程序将 chash。

函数malloc 只是分配请求大小的原始内存。它对类的构造函数一无所知。

【讨论】:

    【解决方案2】:

    malloc 分配一个原始内存块。这对于仅存储数据的简单 (POD) 数据类型就足够了。但是,std::string 需要调用其构造函数才能正确初始化。因此你必须使用new分配节点:

    Node* newNode = new Node();
    

    一般来说,malloc 在 C++ 中很少需要(它不调用任何构造函数)。它是一个 C 函数。

    请注意,您需要调用delete 而不是free 来释放new 分配的内存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      相关资源
      最近更新 更多