Thread: malloc & cover function

  1. #1
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    malloc & cover function

    I want to write a cover function for malloc, so that each time malloc() is invoked, the program execute fmalloc() instead. In fmalloc() I will execute the real malloc(). How do I do that??


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main (void)
    {
    	char *pt,*lt,*gt;
    	
    	pt=(char*)malloc(40);
    	if (pt==NULL)
    		{
    		perror("fmalloc");
    		exit(1);
    		}
    	
    	lt=(char*)malloc(40);
    	if (lt==NULL)
    		{
    		perror("fmalloc");
    		exit(1);
    		}
    
    	gt=(char*)malloc(40);
    	if (gt==NULL)
    		{
    		perror("fmalloc");
    		exit(1);
    		}
    
    	strcpy(pt, "abcdefghijklmnopqrstuvxyz");
    	strcpy(lt, "abcdefghijklmnopqrstuvxyz");
    	strcpy(gt, "abcdefghijklmnopqrstuvxyz");
    
    
    	printf("Address allocated is %p\n",pt);
    	printf("Address allocated is %p\n",lt);
    	printf("Address allocated is %p\n",gt);
    
    	free(pt);
    	free(lt);
    	free(gt);
    
    	printf("Address freed is %p\n",pt);
    	printf("Address freed is %p\n",lt);
    	printf("Address freed is %p\n",gt);
    	
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What do you mean, 'how do you do it'?
    Code:
    void *fmalloc( size_t s )
    {
        return malloc( s );
    }
    I don't understand what you aren't knowing how to do.

    "Bob, I can 'do that code' in one line!"
    "Quzah, do that code!"

    #define fmalloc malloc

    [edit]
    I misunderstoold what you had intended. You shouldn't try and override standard functions. I mean I guess you can try, but it's bad practice. Especially if you're going to try and have reusable code at all.
    [/edit]



    Quzah.
    Last edited by quzah; 10-03-2002 at 01:49 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Maybe something like the following would work.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void *fmalloc(size_t size)
    {
       void *start = malloc(size);
       if(start)
       {
          char *end = start;
          end += size;
          printf("fmalloc:%p-%p\n", start, end);
       }
       else
       {
          puts("fmalloc failed");
       }
       return(start);
    }
    
    #define malloc fmalloc
    
    int main()
    {
       char *ptr = malloc(64);
       free(ptr);
       return 0;
    }
    
    /* my output
    fmalloc:007A305C-007A309C
    */

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I don't see anything too wrong with writing your own malloc. However, of the standard library functions malloc is probably the most frustrating to write.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I don't see anything too wrong with writing your own malloc.
    I do. Without very careful planning and use of a standard memory allocation routine you lose any vestige of portability. Writing a wrapper is far better:
    Code:
    void *fmalloc ( size_t size )
    {
      void *new_block;
    
      new_block = malloc ( size );
      if ( new_block == NULL ) {
        fputs ( "Error allocating memory\n", stderr );
        exit ( EXIT_FAILURE ); /* Or your favorite recovery routinte */
      }
    
      return new_block;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM