中止陷阱-我的代码有问题 我正在用C++开发链表。使用删除时出现问题

中止陷阱-我的代码有问题 我正在用C++开发链表。使用删除时出现问题,c++,C++,我从程序参数中读取元素数。我可以创造它们。我可以列出它们。当我使用delete时,代码不起作用 我是否必须使用delete #include <iostream> #include <sstream> struct element { int value; element *next; }; int main(int argc, char **argv) { int num; std::stringstream ss; if (argc >

我从程序参数中读取元素数。我可以创造它们。我可以列出它们。当我使用
delete
时,代码不起作用

我是否必须使用
delete

#include <iostream>
#include <sstream>

struct element {
  int value;
  element *next;
};

int main(int argc, char **argv) {

  int num;
  std::stringstream ss;

  if (argc >= 2) {
    std::cout << argv[0] << std::endl;
    ss << argv[1];
    ss >> num;
    std::cout << "You want: " << num << " numbers" << std::endl;
  } else {
    std::cout << "Error" << std::endl;
  }

  element *first = NULL;
  element *current = NULL;


  // creating
  for(int i=0; i<num; i++) {
    element *elem = new element();
    if( first == NULL ) {
      first = elem;
    }

    elem -> value = i;
    elem -> next = NULL;

    if( current == NULL ) {
      current = elem;
    } else {
      current -> next = elem;
      current = elem;
    }
  }

  // printing
  current = first;
  while( current ) {
    std::cout << "value: " << current -> value << std::endl;
    current = current -> next;
  }

  // removing
  current = first;
  while( current ) {
    delete current;
    current = current -> next;
  }

  delete first;

}
#包括
#包括
结构元素{
int值;
元素*下一个;
};
int main(int argc,字符**argv){
int-num;
std::stringstream-ss;
如果(argc>=2){

std::cout我已经修复了基于@Yksisarvinen注释的代码。它现在可以工作了。谢谢

#include <iostream>
#include <sstream>

struct element {
  int value;
  element *next;
};

int main(int argc, char **argv) {

  int num;
  std::stringstream ss;

  if (argc >= 2) {
    std::cout << argv[0] << std::endl;
    ss << argv[1];
    ss >> num;
    std::cout << "You want: " << num << " numbers" << std::endl;
  } else {
    std::cout << "Error" << std::endl;
  }

  element *first = NULL;
  element *current = NULL;


  // creating
  for(int i=0; i<num; i++) {
    element *elem = new element();
    elem -> value = i;
    elem -> next = NULL;

    if( current == NULL ) {
      first = elem;
      current = elem;
    } else {
      current -> next = elem;
      current = elem;
    }
  }

  // printing
  current = first;
  while( current ) {
    std::cout << "value: " << current -> value << std::endl;
    current = current -> next;
  }

  // removing
  current = first;
  while( current ) {
    element *to_remove = current;
    current = current -> next;
    delete to_remove;
  }
}
#包括
#包括
结构元素{
int值;
元素*下一个;
};
int main(int argc,字符**argv){
int-num;
std::stringstream-ss;
如果(argc>=2){

std::cout
delete current;current=current->next;
current
已被删除。用
->
解除对它的引用是未定义的行为,因为该对象不再存在。您先删除两次;尝试将nullptr放入已释放的内容“我必须使用
删除
吗?”这就要看。在现代C++中,你不应该使用“<代码>新< /COD>或<代码>删除< /代码>。如果你决定使用它们,有些人会争辩说, DELATE 最后一个含义是没有意义的,程序退出后OS会清理。但是我认为没有指针泄漏是一个好的做法,很容易错过。在重构等情况下会出现这种情况。@Yksisarvinen-如何避免取消引用已删除的对象?我是否必须更改代码?是的,您必须更改代码。您需要首先存储指向下一个元素的指针,然后可以删除当前元素,并将当前指针更改为与下一个相同的指针(之前已存储)。