gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:58:21 25 4
gpt4 key购买 nike

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

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

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

上一个列表:input.txt

新节点文件名:file1.txt

新列表:file1.txt

上一个列表: 什么都不重要(很奇怪,它采用了字符串文件名的值)

新节点文件名:file2.txt

新列表:file2.txt

上一个列表: file2.txt(按预期工作)

新节点文件名:file3.txt

新列表:file3.txt

Previous flist: 随机符号(哈?这是来自堆栈中的随机内存吗?)

新节点文件名:file4.txt

新列表:file4.txt

上一个列表: file4.txt(按预期工作)

新节点文件名:file5.txt

新文件: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,因为一旦函数返回,您的新节点将无效,因此必须对其他人不可见调用者,召集者。所以你必须反向构建这个列表(每次递归调用都会在前面添加一个节点)。

您已经在做类似的事情,所以您的问题可能出在其他地方。但是,如果没有完整的可编译代码,就很难确定位置。

关于C++ 管理堆栈上的内存(链表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6382089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com