2014-03-25 96 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分配的內存。

相關問題