Articles of malloc

检测到glibc *** ./a.out:free():无效指针:

当我编译(C ++程序)在Linux中,我得到以下错误请帮助我 检测到glibc *** ./a.out:free():无效指针:0x0804878d *** ======= Backtrace:========= /lib/libc.so.6[0xbd5f18] /lib/libc.so.6(__libc_free+0x79)[0xbd941d] /usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x3233fe1] ./a.out(__gxx_personality_v0+0x100)[0x8048514] ./a.out(__gxx_personality_v0+0x176)[0x804858a] /lib/libc.so.6(__libc_start_main+0xdc)[0xb877e4] ./a.out(__gxx_personality_v0+0x5d)[0x8048471] =======内存映射:======== 00b55000-00b6e000 r-xp 00000000 fd:00 6687029 /lib/ld-2.4.so 00b6e000-00b6f000 r-xp 00018000 fd:00 6687029 /lib/ld-2.4.so 00b6f000-00b70000 rwxp 00019000 fd:00 6687029 /lib/ld-2.4.so 00b72000-00c9e000 r-xp 00000000 fd:00 6687030 /lib/libc-2.4.so 00c9e000-00ca1000 r-xp 0012b000 fd:00 6687030 /lib/libc-2.4.so 00ca1000-00ca2000 rwxp 0012e000 fd:00 6687030 / lib / li

使用直接I / O进行数据传输的内存alignment要求

我正在阅读Michael Kerrisk的“Linux编程接口”。 我正在通过memalign()用于alignment要求的示例 。 代码和评论对我来说没有意义。 任何人都可以解释为什么我们需要2 *alignment? /* memalign() allocates a block of memory aligned on an address that is a multiple of its first argument. By specifying this argument as 2 * 'alignment' and then adding 'alignment' to the returned pointer, we ensure that 'buf' is aligned on a non-power-of-two multiple of 'alignment'. We […]

无法在malloc()中设置断点

代码示例(foo.c) int main(){ int *x=(int*)malloc(sizeof(int)); // break here *x=10; free(x); return 0; } 我想在malloc中断。 这是我尝试的: # gcc -g foo.c -o bar # gdb bar (gdb) b main Breakpoint 1 at 0x80484cf: file src.c, line 7. (gdb) r Breakpoint 1, main () at src.c:7 (gdb) b malloc Breakpoint 2 at 0x550944 (gdb) c Program exited normally. […]

堆alignment的记忆保护神秘地工作

所以我试图检查,如果我可以正确地使用mprotect更改mmapped分配内存的访问权限,这就是我写的: #include <stdio.h> #include <sys/mman.h> #include <malloc.h> #include <unistd.h> void main() { int pagesize; pagesize = getpagesize(); void *p; p = malloc(pagesize); getchar(); int q = posix_memalign(&p, pagesize, pagesize); getchar(); int a = mprotect(p, pagesize, PROT_READ | PROT_WRITE | PROT_EXEC); getchar(); free(p); } 现在每个函数后,我使用getchar来分析我的内存段使用cat /proc/<pid>/maps文件,这就是我得到:(只显示关于堆的信息,因为这是我唯一担心的) posix_memalign : 01776000-01798000 rw-p 00000000 00:00 0 [heap] mprotectfunction之后: 01776000-01778000 […]

覆盖malloc,free和calloc会导致在Linux中recursion

我已经在Linux平台上覆盖了函数malloc , calloc和free函数,如下所示: #include <stdio.h> #include <dlfcn.h> #include <stdlib.h> typedef void* (*MALLOCFN)(size_t); typedef void* (*CALLOCFN)(size_t, size_t); typedef void* (*CALLOCFN)(size_t, size_t); typedef void (*FREEFN)(void *); MALLOCFN real_malloc = (MALLOCFN) 0; CALLOCFN real_calloc = (CALLOCFN) 0; FREEFN real_free = (FREEFN) 0; void *get_realfn(const char *fnm) { void *pfunc = (void *) NULL; printf("searching for original %s\n", fnm); […]

尝试创build线程时malloc中的错误

大家好,我只发布代码的核心创build问题和哪些线程的工作。 #define HR_OFF h_r-1 pthread_t *threads = NULL; int h_r = 1; int foo(int handler) { // if everything is empty alloc resources if (threads == NULL) { threads = (pthread_t*)malloc(sizeof(pthread_t)); // stuff with other variables h_r++; } else { // stuff with other variables threads = (pthread_t*)realloc(threads, sizeof(pthread_t) * h_r); h_r++; } // stuff […]

debugging器:“无法打开文件:malloc.c”

我在C编码哈希游戏,我正在使用malloc来分配表的内存。 但是,当我编译程序时,出现了分段错误。 使用debugging器,我能够find一个错误:显然,我不能使用malloc,因为它找不到malloc.c或类似的东西。 这里是代码(发生段错误的地方): int inicializaTabuleiro(char *ptrTabuleiro) { //Aloca memória para o tabuleiro ptrTabuleiro = malloc(9); //Verifica se a memória foi alocada if(!ptrTabuleiro) return 0; int contadorPosicao; //Preenche o tabuleiro com '-' for(contadorPosicao = 0; contadorPosicao < 9; contadorPosicao++) ptrTabuleiro[contadorPosicao] = '-'; return 1; } 有什么想法可能是造成它? 附加信息:在Linux Mint上使用GCC编译器的代码块。 谢谢你的时间!

多less数据可以一次mableced? Linux等现代操作系统的限制是什么?

可以调整多less数据,以及如何确定限制? 我正在写一个C语言的algorithm,基本上重复利用数组中存储的一些数据。 我的想法是把这个保存在dynamic分配的数组中,但我不确定是否可能有这样的数量malloced。 我使用200个大小为20的数组,每个大小为8个字节。 我在整个过程中使用这些,所以我不希望一遍又一遍的计算。 你对这种方法的可行性有什么想法? 谢谢,米尔

有没有办法知道在Linux中传递给__free_hook的指针的大小?

我想跟踪一个大型应用程序当前分配了多less内存。 我发现我可以在malloc / free / realloc周围安装钩子以拦截内存分配调用: http://man7.org/linux/man-pages/man3/malloc_hook.3.html 所以我想跟踪的是总字节数分配 – 总字节数释放。 现在问题是免费只需要一个指针,而不是一个大小。 在我的malloc挂钩中可以创build自己的映射或hashmap,跟踪为该指针分配了多less内存,但这会导致相当多的开销。 有没有什么办法(即使这是一个黑客)获得Linux(64位)的大小的空闲时,免费调用(使用默认的g ++ malloc)?

内存损坏是由无效*读取*释放内存引起的?

我越来越 *** glibc检测到***(/ my / program / …):malloc():内存损坏:0xf28000fa *** 我已经在valgrind下运行,它报告已经被释放的内存的情况,但没有非法内存写入的情况 。 可以读取释放内存导致内存损坏? 如果不是的话,还有什么build议可以在valgrind之外的地方看看?