我创建了一个使用链表来模拟内存管理的程序,它在Visual Studio中运行时效果很好。但是,当我尝试从命令行运行相同的程序时,情况并非如此。
我将.cpp和.h源文件复制粘贴到Notepad ++并保存,没有#include Visual Studio特定的标题“stdafx.h”。
注意:程序在正常运行之前从命令行获取一个参数。
这是我在命令行中的过程:
g++ pa2.cpp //this worked fine
a.exe worst //for the worst fit algorithm, tested fine in Visual Studio
然后命令行输出第一行代码“使用最差拟合算法”,然后突然宣布“a.exe已停止工作”。
我不知道如何解决这个问题,并且非常感谢任何建议。
这是pa2.h:
#ifndef PA2_H
#define PA2_H
#include <string>
struct Node {
std::string name;
Node * next;
};
class LinkedList {
private:
std::string defaultName;
Node* head;
public:
LinkedList();
void create();
void print();
bool addProgramWorst(std::string name, int size);
bool addProgramBest(std::string name, int size);
int killProgram(std::string name);
int fragmentCount();
};
#endif
pa2.cpp(不包括main,根本不用于链接列表,以及一些不可能导致内存问题的函数):
//sets a null node to "Free" each time it's called (32 times)
void LinkedList::create() {
Node * newNode = new Node;
newNode->name = defaultName;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
}
else {
Node * temp = new Node;
temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
//prints the linked list with formatting
void LinkedList::print() {
Node * temp = head;
for (int counter = 0; temp->next != NULL; counter++) {
if (counter == 8 || counter == 16 || counter == 24) { //new line every 8 nodes
cout << endl;
}
cout << setw(5) << left << temp->name;
temp = temp->next;
}
cout << setw(5) << left << temp->name;
cout << endl;
}
//worst fit algorithm
bool LinkedList::addProgramWorst(string name, int size) {
Node * temp = new Node;
temp = head;
int fragmentSizeCount = 0;
int compFragmentSizeCount = 0; //comparison fragment size count, in order to find biggest fragment
while (temp->next != NULL) { //find the biggest fragment
if (temp->name == defaultName) { //if space is free...go through entire fragment and count up the nodes
compFragmentSizeCount = 0;
while ((temp->name == defaultName) & (temp->next != NULL) & (temp != NULL)) { //safety precautions to avoid null pointers
compFragmentSizeCount++;
temp = temp->next;
}
}
if (compFragmentSizeCount > fragmentSizeCount) { //if that fragment is the biggest so far, store as the "biggest fragment"
fragmentSizeCount = compFragmentSizeCount;
}
if (temp->name == name) { //exits early if program name already found
cout << "Error, Program " << name << " already running." << endl;
return true; //true == there is an error
}
if (temp->next == NULL) { //safety precaution to avoid null pointer
break;
}
temp = temp->next; //move on to next fragment
}
temp = head;
Node * tempTemp = new Node; //in order to go through fragments while still retaining position of the start of the fragment
tempTemp = temp;
compFragmentSizeCount = 0;
while (temp->next != NULL) { //add program to the biggest fragment
if (temp->name == defaultName) { //if space is free...go through entire fragment and count up the nodes
while ((tempTemp->name == defaultName) & (tempTemp->next != NULL)) {
compFragmentSizeCount++;
if (tempTemp->next->name == defaultName) {
tempTemp = tempTemp->next;
}
else {
break;
}
}
}
if (compFragmentSizeCount == fragmentSizeCount) { //if the fragment matches the largest fragment found earlier...
if ((fragmentSizeCount+1) < size) { //if even the largest fragment is too small...
cout << "Error, not enough memory for Program " << name << endl;
return true;
}
for (int counter = 0; counter < size; counter++) { //fill fragment with program
temp->name = name;
temp = temp->next;
}
return false; //false == there is no error
}
compFragmentSizeCount = 0; //reset fragment count so as to count up the next fragment
tempTemp = tempTemp->next;
temp = tempTemp;
}
}
//best fit algorithm
bool LinkedList::addProgramBest(string name, int size) {
Node * temp = new Node;
temp = head;
int fragmentSizeCount = 32;
int compFragmentSizeCount = 0;
while (temp->next != NULL) { //find smallest fragment
if (temp->name == defaultName) { //if the space is free...count the nodes in it
compFragmentSizeCount = 0;
while ((temp->name == defaultName) & (temp->next != NULL) & (temp != NULL)) { //safety precautions against null pointers
compFragmentSizeCount++;
temp = temp->next;
}
}
if ((compFragmentSizeCount < fragmentSizeCount) & (compFragmentSizeCount != 0)) { //if the fragment is the smallest so far (and not 0)...make it the "smallest fragment"
fragmentSizeCount = compFragmentSizeCount;
}
if (temp->name == name) { //if program name already found, exit early
cout << "Error, Program " << name << " already running." << endl;
return true; //true = there is an error
}
if (temp->next == NULL) { //safety precaution against null pointers
break;
}
temp = temp->next;
}
temp = head;
Node * tempTemp = new Node;
tempTemp = temp;
compFragmentSizeCount = 0;
while (temp->next != NULL) { //fill up smallest fragment with program
if (temp->name == defaultName) { //if the space is free...count up the nodes in it
while ((tempTemp->name == defaultName) & (tempTemp->next != NULL)) { //another null pointer safety precaution
compFragmentSizeCount++;
if (tempTemp->next->name == defaultName) {
tempTemp = tempTemp->next;
}
else { //to assist with counting up fragment size outside this loop, to keep things consistent even if the first space is not a free space
break;
}
}
}
if ((compFragmentSizeCount == fragmentSizeCount) & (compFragmentSizeCount != 0)) { //if the fragment matches the smallest fragment found earlier...
if ((fragmentSizeCount + 1) < size) { //if even the smallest fragment is too small for the program...exit early
cout << "Error, not enough memory for Program " << name << endl;
return true;
}
for (int counter = 0; counter < size; counter++) { //add program to fragment
temp->name = name;
temp = temp->next;
}
return false; //false = there is no error
}
compFragmentSizeCount = 0; //reset to count next fragment
tempTemp = tempTemp->next;
temp = tempTemp;
}
}
答案 0 :(得分:-1)
修正了它!当我进入发布模式而不是调试模式时,我意识到我必须正确初始化一些变量而不是仅仅声明它们。然后我再次将代码复制粘贴到记事本中,并在命令行中运行它。所以这实际上是一个非常简单的问题;对C ++来说很新(以前是Java),所以我甚至认为这不是问题所在。谢谢你的建议!