- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
很抱歉发布意大利面条代码,但我不知道错误来自何处。这个函数应该模仿 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/
我会尽可能地解释我正在做的事情,以获得最好的可能的建议/解决方案。这一切都是在 java 中完成的。 我的客户有一个基于 SWING 的桌面应用程序,它将使用 WebStart 加载。我被指派为用户帐
看来这个page包含 Azure CLI 支持的与 Azure API 管理相关的所有功能。但它没有展示如何使用 Azure CLI 管理用户、产品、证书、订阅和 API 等实体。 Azure CLI
我设置了一个 Hadoop 1.2.x 版本,双节点集群。第一节点(NameNode、Jobtracker)和第二节点(Secondary NameNode、Datanode、TaskTracker)
对于内容驱动的网站,设计好坏的关键是关系型数据库。在这个教程中,我们已经使用了MySQL关系型数据库管理系统(RDBMS)建立了我们的数据库。对于网站的开发者来说,MySQL是一个较受欢迎的选择,这
在尝试运行MariaDB之前,首先确定其当前状态,运行或关闭。 有三个选项用于启动和停止MariaDB – 运行mysqld(MariaDB脚本)。 运行mysqld_safe启动脚本。
我在管理界面中遇到 StackedInlines 前缀的问题。我会尝试发布所有必要的代码。 models.py(简要) ##### Base classes class BaseItem(models
我是新来的。到目前为止,我一直在使用 MVC 模型并使用基本的 session 管理模型,即在 session 中存储一个 token 并检查每个请求。 我正在尝试对lift做同样的事情,但我的 se
我在 win 服务中使用 NHiberante。有时我得到 System.ObjectDisposedException: Session is closed! Object name: 'ISess
我正在尝试使用 HtmlUnit 登录 Facebook 页面并查看其 HTML 内容。我正在尝试通过 HtmlUnit 填写登录凭据,但在单击提交按钮时我没有看到正在执行的 session 。 在
我正在为一个相当大的项目开发一个带有 reactjs 的前端,该项目有两个主要接口(interface)。主站点的前端和管理员的前端。 我应该将它们开发为两个不同的项目还是 reactjs 中的一个项
短版 我有一个使用插件基础结构的应用程序。插件具有可配置的属性,可帮助它们了解如何完成工作。插件按配置文件分组以定义如何完成任务,配置文件存储在由 DataContractSerializer 序列化
如何管理 iPhone 应用程序中的用户 session ?我在应用程序的第一页上从用户那里获取了用户名和密码。用户可以随时注销。如何像其他 Web 应用程序一样在 iPhone 应用程序中存储 se
我正在使用 Azure API 管理,其中包含第三方论坛 (Discourse) 的链接。 api管理提供的默认登录系统用于注册用户。我想知道是否可以对 api 管理和论坛使用单点登录,这样用户就不必
我正在使用 Wordpress 建立一个网站,并且我想利用它的 session 。但我没有找到任何插件,甚至文档。在我开始破解之前有什么建议或引用吗? 注意:我问的是 WP 是否以及如何使用标准 PH
我已阅读《Azure in Action》一书中的以下内容:“在 Windows Azure 中,状态服务器或进程外 session 状态提供程序,不支持” 谁能告诉我为什么不支持这个。他们在书中没有
我有一个内联表单集,我想排除一些模型对象在表单集中显示。 例如。模型 B 具有模型 A 的外键,因此它是 1:n(A 对象有许多 B 对象)关系。现在在 A 管理编辑页面上,我已经获得了 B 的内联。
我正在开发一个基于 session 的项目。我在想,与银行类似,我会创建一张支票并为用户提供阻止 session 超时的能力。 我正在考虑创建一个 setInterval 来检查需要身份验证的空白页面
我正在为一位拥有 Magento 商店的客户工作。里面塞满了产品,但这些产品的名称有点乱。他并没有坚持一种命名约定,而是多年来使用了不同的约定。因此,每当他使用“管理”->“管理产品”部分中的“名称”
我使用大约十几个 XSLT 文件来提供大量输出格式。目前,用户必须知道导出的文件格式的扩展名,例如RTF、HTML、TXT。 我还想使用参数来允许更多选项。如果我可以将元数据嵌入 XSL 文件本身,那
我已阅读《Azure in Action》一书中的以下内容:“在 Windows Azure 中,状态服务器或进程外 session 状态提供程序,不支持” 谁能告诉我为什么不支持这个。他们在书中没有
我是一名优秀的程序员,十分优秀!