를 만드는 동안 :사용 다음 프로그램에서 연결리스트에게
// illustration of linked list
#include <iostream>
using namespace std;
struct node {
int data;
struct node* next;
};
struct node* buildList();
int main() {
struct node* head = buildList();
cout << head->data;
}
struct node* buildList() {
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;
head = new node; // builds up a pointer structure on heap
second = new node;
third = new node;
head->data = 1;
head->next = second;
head->data = 2;
second->next = third;
head->data = 3;
third->next = NULL;
return head;
}
내가 여기 new
작업자의 작업의 인식입니다. 그들이 수행하는 업무는 무엇입니까? (나는 heap에 공간을 할당한다는 것을 읽었습니다. 그러나 그것이 의미하는 것이 무엇인지 모르겠습니다.)이 문장을 제거하면 출력이없고 프로그램이 충돌합니다. 왜 그런가요?
1) 이것은 연산자가 아닌 "새로운 * 표현식 *"입니다. 2) 책을 버리십시오 - '새로운 것'은 역동적 인 저장 공간과 수명을 가진 새로운 객체를 만듭니다. 이것이 중요한 것입니다. –