C++ - 代码在Visual Studio中完美运行,但命令行中的.exe不断崩溃
C++ -- Code runs perfectly in Visual Studio, but a.exe in the command line keeps crashing
我做了一个程序,它使用链表来模拟内存管理,它在Visual Studio中运行时可以完美运行。但是,当我尝试从命令行运行相同的程序时,情况并非如此。
我将.cpp和.h源文件复制粘贴到Notepad++并保存它们,没有 #including 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;
}
}
修复了它!当我进入发布模式而不是调试模式时,我意识到我必须正确初始化一些变量,而不仅仅是声明它们。然后,我再次将代码复制粘贴到记事本中,并在命令行中运行它。所以这实际上是一个非常简单的问题;对C++来说非常陌生(以前是Java(,所以我什至不认为这可能是问题所在。谢谢你的建议!
相关文章:
- 从命令行c++发送文本文件名
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 数组长度,为什么从命令行获取时不能使用它?
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 通过 Openssl 命令行加密,通过 c++ 解密
- 在 Windows 命令行中运行.exe在使用 2 种相同方式执行时会产生不同的输出
- C++ - 代码在Visual Studio中完美运行,但命令行中的.exe不断崩溃
- .EXE程序的C 命令行参数
- 如何在Visual Studio 2010中捕获cl.exe命令行
- 如何在Visual Studio中获取要发送到cl.exe的命令行参数
- 如何使用Qt Windows中的设置路径属性从命令行运行应用程序(.exe)时在命令行上打印日志
- 命令行中的应用错误帮助 cl.exe版本 12.00.8186
- Visual Studio 2010 从命令行运行.exe与运行 (f5) 调试
- 创建我的.exe的批处理版本,它接受命令行参数(ParamCount(), ParamStr()和BCB 4.0中的Fi
- 命令行编译使用cl.exe
- 从命令行执行exe后,命令行应等待exe完成
- 如何在没有IO窗口(命令行窗口)的情况下运行exe