segmentation-fault - 调用函数时出现分段错误(核心转储错误)
问题描述
我在函数调用中遇到核心转储错误,该错误应该返回一个。该函数正在传递一个向量元素,该向量元素是指向结构的指针,作为函数的参数,但作为返回结果是核心转储。
struct block
{
int64_t tag;
bool valid_bit;
bool dirty_bit;
block* next;
block* prev;
};
vector< block* > cash_set;
……
int main (int argc, const char *argv[])
{
int findcash;
.....
if (cash_type == 1)
{
cout<<"\n Fully Associative";
number_of_blocks = cash_size / block_size;
cout<<"\n Enter the Replacement Policy \n 0 for Random\n 1 for LRU\n 2 for Pseudo LRU\n";
cin>>Replacement_policy;
// initializing the cache to valid bit as 0 and dirty bit as 0
int i;
cash_set.push_back(NULL);
//Reading file "traces.txt"
associativity=number_of_blocks;
int loc;
newfile.open ("traces.txt", ios::in);
//open a file to perform read operation using file object
if (newfile.is_open ())
{ //checking whether the file is open
int count = 0;
while (getline (newfile, tp))
{
cash_access++;
iss.str (tp);
//read data from file object and put it into string.
cout << "\n" << count++ << " " << tp << "\n";
//print the data of the string
iss >> std::hex >> addr;
cout << " Address:" << addr << "\t";
operation = tp.substr (11, 1);
cout << operation << "\n";
//extracting Tag, Index from the address
shift = log2 (block_size);
index_addr = (addr >> shift) % number_of_blocks;
cout<<"done\n";
shift = log2 (number_of_blocks + block_size);
tag_addr = addr >> shift;
findcash=find(cash_set[0],tag_addr);
//after the above line of code it gives Core dump
cout<<"\nfindcache:"<<findcash;
查找功能如下——
int find(block* tmp,int64_t tag1)
{
**if(tmp==NULL)
{
compulsry_misses++;
cout<<"Cumpolsary miss:"<<compulsry_misses;
return 0;
}**
else
{
block* cashblock=tmp;
while(tmp->prev!=NULL)
{
// Cache Hit. Bring the accessed node to the front of the DLL
if (tmp->tag==tag1)
{
cout<<"\n Cache Hit"<<cash_hit;
(tmp->next)->prev=tmp->prev;
(tmp->prev)->next=tmp->next;
cashblock->next=tmp;
tmp->prev=cashblock;
tmp->next=NULL;
cashblock->next=tmp;
cashblock=tmp;
return 1;
}
tmp=tmp->prev;
}
return 0;
}
}
Traces.txt 文件包含以下数据 -
0x6b8b4567 w
0x327b23c6 r
0x643c9869 w
0x66334873 r
0x74b0dc51 w
0x19495cff r
0x2ae8944a w
......
在 Ubuntu 上运行代码后的输出如下 -
....
Fully Associative
Enter the Replacement Policy
0 for Random
1 for LRU
2 for Pseudo LRU
0
0 0x6b8b4567 w
Address:1804289383 w
done
Cumpolsary miss:1
Segmentation fault (core dumped)
解决方案
推荐阅读
- sql - SAS/SQL 文本文件读取
- python - 根据每组真实值的数量删除行 - Python
- java - 带有泛型参数的 TypeReference 以反序列化对象列表
- raku - 编写属性特征
- angular - Angular 错误处理和日志记录 - 调用 GlobalErrorHandler 的 handleError
- acumatica - Acumatica API 插入数据“错误请求”
- c# - 如何在 C# .NET Core 2.1 API 应用程序中将 SQL 查询与 CosmosDB 客户端一起使用
- android - 如何在循环中等待执行任务?
- angular - Angular 事件服务启动调度程序中的问题
- php - PHP 将精度浮点数保存到 MYSQL 最终得到舍入的结果字段