- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我不知道我的程序有什么问题。我使用循环使用 malloc 分配内存,当我释放它时,它给了我以下错误:
"*** glibc detected *** ./assignment4: free(): invalid pointer: 0x08eeb196 ***
此错误伴随着一个列表,该列表具有标题内存映射。循环从 1 运行到 7。奇怪的是,当我释放字符串的 [0]
值时,我没有收到任何错误。该错误仅在我尝试将 [1]
释放到使用循环分配的 [7]
时出现。
void lineParse()
{
int i;
FILE *fp;
fp = fopen("specification.txt", "r");
char ** listofdetails;
listofdetails = malloc(sizeof(char*)*6);
for(i=0;i<6;i++)
{
listofdetails[i] = malloc(sizeof(char)*40);
fgets(listofdetails[i], 40, fp);
/*printf("%s \n", listofdetails[i]);*/
/*free(listofdetails[i]);*/
}
char ** stringOne;
stringOne = malloc(sizeof(char*)*8);
stringOne[0] = malloc(sizeof(char)*6);
stringOne[0] = strtok(listofdetails[0], " ");
for(i=1;i<8;i++)
{
stringOne[i] = malloc(sizeof(char)*6);
stringOne[i] = strtok(NULL, " ");
}
char ** stringTwo;
stringTwo = malloc(sizeof(char*)*8);
stringTwo[0] = malloc(sizeof(char)*6);
stringTwo[0] = strtok(listofdetails[1], " ");
for(i=1;i<8;i++)
{
stringTwo[i] = malloc(sizeof(char)*6);
stringTwo[i] = strtok(NULL, " ");
}
char ** stringThree;
stringThree = malloc(sizeof(char*)*8);
stringThree[0] = malloc(sizeof(char)*6);
stringThree[0] = strtok(listofdetails[2], " ");
for(i=1;i<8;i++)
{
stringThree[i] = malloc(sizeof(char)*6);
stringThree[i] = strtok(NULL, " ");
}
char ** stringFour;
stringFour= malloc(sizeof(char*)*8);
stringFour[0] = malloc(sizeof(char)*6);
stringFour[0] = strtok(listofdetails[3], " ");
for(i=1;i<8;i++)
{
stringFour[i] = malloc(sizeof(char)*6);
stringFour[i] = strtok(NULL, " ");
}
char ** stringFive;
stringFive= malloc(sizeof(char*)*8);
stringFive[0] = malloc(sizeof(char)*6);
stringFive[0] = strtok(listofdetails[4], " ");
for(i=1;i<8;i++)
{
stringFive[i] = malloc(sizeof(char)*6);
stringFive[i] = strtok(NULL, " ");
}
char ** stringSix;
stringSix= malloc(sizeof(char*)*8);
stringSix[0] = malloc(sizeof(char)*6);
stringSix[0] = strtok(listofdetails[5], " ");
for(i=1;i<8;i++)
{
stringSix[i] = malloc(sizeof(char)*6);
stringSix[i] = strtok(NULL, " ");
}
printf(" %s \n", stringSixrows);*/
{ //This works fine
free(stringOne[0]);
free(stringTwo[0]);
free(stringThree[0]);
free(stringFour[0]);
free(stringFive[0]);
free(stringSix[0]);
}
for(i=1;i<8;i++) //But here is where the problem arises. If i remove this code is runs fine.
{
free(stringOne[i]);
free(stringTwo[i]);
free(stringThree[i]);
free(stringFour[i]);
free(stringFive[i]);
free(stringSix[i]);
}
free(listofdetails);
free(stringOne);
free(stringTwo);
free(stringThree);
free(stringFour);
free(stringFive);
free(stringSix);
fclose(fp);
}
最佳答案
让我们看看如何使用 strtok()
:
for(i=1; i<8; i++) {
stringOne[i] = malloc(sizeof(char)*6);
stringOne[i] = strtok(NULL, " ");
}
....
for(i=1; i<8; i++) {
free(stringOne[i]);
}
问题:
malloc()
分配的内存。strtok()
本身不分配内存。strtok()
返回的指针,这将导致未定义的行为。很明显,您不了解 strtok()
的工作原理。因此,让我们尝试对其进行描述。
让我们取一个字符串:"hello world and people"
。
char[] s = "hello world and people";
char * token = strtok(s, " ");
// The memory as s is now: "hello\0world and people\0"
// token points to "hello\0world and people"
token = strtok(NULL, " ");
// The memory at s is now: hello\0world\0and people\0"
// token points to "world\0and people"
token = strtok(NULL, " ");
// The memory at s is now: hello\0world\0and\0people\0"
// token points to "and\0people"
token = strtok(NULL, " ");
// The memory at s is now: hello\0world\0and\0people\0"
// token points to "people"
token = strtok(NULL, " ");
// The memory at s is still: hello\0world\0and\0people\0"
// token points to NULL
请注意,在任何时候我们都没有分配任何额外的内存(除了原始字符串的空间,在这种情况下是在堆栈上分配的)。 strtok()
改变原始字符串,用空字符替换分隔符。
关于C - 释放 Malloc。运行时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25352547/
我是 C 的新手,在 Linux 中使用带有开关 gcc -g -std=c89 -Wall ... 的 gcc4.4.6 进行编程,我在许多函数深处遇到了这个错误我的程序名为 compute: **
今天阅读Rust subreddit时,我发现以下评论: jemalloc针对(多线程)速度而不是内存使用进行了优化 经过更多研究后,我发现还有更多选择(例如calloc)。 我想了解不同内存分配器的
相关代码: write(-1, "test", sizeof("test")); void * p = malloc(1024); void * p2 = malloc(510); w
我正在比较不同的 malloc 实现,我想比较它们的运行时间和内存使用情况。 特别是,我对运行时和最大常驻内存感兴趣。重要的是最大常驻内存将是真实的(没有代码段等)。 我不能使用像 valgrind
我承认这三个都有不同的含义。但是,我不明白这些具体情况适用于哪些特定情况。任何人都可以分享每个例子吗?谢谢。 malloc(sizeof(int)) malloc(size
GLib 文档推荐使用 GLib Slice Allocator 而不是 malloc: "For newly written code it is recommended to use the ne
我正在分配一个字符串 int main(){ int buf = 1024; char *input = malloc(sizeof(char*) * buf); //CODE
Here有一个关于 malloc 包的环境变量列表: MallocStackLogging MallocStackLoggingNoCompact MallocPreScribble MallocSc
总体问题:当您将通过malloc分配的返回值分配给一个指针时,您是否需要malloc该指针以及,还是您可以简单地声明并分配它? 例如,假设我有一个函数 foo,它在执行过程中使用 malloc 创建了
这个问题在这里已经有了答案: String assignment in C (4 个答案) 关闭 7 年前。 这是有问题的片段。 int main() { char** RESERV = (
任务是将一个二进制文件解析到内存中。但是,我事先不知道需要分配的内存量。 哪种方法更可取:在解析例程中进行多个小 malloc,或者首先遍历文件以确定所需的内存量,然后再次解析? 感谢任何提示。 最佳
我最近一直在尝试理解严格别名的一个特定方面,我认为我已经制作了尽可能最小的有趣代码。 (对我来说很有趣,就是这样!) 更新:根据到目前为止的答案,很明显我需要澄清这个问题。从某个角度来看,这里的第一个
我一直在为我创建的一个简单程序创建测试。我总是使用类似这样的方法检查使用 malloc 分配内存是否失败 int* ptr = malloc(sizeof(int) * x); if(!ptr){
我是 malloc 和对齐 malloc 的新手。我知道如何使用它们。但是,我不知道在什么情况下我们应该使用对齐的 malloc 而不是标准的 malloc。你能给我解释一下吗? 最佳答案 glibc
这样分配内存是不好的做法吗?: FOO *foo; while (!(foo = malloc(sizeof(FOO)))) ; 最佳答案 我不知道有什么不好的做法,但这种情况并不常见。 malloc
有人可以向我解释使用和不使用 malloc 创建结构之间的区别吗?什么时候应该使用 malloc,什么时候应该使用常规初始化? 例如: struct person { char* name;
假设我有一个类型 node_t typedef struct node{ char* value; struct node *next; }node_t; 当我想创建一个名为 n1 的
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 3年前关闭。 Improve this questi
我对指针感到困惑。这是交换两个名称的代码。请看代码。考虑输入:hellohai(对于 d)和 asd(对于 e)。我得到的输出:asd 1ellohai 1ellohai #include #incl
我已经编写了这个函数(如下)。它应该逐行读取文件。编辑该行并将某些单词/字符放入各种功能中。然后将这些函数放入“entrant”结构的数组(malloc)中。 问题是,当我退出循环并尝试打印数组时,放
我是一名优秀的程序员,十分优秀!