【发布时间】:2013-05-23 13:41:04
【问题描述】:
我正在尝试使用内存迭代地创建“节点”。我的代码目前只是说明了它的地址,实际上并没有尝试在链接列表中创建链接。
这是node 的代码:
struct node {
int num;
node *next;
};
这是malloc()的代码
node *etc = (node*) malloc(sizeof(node));
etc->num = 1;
etc->next = NULL;
cout << etc << endl;
for (int i=2; i<=10; i++) {
node *new_etc;
new_etc = (node*) malloc(sizeof(node));
cout << new_etc << endl;
}
编辑
输出:
0xcff010
0xcff030
0xcff050
0xcff070
0xcff090
0xcff0b0
0xcff0d0
0xcff0f0
0xcff110
0xcff130
【问题讨论】:
-
为什么这个标签是 C++?
-
问题是什么?
-
@JohnDibling:
cout符合 C++ 标准,甚至是有史以来最程序化的代码。 :) -
使用
new/delete,而不是malloc/free。或者写普通的 C。 -
@MatteoItalia:啊,是的,没看到
cout,只看到了malloc。
标签: c++ pointers struct malloc