Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3498518
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:29:59+00:00 2026-05-18T12:29:59+00:00

I have a mem_malloc() and mem_free() defined for me and I want to use

  • 0

I have a mem_malloc() and mem_free() defined for me and I want to use them to replace the malloc() and free() and consequently C++’s new and delete.

I define them as follows:

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);
}
}

However, I get two link errors:

[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) How do I get the multiply defined malloc() and free() errors to go away and just take my definition and not the built in one?

2) What library provides dlopen() and friends? I’d expect this to be built in, but they are undefined.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:30:00+00:00Added an answer on May 18, 2026 at 12:30 pm

    I assume you define malloc and free in the main.cpp file that you try to compile and that mem_alloc and mem_free are located in libmemnmf-0.a

    What is probably happening, is that some references in main.cpp require objects from glibc. Specifically, something is dynamically loading a library (dlopen). This code is included in glibc (to answer question 2).
    When the linker includes the objects from glibc and finds that these objects require a malloc/free symbol, it will try to include the malloc/free from the glibc library directly.
    Due to your -static linker flag, the entire libmemnmf-0.a library is included statically in your executable. This will obviously include another malloc and free object in your executable.

    What you should do is put the malloc and free routines in a separate .o file and add that file somewhere in your link-command, preferrably on the end (assuming you do not specify the standard library in a special way on that line). The .o file will satisfy all symbol requests and the glibc library will find these matches resolved whenever dlopen or other objects require them.
    The difference is that the libmnef-0.a file is a library and linkers deal differently with libraries than with simple objects (which has to do with the number of passes through a library to resolve symbols requested by objects in that library).
    Alternatively, you can drop the -static flag, which I expect to resolve the issue as well, but you probably have a good reason to include that flag to start with.

    If you want to override the behaviour of new and delete, you can also look into overloading operator new and operator delete for classes to provide a class-specific allocation method or memory pool.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to replace the global operator new and delete. In Linux this
Have deployed numerous report parts which reference the same view however one of them
Some of my code still uses malloc instead of new . The reason is
Hello I have a chunk of memory (allocated with malloc()) that contains bits (bit
I have a huge allocation (tens of GBs) happening with a malloc call which
As we know, we can use int (*p)[10] to define a pointer which points
I'm trying use double type in openCL, but doesn't work anyway, i want use
I have vps Mem:524 (215used) Swap:524(75used) , mysql 5.1.61 with three tables. Table 'new'
I have a piece of C++ code that deallocates memory as follows for (int
I have been trying to understand the Placement new concept. I searched on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.