当前位置:  开发笔记 > 编程语言 > 正文

C++修改指向结构的指针向量会导致未定义的行为

如何解决《C++修改指向结构的指针向量会导致未定义的行为》经验,为你挑选了1个好方法。

我有一个指向结构的指针向量,这些结构存储在我学校项目的另一个向量中.当我尝试使用指针更改结构中的元素时,由于某种原因导致未定义的行为.我删除了与下面问题相关的部分代码.

#include 
#include 
#include 

class someException{};

enum class ProcessStatus{
  RUNNING,
  READY
};

struct Process{
  int priority;
  std::string PID;
  ProcessStatus status;
  Process(){
    status = ProcessStatus::READY;
  }
};

struct ReadyList{
  std::vector priority1;
  std::vector priority0;
};

class ProcessManager{
private:
  std::vector processList;
  ReadyList readyList;
public:
  ProcessManager(){};

  void createProcess(std::string PID, int priority){
    Process process;
    process.priority = priority;
    process.PID = PID;
    if (priority == 0)
      process.status = ProcessStatus::RUNNING;
    processList.push_back(process);
    switch(priority){
      case 0:
        readyList.priority0.push_back(&processList.at(processList.size()-1));
        break;
      case 1:
        readyList.priority1.push_back(&processList.at(processList.size()-1));
        break;
      default:
        throw someException();
    }
    schedule(findRunningProcess());
  }

  void printProcesses(){
    std::cout<<"ReadyList results:"<PID << " , Priority: "<priority;
      if (process->status == ProcessStatus::RUNNING)
        std::cout << ", Status: RUNNING"<< std::endl;
      else
        std::cout <<", Status: READY"<PID << " , Priority: "<priority;
      if (process->status == ProcessStatus::RUNNING)
        std::cout << ", Status: RUNNING"<< std::endl;
      else
        std::cout <<", Status: READY"<0)
      highestPriorityProcess = readyList.priority1[0];
    else
      highestPriorityProcess = readyList.priority0[0];
    if (currentProcess->priority < highestPriorityProcess->priority){
      currentProcess->status = ProcessStatus::READY;
      highestPriorityProcess->status = ProcessStatus::RUNNING;
    }
  }

  Process* findRunningProcess(){
    for (auto &process: processList){
      if (process.status == ProcessStatus::RUNNING){
        return &process;
      }
    }
    return nullptr;
  }
};

int main(){
  ProcessManager pm = ProcessManager();
  pm.createProcess("ROOT", 0);
  std::cout<<"After creating process ROOT"<

输出结果如下:

After creating process ROOT
ReadyList results:
Process: ROOT , Priority: 0, Status: RUNNING
ProcessList results: 
Process: ROOT , Priority: 0, Status: RUNNING
After creating process A
ReadyList results:
Process: ROOT , Priority: 0, Status: RUNNING
Process: A , Priority: 1, Status: RUNNING
ProcessList results: 
Process: ROOT , Priority: 0, Status: READY
Process: A , Priority: 1, Status: RUNNING

ProcessList设置为正确的值,进程A正在运行并处理ROOT就绪,但由于某种原因,ReadyList保持不变.在我的原始代码中,读取列表中进程ROOT的PID字符串值变为空,并且在更改状态后,我在此示例中遗漏的进程中存储的映射值也被清除.我还测试了直接使用readyList指针进行更改,而不是使用findRunningProcess函数返回的指针,它没有修复PID和映射值的问题,但在进程状态中导致了一些其他未定义的行为.我对可能导致此问题的想法不满意,请帮忙!非常感谢.



1> user4581301..:

每次你:

processList.push_back(process);

vector可能会调整.这意味着支持该数据存储的数据存储区vector将被复制到新的数据存储区中,然后被丢弃.这将留下另外两个vector包含已释放并可能重新分配的内存指针.

processList是一个使用a std::list或a 的好地方,std::deque因为它们在增长时不会使指针无效.std::deque应具有一些性能优势,因为它往往具有更好的空间局部性.

另一种方法是让其他两个vector存储进程的索引,processList因为它们不会改变,只要您只回退并且不删除进程.

在任何一种情况下,删除进程processList而不确定它们是否已首先从其他进程中删除vector将是一个坏主意.std::deque如果你erase从中间进行一个进程,因为这会使指针无效,因此处于不利地位.

推荐阅读
凹凸曼00威威_694
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有