- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在处理字符串流时遇到了一些问题;每当我尝试在非空字符串上使用操作符 (<<) 时,我都会在运行程序时打印内存映射。这是代码:
for(int i=1 ; i<djikstra_result[0] ; i++)
{
cout << "DEBUG 5:10 - Round " << i << endl;
if(i==djikstra_result[0]-1)
{
ss << djikstra_result[i] << "km.";
cout << "DEBUG 5:11" << endl;
}
else
{
int j = djikstra_result[i];
cout << "DEBUG 5:13 - j=" << j << endl;
ss << this->airport[j] << " - ";
cout << "DEBUG 5:14" << endl;
}
}
ss是我的stringstream,djikstra_result[]是一个动态int数组,this-airport[]是一个动态字符串数组。
代码编译得很好,但它只运行到 "cout << "DEBUG 5:13 - j="<< j << endl;"。从那一点开始的结果是:
DEBUG 5:13 - j=0*** glibc detected *** ./graph: malloc(): memory corruption: 0x0000000000ec10f0 ***======= Backtrace: =========/lib/libc.so.6(+0x77806)[0x7f9f8403a806]/lib/libc.so.6(+0x7b928)[0x7f9f8403e928]/lib/libc.so.6(__libc_malloc+0x6e)[0x7f9f8403f7de]/usr/lib/libstdc++.so.6(_Znwm+0x1d)[0x7f9f848ab24d]/usr/lib/libstdc++.so.6(_ZNSs4_Rep9_S_createEmmRKSaIcE+0x59)[0x7f9f84887d79]/usr/lib/libstdc++.so.6(_ZNSs4_Rep8_M_cloneERKSaIcEm+0x2b)[0x7f9f8488874b]/usr/lib/libstdc++.so.6(_ZNSs7reserveEm+0x3c)[0x7f9f8488927c]/usr/lib/libstdc++.so.6(_ZNSs6appendERKSs+0x4f)[0x7f9f848896ff]./graph[0x402138]./graph[0x4032ce]/lib/libc.so.6(__libc_start_main+0xfd)[0x7f9f83fe1c4d]./graph[0x401439]======= Memory map: ========00400000-00404000 r-xp 00000000 00:15 15341576 /home/psyberion/Dropbox/algorithms/assign-3b/graph00604000-00605000 r--p 00004000 00:15 15341576 /home/psyberion/Dropbox/algorithms/assign-3b/graph00605000-00606000 rw-p 00005000 00:15 15341576 /home/psyberion/Dropbox/algorithms/assign-3b/graph00ec1000-00ee2000 rw-p 00000000 00:00 0 [heap]7f9f7c000000-7f9f7c021000 rw-p 00000000 00:00 0 7f9f7c021000-7f9f80000000 ---p 00000000 00:00 0 7f9f83fc3000-7f9f8413d000 r-xp 00000000 08:05 2887651 /lib/libc-2.11.1.so7f9f8413d000-7f9f8433c000 ---p 0017a000 08:05 2887651 /lib/libc-2.11.1.so7f9f8433c000-7f9f84340000 r--p 00179000 08:05 2887651 /lib/libc-2.11.1.so7f9f84340000-7f9f84341000 rw-p 0017d000 08:05 2887651 /lib/libc-2.11.1.so7f9f84341000-7f9f84346000 rw-p 00000000 00:00 0 7f9f84346000-7f9f8435c000 r-xp 00000000 08:05 2884391 /lib/libgcc_s.so.17f9f8435c000-7f9f8455b000 ---p 00016000 08:05 2884391 /lib/libgcc_s.so.17f9f8455b000-7f9f8455c000 r--p 00015000 08:05 2884391 /lib/libgcc_s.so.17f9f8455c000-7f9f8455d000 rw-p 00016000 08:05 2884391 /lib/libgcc_s.so.17f9f8455d000-7f9f845df000 r-xp 00000000 08:05 2887634 /lib/libm-2.11.1.so7f9f845df000-7f9f847de000 ---p 00082000 08:05 2887634 /lib/libm-2.11.1.so7f9f847de000-7f9f847df000 r--p 00081000 08:05 2887634 /lib/libm-2.11.1.so7f9f847df000-7f9f847e0000 rw-p 00082000 08:05 2887634 /lib/libm-2.11.1.so7f9f847e0000-7f9f848d6000 r-xp 00000000 08:05 25175504 /usr/lib/libstdc++.so.6.0.137f9f848d6000-7f9f84ad6000 ---p 000f6000 08:05 25175504 /usr/lib/libstdc++.so.6.0.137f9f84ad6000-7f9f84add000 r--p 000f6000 08:05 25175504 /usr/lib/libstdc++.so.6.0.137f9f84add000-7f9f84adf000 rw-p 000fd000 08:05 25175504 /usr/lib/libstdc++.so.6.0.137f9f84adf000-7f9f84af4000 rw-p 00000000 00:00 0 7f9f84af4000-7f9f84b14000 r-xp 00000000 08:05 2887636 /lib/ld-2.11.1.so7f9f84ceb000-7f9f84cef000 rw-p 00000000 00:00 0 7f9f84d10000-7f9f84d13000 rw-p 00000000 00:00 0 7f9f84d13000-7f9f84d14000 r--p 0001f000 08:05 2887636 /lib/ld-2.11.1.so7f9f84d14000-7f9f84d15000 rw-p 00020000 08:05 2887636 /lib/ld-2.11.1.so7f9f84d15000-7f9f84d16000 rw-p 00000000 00:00 0 7fff9500a000-7fff9501f000 rw-p 00000000 00:00 0 [stack]7fff951ff000-7fff95200000 r-xp 00000000 00:00 0 [vdso]ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]Aborted
I have encountered this problem before, but I haven't been able to work out what's causing it, other than some kind of memory problem. Could someone please help?Here's the code for the whole function:
string graph::get_shortest(string name1, string name2)
{
int* djikstra_result;
int result[this->nr_airport];
int source=-1;
int goal=-1;
bool source_found = false;
bool goal_found = false;
string final="";
string space=" - ";
stringstream ss;
cout << "DEBUG 5:1" << endl;
for(int i=0 ; i<this->nr_airport ; i++)
{
cout << "DEBUG 5:2 - Round " << i << endl;
if(name1==this->airport[i])
{
cout << "DEBUG 5:3" << endl;
source = i;
source_found = true;
}
else if(name2==this->airport[i])
{
cout << "DEBUG 5:4" << endl;
goal = i;
goal_found = true;
}
}
cout << "DEBUG 5:5" << endl;
if(!source_found || !goal_found)
{
throw "Starting point or destination not found";
}
cout << "DEBUG 5:6" << endl;
for(int i=0 ; i<this->nr_airport ; i++)
{
result[i] = -1;
cout << "DEBUG 5:7 - Round " << i << endl;
}
cout << "DEBUG 5:8" << endl;
djikstra_result = djikstra(source, goal);
cout << "DEBUG 5:9" << endl;
for(int i=1 ; i<djikstra_result[0] ; i++)
{
cout << "DEBUG 5:10 - Round " << i << endl;
if(i==djikstra_result[0]-1)
{
ss << djikstra_result[i] << "km";
cout << "DEBUG 5:11" << endl;
}
else
{
int j = djikstra_result[i];
cout << "DEBUG 5:13 - j=" << j << endl;
//string val = this->airport[j];
cout << "DEBUG 5:14" << endl;
ss << this->airport[j];
cout << "DEBUG 5:15" << endl;
}
}
ss >> final;
return final;
}
编辑:
数组 this->airport[] 声明如下:
this->cap_airport = 1;
this->airport = new string[this->cap_airport];
当我向数组添加内容时,我使用以下函数扩展它:
void graph::expand_list()
{
if(this->nr_airport==this->cap_airport)
{
this->cap_airport++;
string* temp = new string[this->cap_airport];
for(int i=0 ; i<this->nr_airport ; i++)
{
temp[i] = "";
if(this->airport[i]!="")
{
temp[i] = this->airport[i];
}
}
delete [] this->airport;
this->airport = temp;
}
}
我这样做是为了避免分配比需要更多的空间,并且我只在从文件读取时向数组添加信息。
编辑:
从字符串中删除 << "- "; 后,似乎取得了一些进展。我在主函数中调用函数 get_shortest(string name1, string name2),如下所示:
graph g1;
cout << g1.get_shortest("A","E") << endl;
我现在可以在运行程序时看到更多输出:
DEBUG 5:15DEBUG 5:10 - Round 5DEBUG 5:11AAAA0km*** glibc detected *** ./graph: free(): invalid pointer: 0x0000000001ba7070 ***======= Backtrace: =========/lib/libc.so.6(+0x77806)[0x7f400d9cf806]/lib/libc.so.6(cfree+0x73)[0x7f400d9d60d3]//And so on as before
所以问题似乎与字符串流本身无关。有什么想法吗?
最佳答案
我在程序的其他部分发现了问题;内存映射是无效内存操作的结果,但它并没有像建议的那样从 djikstra_result[] 或 this->airport[] 中读取。试图从数组中的无效位置读取会导致段错误。问题是我在函数 djikstra(int source, goal) 中试图将数据添加 到数组中的无效位置。这是代码:
result = new int[nr_visited];
for(int i=0 ; i<nr_visited+2 ; i++)
{
if(i==0)
{
result[i] = nr_visited;
}
else if(i==nr_visited+1)
{
result[i] = distance;
}
else
{
result[i] = route[i-1];
}
}
return result;
我创建了一个长度为nr_visited的动态int数组,但是我尝试将数据放在nr_visited+1的位置,这是无效的。这会导致内存映射。因此,修复方法是声明长度为 nr_visited+2 的 int 数组:
result = new int[nr_visited+2];
现在,一切正常。
关于c++ - 字符串流产生内存映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10400868/
我在 Chrome 上做了一些测试,requestAnimationFrame 产生了 61 fps 而 setTimeOut( callback, 0 ) 产生了 233 fps。 如果一个人想要超
当我调试代码时,我发现 GCC 和 Clang 都为 0.0/0.0 产生 nan,这是我所期望的,但 GCC 产生的 nan 将符号位设置为 1,而Clang 将其设置为 0(如果我没记错的话,与
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
我在 R Studio 中有一个时间序列。现在我想计算这个系列的log()。我尝试了以下方法: i <- (x-y) ii <- log(i) 但是我得到以下信息:Warning message: I
我有兴趣了解 JavaScript 的内部结构.我试图阅读 SpiderMonkey 的来源和 Rhino但是绕过我的头是相当复杂的。 我问的原因是:为什么像 (![]+[])[+!![]+[]] 生
我们在 Delphi 中使用标准 TWebbrowser 组件,该组件在内部使用 mshtml.dll。另外,我们使用注册表来确保页面使用新的渲染引擎( Web-Browser-Control-Spe
我必须实现一个序列化/反序列化类,并且我正在使用 System.Xml.Serialization 。我有一些IList类型属性并希望在 IList 中序列化解码属于具有特定区域性信息的列表的所有十进
我有一个 Java 应用程序,它读取包含 SQL 查询的 JSON 文件,并使用 JDBC 在数据库上触发它们。 现在我有 5 万个这样的文件,我需要生成 5 万个独立线程来读取每个文件并将它们上传到
我正在尝试将 TensorFlow 入门页面上的示例线性回归程序调整为二次回归。为此,我只是添加了另一个变量并更改了函数。然而,这似乎会导致 NaN 值。这是我的代码: import numpy as
申请后KernelPCA到我的数据并将其传递给分类器 ( SVC ) 我收到以下错误: ValueError: Input contains NaN, infinity or a value too
这背后的想法是,如果我的数据库中存在登录名(正确的用户名+密码),我将重定向到一个页面,并且在进行此身份验证后,他们可以将消息存储在文本文件中。代码非常简单尽管我不确定为什么会收到 IllegalSt
我有一个返回 log10 值的函数。在将它们转换为正常数字时,出现溢出错误。 OverflowError: (34, 'Numerical result out of range') 我检查了日志值,
nosetests 抛出一个 ImportError,尽管我认为这是一个正确配置的 virtualenv。 ==============================================
我是这个网站的新手,所以如果我做错了什么,我提前道歉。当我尝试使用 kivy-garden 的 ScrollLabel 时,它给了我一个错误。基本上我正在尝试创建一个控制台日志,并且我需要能够在文本框
任何人都对 MDSJ 有任何经验?以下输入仅产生 NaN 结果,我不明白为什么。文档非常稀少。 import mdsj.Data; import mdsj.MDSJ; public class MDS
我有一个非常简单的 scala jcuda 程序,它添加了一个非常大的数组。一切都编译和运行得很好,直到我想从我的设备复制超过 4 个字节到主机。当我尝试复制超过 4 个字节时,我收到 CUDA_ER
我正在使用 Hero 组件在两个页面之间创建动画。Hero 组件用于包装一个 Image 小部件(没问题)和一个 Container 小部件(有问题)。 抛出以下溢出错误: ══╡ EXCEPTIO
我无法理解页面 https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/void 中的这一段: This ope
当在 Angular 中使用不立即触发事件的异步管道时(http 请求或任何有延迟的可观察对象),第一个值为 null为什么会这样?如何避免这种情况? 第一个变化: SimpleChange {
如果一个导入的库生成了一个会 panic 的 goroutine 怎么办?在这种情况下,开发人员无法阻止程序退出。 就像在这段代码中一样,使用延迟恢复调用一个错误的库没有帮助,因为该库正在生成一个 p
我是一名优秀的程序员,十分优秀!