链表节点移除
if (head->next) {
while (workptr->next && (workptr->next->index != index)) {
workptr = workptr->next;
}
if (workptr->next) {
tempptr = workptr->next;
workptr->next = tempptr->next;
free(tempptr); //这里用delete就会出错
tempptr = nullptr;
--this->size;
}
}
貌似delete把后面的节点一起释放掉了?
高洛峰2017-04-17 13:26:10
A good habit is to use it in pairs
ps: I don’t know why, brother, don’t seek death, follow this creed first, and slowly figure out the reason
new <--> delete
new[] <--> delete[]
malloc <--> free
New/delete in C++ is a newly implemented 内存分配
implement, while malloc and free are 另一套内存分配
implements implemented by the C standard library. They use different algorithms, so of course they cannot be mixed.
As for some weird C++ compiler implementations, they may use free and malloc in the C standard library to implement new/delete. Then we are still not sure whether they can be mixed. If you use it and it works, don't mix it.
In short, they are two different things. If you ask Zhang San to borrow money, you can’t go to Li Si to pay it back
黄舟2017-04-17 13:26:10
In fact, it should be like this
1) delete is a keyword, and free is a library function,
C++ keyword
http://baike.baidu.com/link?url=TJ8RZ7ac_liACKZkHnQt0LOWCtQS9Lxj6vnXKlycyrqZmGEpIaP9H0rO_2hbwxpYUw2G_ZjUShw Pi742hShgAq
The keyword of C is http://www.cnblogs.com/yezhenhan/archive/2011/10/16/2214420.html
Note that there is no free
2) operation in the keyword list of C Objects are different. The operation object of delete is an object or an array of objects, while free is void *
3) Delete in C++ can be overloaded. In other words, delete is an object method, and generally no one overloads free
PHPz2017-04-17 13:26:10
Speaking of the fact that delete and new have a one-to-one correspondence, how does tempptr apply for memory? malloc? If it is not applied with new, it cannot be released with delete.
PHP中文网2017-04-17 13:26:10
It seems that delete releases the subsequent nodes together?
It’s definitely impossible, this is a linked list, the space is not continuous, it’s impossible to release the subsequent nodes
delete is an operator. If the pointer points to an object, the compiler will not only generate instructions to release space during compilation, but also call the destructor of the object
So, could it be a problem with the destructor?
天蓬老师2017-04-17 13:26:10
delete will call the destructor and then release the memory. New and delete are not necessarily forced to be paired. You can malloc to allocate memory and then use placement new to construct the object on the requested memory. After that, you can also use delete to delete it
巴扎黑2017-04-17 13:26:10
It depends on what kind of space you use to apply for. If you use new, use delete, and if you use malloc, use free. In fact, malloc/free is the usage of C language, and C++ is only compatible.
PHP中文网2017-04-17 13:26:10
delete
will call destructor before release.
For POD, these two functions behave the same, but it is still not recommended to mix them.
迷茫2017-04-17 13:26:10
http://stackoverflow.com/questions/240212/what-is-the-difference-between-new-delete-and-malloc-free