Overriding C run-time functions

This is a discussion on Overriding C run-time functions within the C Programming forums, part of the General Programming Boards category; Hi, does anyone know how to override the C run-time functions. I need to write my own malloc() function with ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    13

    Question Overriding C run-time functions

    Hi, does anyone know how to override the C run-time functions. I need to write my own malloc() function with the same function signature as Microsoft's C run-time malloc(). Any suggestion/help? When I link my program I get error message indicating a duplicate function.
    Last edited by rassul; 09-12-2004 at 11:03 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    #define malloc(x) my_malloc(x)

    Then write your malloc() implementation as my_malloc().
    If you understand what you're doing, you're not learning anything.

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    I don't know what compiler you are using, but if you are writing your own malloc(), I am guessing you aren't going for portability. Looking at my stdlib.h, gcc, I found this macro: __need_malloc_and_calloc. Might want to mess with that and see what happens.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    if you are lucky and don't use any function from stdlib.h then just don't include it and then define malloc yourself.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    13
    Thank you all for your help.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not just write your own version of malloc() and give it another name? I think thats ultimately the best way of doing this.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    If you're using Linux (or at least GNU LD), then
    http://www.gnu.org/software/binutils...l_mono/ld.html
    and look at
    --wrap symbol
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Run time error?
    By shwetha_siddu in forum C Programming
    Replies: 6
    Last Post: 04-21-2009, 01:37 PM
  2. having a real tough time with functions..
    By simstim99 in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2007, 12:48 AM
  3. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  4. Average asymptotic run time?
    By Ariod in forum C Programming
    Replies: 1
    Last Post: 08-03-2005, 06:47 PM
  5. Create class at run time
    By muaed in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-11-2002, 08:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21