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 8210549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:06:30+00:00 2026-06-07T10:06:30+00:00

In GNU C Library Reference Manual , there is an example program(p.65), But I

  • 0

In GNU C Library Reference Manual, there is an example program(p.65), But I don’t know what the three sentences:
__malloc_hook = old_malloc_hook;
old_malloc_hook = __malloc_hook;
__malloc_hook = my_malloc_hook;
mean. Especailly the second one, who can explain for me? thanks.

static void *
my_malloc_hook (size_t size, const void *caller)
{
    void *result;
    /* Restore all old hooks */
    __malloc_hook = old_malloc_hook;
    __free_hook = old_free_hook;
    /* Call recursively */
    result = malloc (size);
    /* Save underlying hooks */
    old_malloc_hook = __malloc_hook;
    old_free_hook = __free_hook;
    /* printf might call malloc, so protect it too. */
    printf ("malloc (%u) returns %p\n", (unsigned int) size, result);
    /* Restore our own hooks */
    __malloc_hook = my_malloc_hook;
    __free_hook = my_free_hook;
    return result;
}

I write a little program to test it:

#include <stdio.h>
#include <malloc.h>

/* Prototypes for our hooks.  */
static void my_init_hook(void);
static void *my_malloc_hook(size_t, const void *);

/* Variables to save original hooks. */
static void *(*old_malloc_hook) (size_t, const void *);

/* Override initializing hook from the C library. */
void (*__malloc_initialize_hook) (void) = my_init_hook;

static void my_init_hook(void)
{
    old_malloc_hook = __malloc_hook;
    __malloc_hook = my_malloc_hook;
}

static void *my_malloc_hook(size_t size, const void *caller)
{
    void *result;

    /* Restore all old hooks */
    __malloc_hook = old_malloc_hook;

        printf("1: __malloc_hook = %x  old_malloc_hook = %x\n", __malloc_hook, old_malloc_hook);
    /* Call recursively */
    result = malloc(size);

        printf("2: __malloc_hook = %x  old_malloc_hook = %x\n", __malloc_hook, old_malloc_hook);


    /* Save underlying hooks */
    old_malloc_hook = __malloc_hook;

        printf("3: __malloc_hook = %x  old_malloc_hook = %x\n", __malloc_hook, old_malloc_hook);
    /* printf() might call malloc(), so protect it too. */
    printf("malloc(%u) called from %p returns %p\n",
           (unsigned int)size, caller, result);

    /* Restore our own hooks */
    __malloc_hook = my_malloc_hook;
        printf("4: __malloc_hook = %x  old_malloc_hook = %x\n", __malloc_hook, old_malloc_hook);

    return result;
}

int main(void)
{
        char *p;
        p = malloc(10);
        free(p);
        return 0;
}

the result of the program is :

1: __malloc_hook = 0  old_malloc_hook = 0
2: __malloc_hook = 0  old_malloc_hook = 0
3: __malloc_hook = 0  old_malloc_hook = 0
malloc(10) called from 0xb7797f38 returns 0x932c008
4: __malloc_hook = 804849d  old_malloc_hook = 0

but now I have more problems, why old_malloc_hook are all 0, in 1,2,3, why __malloc_hook are 0? I am really confused. Help me.

  • 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-06-07T10:06:31+00:00Added an answer on June 7, 2026 at 10:06 am

    As far as I can tell, everything is working exactly as expected and the output is fine.

    The variable, __malloc_hook, is 0 (or null) probably because the system’s default is to not have a malloc hook.

    As David Schwartz mentioned above, saving the original __malloc_hook is important so that it can be restored just before the original malloc() is called. That’s the line just below the comment /* Restore all old hooks */. I’m guessing that in this specific case, it’s unnecessary, since the original malloc hook is null, but to be safe it should be done.

    Please rest assured that this code is running just like you want it to. For now, I would simply let this stew for a while and perhaps a light-bulb will go off and one day, you’ll understand it completely. (Sorry, but that’s the best I can do today.)

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

Sidebar

Related Questions

HI, guys, I write a small program, named ioprogram.c, with gnu readline library, refering
I am porting a Windows library to Android (with the GNU Standard C++ Library
I want to build a shared library. GNU/Linux is the development and target platform.
I want to use nVIDIA compiler to generate a shared library for my GNU
I tried to use Boost library but I failed, see my code: #include listy.h
I have downloaded source code of glibc (GNU c library), version - 2.15.90. I
I'm trying to integrate the FFTW3 (a GNU FFT library written in C, http://www.fftw.org/
I got a problem while installing the GNU Scientific Library (gsl). I put the
The GNU Scientific library has a multi-dimensional function minimization framework. However, its caveats explicitly
What are difference between The GNU C++ Library (libstdc++), C++ Standard Library , Standard

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.