- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我自己编写了 malloc 和 free 并将它们编译到一个共享库中。我用我的程序 LD_PRELOAD 那个库。通过这种方式,我的程序将始终使用我的自定义 malloc 和 free 还是在某些情况下不是这样。我听说 gcc 也内置了 malloc 和 free。我的 gcc 附带的 glibc 是否有可能使用内置的 malloc 和 free。
其次,我注意到当我运行我的程序时,我看到 free 函数调用比 malloc/calloc 调用更频繁(98 到 16)。我自己不做任何内存分配(除了一个地方),所以所有分配都是由我使用的标准库函数完成的。还要注意我在我的程序中使用了 pthread。如果你想知道,我的程序是这样的。
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#define NUM_THREADS 8
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
int sum;
float total = 1;
extern int __did_libc_start_main;
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
pthread_mutex_lock( &m );
sum++;
total *= total + tid * 0.097891313423578;
printf( "p%d, tid%d, total = %g, start = %d!\n", getpid(), tid, total, 0 );
pthread_mutex_unlock( &m );
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
char * p;
char * m;
fork();
p = (char*)mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
p[0] = 78;
printf( "p = %p, p[0] = %d, pid = %d!\n", p, p[0], getpid() );
m = (char*)malloc( 80 );
printf( "m = %p!\n", m );
#if 1
for(t=0; t<NUM_THREADS; t++)
{
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
for(t=0; t<NUM_THREADS; t++)
pthread_join(threads[t], NULL);
printf( "\n\nTotal = %g\n\n", total );
/* Last thing that main() should do */
pthread_exit(NULL);
#endif
printf( "\n\n%d: Done without major problems\n\n", getpid() );
return 0;
}
最佳答案
使用 LD_PRELOAD
覆盖 malloc
等预计会起作用;这就是例如DUMA有效。
除了 malloc
、calloc
和 free
之外,确保覆盖 realloc
、memalign
和 valloc
。此外,您可能需要覆盖 C++ new
、new[]
、delete
和 delete[]
。
参见 Overriding 'malloc' using the LD_PRELOAD mechanism有关如何正确执行此操作的示例。
关于c - LD_PRELOADing malloc 和 free,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10913186/
这个问题在这里已经有了答案: How do free and malloc work in C? (8 个答案) 关闭 8 年前。 如果你使用malloc()为4个整数分配内存,它不应该返回第一个整
首先,介绍一下背景知识,这样您就不会认为我在尝试做一些疯狂的事情: 我正在尝试调试由其他人编写的 C 库中的崩溃。崩溃看起来像这样: TheProgram(44365,0x7fff75996310)
我正在 cstdlib malloc() 和 free() 函数之上创建自定义内存分配器(出于性能原因)。分配器位于一个简单的类中,该类存储一些内存缓冲区和其他参数。我想将释放内存的方法命名为 fre
我一直在解决这个练习,我不知道从哪里开始: 语言 B 是上下文无关的;语言 C 是 B 的子集:C 是否是上下文无关的?证明或反驳。 我试过使用闭包属性: C = B - ( (A* - C) ∩ B
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
如果我想获得在 C 中进行 malloc 的指针的所有权,则 docs for the Python cffi package和 this answer假设使用 ffi.gc 和 lib.free 作
#include #include struct node { int value; struct node* next; }; typedef struct node node_
众所周知,Oracle 在 Java 11 中更改了 Java 许可证,要求 JDK 的商业用途需要付费许可证。然而,使用 OpenJDK 仍然是免费的。 我的 PC 上有一个 JDK 11 文件夹,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我是 C 的新手,在 Linux 中使用带有开关 gcc -g -std=c89 -Wall ... 的 gcc4.4.6 进行编程,我在许多函数深处遇到了这个错误我的程序名为 compute: **
在多线程编程中,我们可以找到两个或多个线程/任务之间的数据传输同步的不同术语。 什么时候我们可以说某个算法是: 1)Lock-Free 2)Wait-Free 3)Wait-Freedom 我明白无锁
我正在尝试使用我通过 malloc() 手动分配的数组来运行程序。我在程序末尾释放()这个数组,但我不断收到错误消息 *** glibc detector *** ./test: double fre
我将 libxml2 与 libxslt 一起用于 C++ 程序的 XML 处理。为了使用 XSL 转换 XML 文档,我使用了以下函数(删除了错误处理): xmlDocPtr transformXm
new/delete 关键字使用免费商店 malloc/free 关键字是使用堆 我看到某处写着new 使用malloc。怎么会这样?它们不在内存段中使用? 其次,我看到某处写道我们不能在new 之后
我有这个简单的代码,我想在 tutorialspoint.com 上运行 #include using namespace std; class Vehicle { string vehic
我需要创建一个函数来删除 c 中链表的前 n 个节点,并返回删除的节点数。如果列表小于 n,它应该变为空。 另外,我不能使用递归。 使用现在的代码,它可以工作,但我没有释放“已删除”节点的内存。如果我
我需要调试这段代码的帮助。我知道问题出在 malloc 和 free 中,但找不到确切的位置、原因和解决方法。请不要回答:“使用 gdb”,仅此而已。我会使用 gdb 来调试它,但我仍然不太了解它并且
这个问题在这里已经有了答案: Unable to free const pointers in C (12 个答案) 关闭 8 年前。 将 C++11 代码连接到某些 C 回调,我必须传递 cons
这是出于好奇,我试图找到我对之前问题的疑问的答案,但他们似乎没有答案。所以在这里问,我只是写了一个代码,我试图将内存分配给一个 int 指针(以填充一个数组)并将 int 值扫描到它。完成数组后,我想
我有两个免费的单子(monad),用于不同上下文中的不同操作。但是,如果特定操作位于上下文中,则一个(主要)DSL 需要包含另一个(action)DSL: import Control.Monad.F
我是一名优秀的程序员,十分优秀!