- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个文件“a”,有 2000 个字符,只有字符“a”,没有空格。
然后我有这段代码,它通过循环运行,将其添加到缓冲区,如果达到限制最终重新分配,并在出现错误时释放 strBuffer 变量。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
int maxS = 200;
int numericExpression;
int strLength;
char *strBuffer;
strBuffer = malloc(sizeof(char)*maxS+1);
if(strBuffer == NULL)
{
printf("Failed to allocate requested memory!\n");
free(strBuffer);
strLength = sizeof(strBuffer);
printf("Freed %d bytes of memory!\n", strLength);
exit(99);
}
else
{
numericExpression = sizeof(char)*maxS+1;
printf("Alocated: %d Bytes of memory.\n", numericExpression);
}
// while simulation
int fcv = -1;
int numEx;
// file opening simulation
FILE* fd = fopen("a", "r");
int c;
while((c=fgetc(fd) != EOF)) // condition to make sure we realloc only once
{
fcv++;
strBuffer[fcv] = c;
if(fcv == (maxS))
{
printf("Additional memory space required!\n");
int strlensize = strlen(strBuffer);
numEx = (sizeof(char)*(2*strlensize));
strBuffer = realloc(strBuffer, numEx);
if(strBuffer == NULL)
{
printf("Failed to allocate requested memory!\n");
strLength = sizeof(strBuffer);
free(strBuffer);
printf("Freed %d bytes of memory!\n", strLength);
exit(99);
}
else
{
maxS = numEx;
printf("Reallocation successful!\n");
printf("Alocated: %d Bytes of memory.\n", numEx);
}
}
}
strLength = sizeof(strBuffer);
free(strBuffer);
printf("Freed %d bytes of memory!\n", strLength);
}
问题是它最后告诉我,我只释放了 8 个字节的内存。我想这是因为 sizeof(strBuffer) 没有响应预期的大小。当我改用 strlen(strBuffer) 时,我只释放了 2001 个字节。
我想这可能只是打印出释放的字节数的问题。我可能做错了。所以也许我只是不知道我释放了多少字节。但是后来我尝试了 valgrind,它告诉我,我没有足够的自由,存在内存泄漏。但是在程序的每个分支中,我都可以释放 strBuffer 使用的内存。
当我通过 valgrind ("valgrind ./realloc") 运行它时,它告诉我:
==780== Memcheck, a memory error detector
==780== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==780== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==780== Command: ./realloc
==780==
Alocated: 201 Bytes of memory.
Additional memory space required!
==780== Invalid read of size 1
==780== at 0x4C2E0F4: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==780== by 0x400846: main (in /home/dan/Desktop/test_ifj/realloc)
==780== Address 0x51fd109 is 0 bytes after a block of size 201 alloc'd
==780== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==780== by 0x40078C: main (in /home/dan/Desktop/test_ifj/realloc)
==780==
Reallocation successful!
Alocated: 402 Bytes of memory.
==780== Invalid write of size 1
==780== at 0x400823: main (in /home/dan/Desktop/test_ifj/realloc)
==780== Address 0x51fd562 is 0 bytes after a block of size 402 alloc'd
==780== at 0x4C2CE8E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==780== by 0x400866: main (in /home/dan/Desktop/test_ifj/realloc)
==780==
Additional memory space required!
Reallocation successful!
Alocated: 806 Bytes of memory.
Additional memory space required!
==780== Conditional jump or move depends on uninitialised value(s)
==780== at 0x4C2E0F8: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==780== by 0x400846: main (in /home/dan/Desktop/test_ifj/realloc)
==780==
Reallocation successful!
Alocated: 804 Bytes of memory.
Freed 8 bytes of memory!
==780==
==780== HEAP SUMMARY:
==780== in use at exit: 568 bytes in 1 blocks
==780== total heap usage: 5 allocs, 4 frees, 2,781 bytes allocated
==780==
==780== LEAK SUMMARY:
==780== definitely lost: 0 bytes in 0 blocks
==780== indirectly lost: 0 bytes in 0 blocks
==780== possibly lost: 0 bytes in 0 blocks
==780== still reachable: 568 bytes in 1 blocks
==780== suppressed: 0 bytes in 0 blocks
==780== Rerun with --leak-check=full to see details of leaked memory
==780==
==780== For counts of detected and suppressed errors, rerun with: -v
==780== Use --track-origins=yes to see where uninitialised values come from
==780== ERROR SUMMARY: 1200 errors from 3 contexts (suppressed: 0 from 0)
如何正确释放分配的内存?所以不会造成内存泄漏?最终-我做错了吗,有更好的方法吗?感谢您的帮助。
我听从了建议,在 1 个上下文中出现了 3 个错误。这就是我的代码现在的样子:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
int maxS = 200;
int numericExpression;
char *strBuffer;
strBuffer = malloc((maxS+1));
if(strBuffer == NULL)
{
printf("Failed to allocate requested memory!\n");
printf("Freed %d bytes of memory!\n", maxS);
exit(99);
}
else
{
numericExpression = sizeof(char)*maxS+1;
printf("Alocated: %d Bytes of memory.\n", numericExpression);
}
// while simulation
int fcv = -1;
int numEx;
// file opening simulation
FILE* fd = fopen("a", "r");
if(fd == NULL)
{
printf("Error opening a file!\n");
if(strBuffer != NULL)
{free(strBuffer);}
exit(99);
}
int c;
char *tmpBuffer;
while((c=fgetc(fd)) != EOF) // condition to make sure we realloc only once
{
fcv++;
strBuffer[fcv] = c;
if(fcv == (maxS))
{
printf("Additional memory space required!\n");
numEx = ((2*fcv));
tmpBuffer = realloc(strBuffer, numEx);
if(!tmpBuffer)
{
free(strBuffer);
printf("Realloc() failed!\n");
exit(99);
}
else
{
strBuffer = tmpBuffer;
}
if(strBuffer == NULL)
{
printf("Failed to allocate requested memory!\n");
printf("Freed %d bytes of memory!\n", maxS); // well this is questionable, I think
exit(99);
}
else
{
maxS = numEx;
printf("Reallocation successful!\n");
printf("Alocated: %d Bytes of memory.\n", maxS);
}
}
}
free(strBuffer);fclose(fd); // ADDED, still errors occur
printf("Freed %d bytes of memory!\n", maxS);
}
通过相同的 valgrind 调用(“valgrind ./realloc”)我得到了这个:
==1213== Invalid write of size 1
==1213== at 0x4007FD: main (in /home/dan/Desktop/test_ifj/realloc)
==1213== Address 0x51fd560 is 0 bytes after a block of size 400 alloc'd
==1213== at 0x4C2CE8E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1213== by 0x400831: main (in /home/dan/Desktop/test_ifj/realloc)
==1213==
Additional memory space required!
Reallocation successful!
Alocated: 800 Bytes of memory.
Additional memory space required!
Reallocation successful!
Alocated: 1600 Bytes of memory.
Additional memory space required!
Reallocation successful!
Alocated: 3200 Bytes of memory.
Freed 3200 bytes of memory!
==1213==
==1213== HEAP SUMMARY:
==1213== in use at exit: 568 bytes in 1 blocks
==1213== total heap usage: 6 allocs, 5 frees, 6,769 bytes allocated
==1213==
==1213== LEAK SUMMARY:
==1213== definitely lost: 0 bytes in 0 blocks
==1213== indirectly lost: 0 bytes in 0 blocks
==1213== possibly lost: 0 bytes in 0 blocks
==1213== still reachable: 568 bytes in 1 blocks
==1213== suppressed: 0 bytes in 0 blocks
==1213== Rerun with --leak-check=full to see details of leaked memory
==1213==
==1213== For counts of detected and suppressed errors, rerun with: -v
==1213== ERROR SUMMARY: 3 errors from 1 contexts (suppressed: 0 from 0)
有什么提示可以导致这个吗?
最佳答案
这是你的问题:
strBuffer = realloc(strBuffer, numEx);
如果您对 realloc()
的调用失败,则它会返回一个空指针,但不会释放原始内存分配。
需要先检查返回值,如果成功则赋值给原指针:
char *tmpBuffer = realloc(strBuffer, numEx);
if (!tmpBuffer) {
free(strBuffer);
puts("realloc() failed");
exit(1);
}
else {
strBuffer = tmpBuffer;
}
您的代码还有一些其他问题,包括:
如果 strBuffer 为 NULL,则将其传递给 free() 没有意义。
sizeof()
不是运行时函数,因此它不知道分配了多少内存。
strBuffer = malloc(sizeof(char)*maxS+1);
有点草率。我想你的意思是 strBuffer = malloc(sizeof(char)*(maxS+1));
,但你可以只输入 strBuffer = malloc(maxS+1);
因为根据定义,sizeof(char)
为 1。
关于c - 尽管内存已被释放,Valgrind 仍检测到内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27238998/
我希望 valgrind 在发现第一个错误时停止并退出。 请勿推荐 --vgdb-error=1 :它不会退出 valgrind。您必须连接 gdb 并从那里终止。 --db-attach : 在最近
有人可以快速解释 Valgrind 的工作原理吗?一个例子:它如何知道内存何时被分配和释放? 最佳答案 Valgrind 基本上在“沙箱”中运行您的应用程序。在此沙箱中运行时,它能够插入自己的指令来进
我有一个因 SIGSEGV 而崩溃的应用程序。 --20183-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) -
我有一个因 SIGSEGV 而崩溃的应用程序。 --20183-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) -
我想使用 valgrind 检查长时间运行的进程是否存在内存泄漏。我怀疑我所追求的内存泄漏可能仅在执行几个小时后才会发生。我可以在 valgrind 下运行应用程序并获取 valgrind 日志,但这
我想用 valgrind 检查一个长时间运行的进程是否有内存泄漏。我怀疑我所追求的内存泄漏可能仅在执行数小时后才会发生。我可以在 valgrind 下运行应用程序并获得 valgrind 日志,但这样
如何在不通过 valgrind 命令选项启动它的情况下对每个 Process 实例执行 valgrind memcheck。 有没有办法将监控选项保存在进程中,而不是每次都使用 valgrind 命令
我使用了“--trace-children=yes”选项,我还使用了“--trace-children-skip=patt1,patt2,...”选项(过滤掉噪音过程)。但它对我来说仍然很慢,我的多进
我从 Valgrind 得到以下日志: MPK ==5263== 4 bytes in 1 blocks are still reachable in loss record 1 of 84 ==52
如何在 Valgrind 抑制文件中添加注释? 我需要为一个大型项目维护一个 Valgrind 抑制文件。我们从我们链接到的工具中过滤无法修复的错误。随着工具的新版本发布,此文件可能需要随着时间的推移
我有一个大程序要运行。使用 valgrind 需要几个小时才能运行。我听说有一些东西可以让我们为程序中的特定函数调用 valgrind。其余程序将正常执行(没有 valgrind env)。 任何人都
我可以用 valgrind 检测整数溢出缺陷吗?里面的哪个工具可以做到这一点? 最佳答案 Valgrind 没有可以检测整数溢出的工具。 您可能会使用 gcc 选项捕获这些错误: -ftrapv Th
我有一个简单的程序: int main(void) { const char sname[]="xxx"; sem_t *pSemaphor; if ((pSemaphor = sem_o
如何让 Valgrind 准确显示错误发生的位置?我编译了我的程序(通过 PuTTy 在 Windows 机器上通过 Linux 终端)添加了 -g 调试选项。 当我运行 Valgrind 时,我得到
或者最好是全部,而不仅仅是我的代码?我的程序使用 Gtk、Loudmouth 和其他一些东西,而这两个(以及它们背后的一些,libgcrypto、libssl)本身导致了如此多的错误,以至于我无法检测
我想尝试使用 valgrind 进行一些堆损坏检测。通过以下腐败“单元测试”: #include #include #include int main() { char * c = (ch
我看过类似的问题here ,但我的问题是我没有编辑 default.supp 文件的权限。例如,Valgrind 中是否有任何忽略所有抑制文件的命令行选项? 最佳答案 在 Valgrind 3.10.
我在一个运行无限循环的程序上使用 valgrind。 由于memcheck在程序结束后显示内存泄漏,但由于我的程序有无限循环,它永远不会结束。 那么有什么方法可以强制从 valgrind 时不时地转储
我一直在尝试使用 valgrind 查找一些可疑的内存错误。 在被分析的程序甚至到达我希望分析的点之前,它会因为对 mmap 的调用开始失败而退出。当它不在 valgrind 下时,这些调用会成功。
由于 OpenSSL 使用未初始化的内存,因此对使用 openldap2 的 libldap 的程序进行 Valgrind 是一件苦差事。存在一个 --ignore-fn选项,但仅适用于 Valgri
我是一名优秀的程序员,十分优秀!