c - MinGW gcc malloc issue with -fno-builtin -


i having strange problem malloc , free in mingw gcc

it can best illustrated following program(notice no external headers)

void free(void* p) {     write(1,"called free\n",12); } int main() { } 

i compiling following command: gcc -g -fno-builtin test.c

running program, expect no output, yet when run program following output:

called free called free called free called free 

using gdb, found free being called in mingwrt-4.0.3-1-mingw32-src\mingwrt-4.0.3-1-mingw32-src\src\libcrt\misc\glob.c

is there anyway turn off? have thought specifying -fno-builtin have made program not expect able call things free

edit: should clarify have written own memory library malloc , free , issue not want mingw call functions. not want use external libraries stdio or stdlib.

i have implemented simple fix of renaming malloc , free ideally able name them malloc , free , not have worry external code calling them. if explain why mingw needs malloc memory in simple program wrote above, appreciate well.

the fact using -fno-builtin flag not mean compiler not call built-in functions.

according documentation, means compiler generate full function call built-in functions, instead of using specials optimizations generating code function in place.

from gcc man page:

don't recognize built-in functions not begin _builtin prefix.

gcc generates special code handle built-in functions more efficiently; instance, calls "alloca" may become single instructions adjust stack directly, , calls "memcpy" may become inline copy loops. resulting code both smaller , faster, since function calls no longer appear such, cannot set breakpoint on calls, nor can change behavior of functions linking different library. in addition, when function recognized built-in function, gcc may use information function warn problems calls function, or generate more efficient code, if resulting code still contains calls function. example, warnings given -wformat bad calls "printf", when "printf" built in, , "strlen" known not modify global memory.

despite being able override implementation of malloc , free, believe way remove calls compiler add these functions initialize program environment possible remove argument -nostartfiles. problem this, responsible start environment, since compiler not provide anymore you.

this article bit off topic, believe can find more answers , details on there:

a whirlwind tutorial on creating teensy elf executables linux


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -