- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经为这个错误苦苦挣扎了一段时间,但我不知道出了什么问题。这是代码:
//the code for the function that is being called
//charset is a const char[] consisting of 91 characters
//charset_size is 91
void set_sequence(char keyword[], int keyword_size){
sequence = malloc(keyword_size);
int i = 0, j = 0;
for(i = 0; i < keyword_size; i++){
for(j = 0; j < charset_size; j++){
if(keyword[i] == charset[j]){
sequence[i] = j;
}
}
}
sequence_size = keyword_size;
}
//the function call in main
set_sequence("foo bar\n", 8);
//there's supposed to be stuff done here with sequence that I haven't implemented yet
free(sequence); //sequence is a global variable that I use the function to set
FILE* dest = fopen("cipher", "w");
我包含文件位的原因是因为当它存在时,我得到断言失败,但当它被注释掉时代码运行绝对正常(我在该行之后没有任何内容,因为我试图查明问题)。
我通过 valgrind 的 memcheck valgrind --tool=memcheck ../bin/cipher
运行代码以查看问题所在,但我无法理解它。这是输出:
==10608== Memcheck, a memory error detector
==10608== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==10608== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==10608== Command: ../bin/cipher -e testfile
Program running in encrypt mode
Open source file: success
Allocate memory for raw_input: success
Read source file: success
Allocate memory for input: success
input set-> freeing raw_input
==10608== Invalid write of size 4
==10608== at 0x80486DC: set_sequence (in /home/hugo/Programming/C++/Cipher
==10608== by 0x8048A86: main (in /home/hugo/Programming/C++/Cipher/bin/cipher)
==10608== Address 0x41f6688 is 0 bytes inside a block of size 8 alloc'd
==10608== at 0x402BB7A: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==10608== by 0x804868C: set_sequence (in /home/hugo/Programming/C++/Cipher/bin/cipher
==10608== by 0x8048A86: main (in /home/hugo/Programming/C++/Cipher/bin/cipher)
==10608==
==10608== HEAP SUMMARY:
==10608== in use at exit: 704 bytes in 2 blocks
==10608== total heap usage: 6 allocs, 4 frees, 1,793 bytes allocated
==10608==
==10608== LEAK SUMMARY:
==10608== definitely lost: 0 bytes in 0 blocks
==10608== indirectly lost: 0 bytes in 0 blocks
==10608== possibly lost: 0 bytes in 0 blocks
==10608== still reachable: 704 bytes in 2 blocks
==10608== suppressed: 0 bytes in 0 blocks
==10608== Rerun with --leak-check=full to see details of leaked memory
==10608==
==10608== For counts of detected and suppressed errors, rerun with: -v
==10608== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 0 from 0)
最佳答案
您没有给出序列声明。它是 char 数组还是 int 数组?如果它是一个int数组,你的malloc是错误的,它需要分配keyword_size * sizeof(int)
字节
关于c - 打开文件会导致 sYSMALLOc 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16889919/
我希望有人能帮助我理解我哪里出了问题。我正在实现一个程序来检查拼写正确性。在此过程中,我使用 trie 数据结构将字典文本文件加载到内存中以检查单词。 总体而言,它似乎按预期运行,但在加载尽可能长的单
我在运行 C 程序时遇到以下 sysmalloc 错误。 malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *)
sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size)
我编写了一个代码来将新成员添加到列表中。当我添加两个成员时它工作正常。但是,一旦我添加第三个并编译并运行代码,就会出现错误。代码及错误如下: #include #include struct list
我已经为这个错误苦苦挣扎了一段时间,但我不知道出了什么问题。这是代码: //the code for the function that is being called //charset is a
我正在尝试编写一个函数,用于搜索模式的所有出现并返回文件中与该模式匹配的偏移量数组。我想使用 realloc 动态增长返回的数组,但出现 SYSMALLOC 断言错误。有时,如果我使用不同的搜索模式,
我在使用 opencv 时遇到 sysmalloc 错误。当我调试时,我发现错误发生在这里: sm = cv::Mat::zeros(h,w,img.type()); 其中h和w分别是img行和w列。
#include #include char* text1 = "This is a string."; char* text2 = "Yet another thing."; void copy
我的 C 程序出现以下消息错误: a.out: malloc.c:2369: sysmalloc: Assertion `(old_top == (((mbinptr)(((char *) &((av
下面的 C 代码在我的 mac OS X 环境中运行良好,但如果我尝试在 ubuntu 环境中运行此代码,每当我输入偶数个输入(例如“1 2”)时,我都会遇到 malloc 断言失败,但奇数输入“1
我在 C 程序上收到一条 SYSMALLOc 错误消息,我相信此错误消息与我使用过的 malloc 相关,而且我通过在 malloc 周围放置 printf 语句发现了这一点,并且它导致了问题。我看不
我正在运行这个 c++ 程序,它不断给我 sysmalloc assertion failed 错误。我正在使用 g++-4.8。 # include # include # include #
/* Dynamic Programming implementation of LCS problem */ #include #include #include #include using na
EDIT: For clarity's sake, I'll leave the question as it is. The problem seems to be that the Kameleo
我正在编写一个名为 Process 的非常简单的结构,乍一看代码似乎已正确实现,但在测试我的代码时,程序似乎一直在崩溃,无论是由于 sysMalloc 断言失败还是由于 double free() 错
我的服务器守护进程在大多数机器上运行良好,但在我得到的一台机器上: malloc.c:3074: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((ch
我的函数如下: void Insert_ldb(int t){ struct node_ldb *temp_ldb1,*lastnode_ldb; temp_ldb1=root_ldb
我这辈子都搞不清楚到底发生了什么。这是我得到的错误: alloc static vecs a.out: malloc.c:2451: sYSMALLOc: Assertion `(old_top ==
我遇到了一个神秘的错误,我不知道为什么。这段代码在失败之前运行了几次,并且总是在同一点失败。 这是我的代码: assert(size > 0); int* sorted = mallo
对于学校作业,我需要跟踪 Product 类的多个实例。 在我的程序的主要部分,我正在制作一个 vector ,如下所示: std::vector> products; 在我的 main 中的特定条件
我是一名优秀的程序员,十分优秀!