2014-03-25 65 views
1

我编程双向链表,一切都进行得很顺利,但在阅读的字符串值的结构,当我面临崩溃(代码行的功能评论说:“结构节点* 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 TRUE $ ASDF
6 7.3 TRUE#QWER
9 8.8 FALSE @ zxvc
7 6.35假的! vbmn
1 0.89 TRUE%ghjk

有人可以解释这里有什么问题吗?

+0

仅供参考,'Initialize()'没用。你设置了一个局部指针'stack'作为全局'head'的值(当它触发时它是NULL,但这不相关)。你传递的参数是不变的,因为它是通过值*传递的。 – WhozCraig

回答

4

而不是使用标准C函数的malloc

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

,你必须使用operator new

在这种情况下,编译器会调用std::string类的构造函数来创建数据成员str的 Othewise对象海峡将不会创建类型std::string,并且程序将发生冲突。

功能malloc简单地分配请求大小的原始内存。它对类的构造器一无所知。

1

malloc分配存储原始块。这对于仅存储数据的简单(POD)数据类型来说已经足够了。然而,std::string需要将其构造函数调用为正确初始化。因此,你必须分配使用new节点:

Node* newNode = new Node(); 

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

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

相关问题