struct list{
char data;
list *next;
}
void main(){
list *start,node1,node2;
//below two lines got me
start=(struct list*)malloc(sizeof(list)); //Dynamic allocation of memory of size list whose address is stored in start
start=&node1; // start holds the address of node1 which is not dynamically allocated .
我不明白为什么如果第二个语句通过给它提供堆栈中 node1 的内存地址来覆盖它,为什么 *start 会被赋予一个动态地址(至少我的理解是这样)。任何人请帮助。
请解释为什么 *start 以动态和静态两种方式初始化?
最佳答案
我的第一 react :如果你读了那本书,把书扔掉。
我的第二个 react :如果他们只是展示设置指针的不同有效方式,那没关系——尽管我们没有足够的上下文来了解。
基本上你是对的——因为写的代码很糟糕。您无法跟踪分配的内存(因此它会泄漏)。
关于c++ - 在从一本书中阅读有关链表的内容时,我陷入了两行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31421344/