linux中malloc程序,如何在Linux中重新定义malloc()以用于C new

我为我定义了mem_malloc()和mem_free(),我想用它们来替换malloc()和free(),从而替换C的new和delete.

我将它们定义如下:

extern "C" {

extern void *mem_malloc(size_t);

extern void mem_free(void *);

void *

malloc(size_t size) {

return mem_malloc(size);

}

void

free(void *memory) {

mem_free(memory);

}

}

但是,我收到两个链接错误:

[user@machine test]$g++ -m32 -pthread main.cpp -static libmemnmf-O.a

/usr/lib/../lib/libc.a(malloc.o): In function `free':

(.text+0x153c): multiple definition of `free'

/tmp/ccD2Mgln.o:main.cpp:(.text+0x842): first defined here

/usr/lib/../lib/libc.a(malloc.o): In function `malloc':

(.text+0x3084): multiple definition of `malloc'

/tmp/ccD2Mgln.o:main.cpp:(.text+0x856): first defined here

libmemnmf-O.a(mem_debug.o): In function `mem_init_debug_routines':

mem_debug.c:(.text+0x83c): undefined reference to `dlopen'

mem_debug.c:(.text+0x89d): undefined reference to `dlsym'

mem_debug.c:(.text+0xa03): undefined reference to `dlclose'

mem_debug.c:(.text+0xa24): undefined reference to `dlclose'

mem_debug.c:(.text+0xa2e): undefined reference to `dlerror'

collect2: ld returned 1 exit status

1)如何让多重定义的malloc()和free()错误消失,只是采用我的定义,而不是内置的?

2)什么图书馆提供dlopen()和朋友?我希望这可以内置,但它们是未定义的.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux创建线程有两种方式: 1. pthread_create()函数 pthread_create()函数是POSIX标准线程库创建线程的函数,其函数原型如下: ```c int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); ``` 参数说明: - thread:指向线程标识符的指针 - attr:线程属性 - start_routine:线程函数的指针 - arg:传递给线程函数的参数 示例代码: ```c #include <stdio.h> #include <pthread.h> void *thread_func(void *arg) { printf("Hello, I am a new thread!\n"); return NULL; } int main() { pthread_t tid; int ret; ret = pthread_create(&tid, NULL, thread_func, NULL); if (ret != 0) { printf("Failed to create thread!\n"); return -1; } printf("Main thread: Created a new thread (thread ID: %ld)\n", tid); pthread_exit(NULL); return 0; } ``` 2. clone()系统调用 clone()系统调用可以创建一个线程,其函数原型如下: ```c int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, .../* pid_t *ptid, void *tls, pid_t *ctid */); ``` 参数说明: - fn:线程函数指针 - child_stack:子线程的栈空间 - flags:线程创建标志 - arg:传递给线程函数的参数 示例代码: ```c #define _GNU_SOURCE #include <stdio.h> #include <sched.h> void *thread_func(void *arg) { printf("Hello, I am a new thread!\n"); return NULL; } int main() { char *stack; char *stack_top; int ret; stack = malloc(1024*1024); stack_top = stack + 1024*1024; ret = clone(thread_func, stack_top, CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_VM, NULL); if (ret == -1) { printf("Failed to create thread!\n"); return -1; } printf("Main thread: Created a new thread (thread ID: %ld)\n", ret); return 0; } ``` 注意:clone()系统调用需要在程序定义宏#define _GNU_SOURCE才能使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值