为链表中的节点分配内存,没想到链表中的下一个节点也被分配了
allocate memory for node in linked list but unexpectedly, the next node inside it is also allocated
我正在尝试实现一个链表,但是当我为一个音符分配内存时,其中的指针不是 NULL。
这是我的结构
template <typename T>
struct Node {
T value;
Node* next;
};
我为笔记分配内存
first = new Node<T>;
第一个->下一个不为NULL。这迫使我将该注释显式分配给 NULL。这真的让我很困惑。但是为什么会这样呢?
它有一些垃圾值。你应该在构造函数中初始化它。
Node() : next(0), value(T()) {}
或者您可以直接使用
first = new Node<T>();
对于value-initialization
。
未初始化指针的地址在您设置之前是未定义的。当您调用 new Node<T>
时,您正在调用 default-initialization
Default initialization is performed in three situations:
- when a variable with automatic, static, or thread-local storage duration is declared with no initializer.
- when an object with dynamic storage duration is created by a new-expression with no initializer or when an object is created by a new-expression with the initializer consisting of an empty pair of parentheses (until C++03)
- when a base class or a non-static data member is not mentioned in a constructor initializer list and that constructor is called.
The effects of default initialization are:
- If T is a non-POD (until C++11) class type, the constructors are considered and subjected to overload resolution against the empty argument list. The constructor selected (which is one of the default constructors) is called to provide the initial value for the new object.
- If T is an array type, every element of the array is default-initialized.
- Otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
要获得 struct
value-initialized,您需要将其命名为
first = new Node<T>();
请注意,注释 2 的后半部分(非粗体部分)在 C++11 及更高版本中不再为真,现在以下为真:
§8.5 初始化器
An object whose initializer is an empty set of parentheses, i.e., ()
shall be value-initialized.
我正在尝试实现一个链表,但是当我为一个音符分配内存时,其中的指针不是 NULL。
这是我的结构
template <typename T>
struct Node {
T value;
Node* next;
};
我为笔记分配内存
first = new Node<T>;
第一个->下一个不为NULL。这迫使我将该注释显式分配给 NULL。这真的让我很困惑。但是为什么会这样呢?
它有一些垃圾值。你应该在构造函数中初始化它。
Node() : next(0), value(T()) {}
或者您可以直接使用
first = new Node<T>();
对于value-initialization
。
未初始化指针的地址在您设置之前是未定义的。当您调用 new Node<T>
时,您正在调用 default-initialization
Default initialization is performed in three situations:
- when a variable with automatic, static, or thread-local storage duration is declared with no initializer.
- when an object with dynamic storage duration is created by a new-expression with no initializer or when an object is created by a new-expression with the initializer consisting of an empty pair of parentheses (until C++03)
- when a base class or a non-static data member is not mentioned in a constructor initializer list and that constructor is called.
The effects of default initialization are:
- If T is a non-POD (until C++11) class type, the constructors are considered and subjected to overload resolution against the empty argument list. The constructor selected (which is one of the default constructors) is called to provide the initial value for the new object.
- If T is an array type, every element of the array is default-initialized.
- Otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
要获得 struct
value-initialized,您需要将其命名为
first = new Node<T>();
请注意,注释 2 的后半部分(非粗体部分)在 C++11 及更高版本中不再为真,现在以下为真:
§8.5 初始化器
An object whose initializer is an empty set of parentheses, i.e.,
()
shall be value-initialized.