【发布时间】:2017-06-10 03:54:55
【问题描述】:
我有一个指向结构的指针向量,这些结构存储在我的学校项目的另一个向量中。当我尝试使用指针更改结构中的元素时,由于某种原因导致了未定义的行为。我撕掉了与以下问题相关的部分代码。
#include <vector>
#include <string>
#include <iostream>
class someException{};
enum class ProcessStatus{
RUNNING,
READY
};
struct Process{
int priority;
std::string PID;
ProcessStatus status;
Process(){
status = ProcessStatus::READY;
}
};
struct ReadyList{
std::vector<Process*> priority1;
std::vector<Process*> priority0;
};
class ProcessManager{
private:
std::vector<Process> 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:"<<std::endl;
for(auto &process: readyList.priority0){
std::cout << "Process: "<< process->PID << " , Priority: "<<process->priority;
if (process->status == ProcessStatus::RUNNING)
std::cout << ", Status: RUNNING"<< std::endl;
else
std::cout <<", Status: READY"<<std::endl;
}
for(auto &process: readyList.priority1){
std::cout << "Process: "<< process->PID << " , Priority: "<<process->priority;
if (process->status == ProcessStatus::RUNNING)
std::cout << ", Status: RUNNING"<< std::endl;
else
std::cout <<", Status: READY"<<std::endl;
}
std::cout<<"ProcessList results: "<<std::endl;
for(auto &process: processList){
std::cout << "Process: "<< process.PID << " , Priority: "<<process.priority;
if (process.status == ProcessStatus::RUNNING)
std::cout << ", Status: RUNNING"<< std::endl;
else
std::cout <<", Status: READY"<<std::endl;
}
}
private:
void schedule(Process* currentProcess){
Process* highestPriorityProcess;
if (readyList.priority1.size()>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"<<std::endl;
pm.printProcesses();
pm.createProcess("A", 1);
std::cout<<"After creating process A"<<std::endl;
pm.printProcesses();
return 0;
};
输出结果是这样的:
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 未更改。在我的原始代码中,readylist 中进程 ROOT 的 PID 字符串值变为空,并且我在此示例中遗漏的进程中存储的映射值在更改状态后也被清除。 我还测试了直接使用 readyList 指针进行更改,而不是使用 findRunningProcess 函数返回的指针,这并没有解决 PID 和映射值的问题,而是导致进程状态中的一些其他未定义行为。我对可能导致这种情况的原因一无所知,请帮忙!非常感谢。
【问题讨论】:
-
当您执行
processList.push_back(process)时,可能会导致向量重新分配,从而使您的所有指针悬空
标签: c++ pointers vector struct