C++管理堆栈上的内存(链表)

C++ managing memory on the stack (linked lists)

本文关键字:链表 内存 管理 堆栈 C++      更新时间:2023-10-16

很抱歉发布意大利面条代码,但我不知道错误是从哪里来的。这个函数应该模仿c++的文件包含方面的简化版本。它用#include复制粘贴所有文件的内容(并递归地粘贴所包含文件的内容)。

为了确保两个文件不会递归地相互包含,我创建了一个链表,存储之前包含的每个文件。之后包含的文件将根据此链接列表进行检查,以确保文件不包含以前的文件。我们不允许动态分配,所以我们必须使用堆栈中的内存来处理链表。我听说当变量超出范围时,这可能会导致链表的内存问题。

问题是,我的部分链接列表被随机的东西覆盖了。如果你注意到cout语句,结果可能是这样的

上一页:input.txt

newnode文件名:file1.txt

new flist:file1.txt

上一次飞行: 无论什么都很重要(奇怪,它采用了字符串文件名的值)

newnode文件名:file2.txt

新的flist:file2.txt

上一次飞行: file2.txt(按预期工作)

newnode文件名:file3.txt

新的flist:file3.txt

上一次飞行: 随机符号(嗯?这是来自堆栈中的随机存储器吗?)

newnode文件名:file4.txt

新的flist:file4.txt

上一次飞行: file4.txt(按预期工作)

newnode文件名:file5.txt

新的flist:file5.txt

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
struct Node { 
    string fileName;
    Node *link;
};
string extractfilename (string str) 
{
    string filename;
    if ((str.substr(10,1)) == "/" )
    {
        filename = str.substr((str.find_last_of("/"))+1,(str.length()) - (str.find_last_of("/"))-2);
        return filename;            
    } // if     
    else if ( (str.find_last_of("/")) != -1)
    {
        filename = str.substr((str.find_last_of("/")) + 1, (str.length()) - (str.find_last_of("/")) - 2);   
        return filename;
    } // else if 
    else
    {
        filename = str.substr(10,(str.length())-11);
        return filename;
    } // else
    return "ERROR";
}
void check_overlap (string filename, Node *flist)
{  
    while (flist != NULL)
    {
        if (flist->fileName == filename)
        {
            cerr << "Recursive include is being attempted. Terminating program." << endl;
            exit ( -1 );
        }
        flist = flist->link;
    }
}
void processOneFile( istream &in, ostream &out, Node *flist, string prefixDir )
{
    string str;
    getline(in,str);
    while(!(in.fail()))
    {
        string checkinclude = "";
        string checkabsolute = "";
        string prefix = "";
        string filename = "WHATEVER DOESNT MATTER";    
        string relpath = "";
        int checkrelative = 0;    
        int lastof = 0;    
        int length = str.length();
        if ( length > 11)
        {    
            checkinclude = str.substr(0,8);
            checkabsolute = str.substr(10,1);
            checkrelative = str.find_last_of("/");
        }    
        if (checkinclude == "#include")
        {
            ifstream newinput;
            filename = extractfilename(str);
            // PROBLEM WITH THIS ************
            //check_overlap(filename,flist) CAUSES INFINITE LOOP DUE TO DANGLING POINTERS?      
            Node newnode;
            cout << "Previous flist:    "<< flist->fileName << endl;
            newnode.fileName = filename;
            newnode.link = flist;
            Node surrogate_flist = newnode;
            cout << "newnode filename: "<< newnode.fileName << endl;       
            cout << "New flist:         "<< surrogate_flist.fileName << endl;
            cout << endl;
            // PROBLEM WITH THIS **************
            if (checkabsolute == "/" )
            {
                lastof = str.find_last_of("/");
                prefix = str.substr(10,lastof - 9);                    
                newinput.open((prefix + filename).c_str());
                if (!(newinput.is_open()))
                {
                    cout << prefix+filename << " cannot be opened" << endl;
                    exit( -1) ;
                }
                processOneFile(newinput,out,&surrogate_flist,prefix);
                newinput.close();
            } // if        
            else if ( checkrelative != -1)
            {
                relpath = str.substr(10, checkrelative - 9);        
                newinput.open((prefixDir+relpath+filename).c_str());
                if (!(newinput.is_open()))
                {
                    cout << prefixDir + relpath + filename << " cannot be opened" << endl;
                    exit( -1) ;
                }
                processOneFile(newinput,out,&surrogate_flist,(prefixDir+relpath));
                newinput.close();
            } // else if
            else
            {
                newinput.open((prefixDir + filename).c_str());
                if (!(newinput.is_open()))
                {
                    cout << prefixDir +filename << " cannot be opened" << endl;
                    exit( -1) ;
                }
                processOneFile(newinput,out,&surrogate_flist,prefixDir);
                newinput.close();
            } // else
        } // if
        else
        {
            out << str << endl;
        } // else
        getline(in,str);
    } // while
} // processOneFile

感谢

编辑:使用节点结构和链表是的要求

允许字符串函数的隐式动态分配

添加了主要和"完整"可编译代码

给定使用链表的要求,必须使用递归。它应该是这样的:

struct Node
{
    string value;
    Node *link;
};
void Process(Node *front)
{
    Node newNode;
    newNode.link = front;
    if( /* some condition */ )
        return; // end recursion
    else
        Process(&newNode);
}

对Process的第一个调用将只是Process(NULL);,以指示一个空列表。当然,添加额外的参数来包括你所在州的其他部分(我不是来为你做作业的;))。

重要的是,不要修改front->link,因为一旦函数返回,您的新节点将无效,因此调用方不能看到它。因此,您必须反向构建这个列表(每个递归调用都会在前面添加一个节点)。

你已经在做类似的事情了,所以你的问题可能在其他地方。然而,如果没有完整的、可编译的代码,就很难确定在哪里。