C++ - 代码在Visual Studio中完美运行,但命令行中的.exe不断崩溃

C++ -- Code runs perfectly in Visual Studio, but a.exe in the command line keeps crashing

本文关键字:命令行 exe 崩溃 运行 代码 Visual 完美 Studio C++      更新时间:2023-10-16

我做了一个程序,它使用链表来模拟内存管理,它在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(,所以我什至不认为这可能是问题所在。谢谢你的建议!