refer malloc hook

*http://man-wiki.net/index.php/3:_malloc_hook*
*
*
*
http://www.google.co.in/codesearch?hl=en&q=malloc_hook&um=1&ie=UTF-8&ei=o1VMTPKOOoO4rAeJg8y5Dg&sa=X&oi=codesearch_group&ct=title&resnum=4&ved=0CCoQrwQwAw
*
*
*Best Regards
Ashish Goel
"Think positive and find fuel in failure"
+919985813081
+919966006652


On Sun, Jul 25, 2010 at 7:38 PM, xyombie <[email protected]> wrote:

> Create your own malloc() & free() functions.  when you allocate
> memory, allocate a little bit extra memory to add a token of your
> choosing.  When free'ing the memory, check that the token is still
> there.  You are going to want to add the token to the beginning of the
> memory, and not at the end for 2 reasons:
> 1. When free'ing, you don't know how much memory was allocated, so it
> would be difficult to find the end of the memory.
> 2. If there was a buffer overflow error in the application, it would
> likely overwrite your token if it were at the end.
> The other thing to consider is that depending on your compiler flags,
> memory allocations typically occur at word boundaries to help improve
> speed.  Therefore, it would be helpful for the token added to be a
> boundary supported by your compiler.
>
> It is still possible that if there is a buffer overflow error in your
> application, the token can still be wiped out.
>
> Below are some sample functions that could implement this algorithm.
> I used the address of the memory allocated as my token.  I take the
> MAX between sizeof(void*) and sizeof(long long) to ensure that the
> token size will be compatible with the alignment of most
> architectures.  However, you might want to tune this to work better
> (or to work at all) for your architecture.
>
> #define MAX(a,b)   (a > b ? a : b)
> #define TOKEN_LEN MAX(sizeof(void*), sizeof(long long))
>
> void *my_malloc(size_t size)
> {
>     void *p = malloc(size + TOKEN_LEN);
>     if(p != NULL)
>     {
>          ((void*)*p) = p;
>          p += TOKEN_LEN;
>     }
>     return p;
> }
>
> void *my_free(void *p)
> {
>     if(p!=NULL)
>     {
>          p -= TOKEN_LEN;
>          if( ((void*)*p) != p )
>               // ERROR: Trying to free memory not allocated by
> my_malloc() -OR- there was a buffer overflow error
>          else
>               free(p);
>     } else {
>          // ERROR: Trying to free NULL
>     }
> }
>
>
> On Jul 24, 2:08 pm, "dreamer ................" <[email protected]>
> wrote:
> > Write a code that will check whether the memory allotted to the
> > program at the initial and the memory returned to the system is same
> > or not.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<algogeeks%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to