Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
722 views
in Technique[技术] by (71.8m points)

c - Overloading free() so my program use mine instead of the system's

I need to recode the free() func for educational purpose and it must be named free() also.

When i rename my function myfree() it work flawlessly but when i name it free() the program don't know if he need to use mine or the system's so the program just Segmentation fault(core dumped) even if i don't call my free (just the declaration of another free() func seem to crash it)

so how can i tell the compiler to use mine instead of the system's ?

thanks you in advance.

EDIT : Linux operating system

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Basically, you have three options that I can see

  • Redirect it during compile time, for example using #define as @Mohamed suggests.
  • Change it at runtime using LD_PRELOAD.
  • Modify the existing malloc/free using malloc hooks.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...