Ilya
Ilya

Reputation: 5613

I define a C function named 'free': How to stop glibc's printf from using it?

My OS class assignment requires defining a C function named free which works differently from the libc free. (We're doing a memory allocator exercise.)

All of my code uses my own variant of free, so it's not a problem in my own code. However, when glibc uses free internally (e.g. for vprintf), it ends up using my free rather than its own.

I'm using Ubuntu 11.10 (libc6 version 2.15-0ubuntu10). I heard it doesn't happen on other Ubuntu versions, and I wonder if I could get the other versions' behavior via some compile / link flag.

BTW, naming your own function free is not best practice, but let's say I don't have a choice :-)

Upvotes: 1

Views: 270

Answers (2)

ouah
ouah

Reputation: 145919

Do not name your function free if it has external linkage, the function name is reserved for as an identifier with external linkage.

(C99, 7.1.3p1) "All identifiers with external linkage in any of the following subclauses (including the future library directions) are always reserved for use as identifiers with external linkage."

Upvotes: 2

sarnold
sarnold

Reputation: 104120

If you're going to be replacing free and malloc with functions that do not behave identical to the standard ones, then you should probably not use any other "convenience" functions in the C library. Just stick to read(2) and write(2) for your IO -- or write your own wrappers around read(2) and write(2) to provide getc(3) and printf(3) kinds of convenience functions.

Upvotes: 1

Related Questions