Sources for functions like malloc() | | |
Hey all, I have another question. I may seem like a moron here, but
where can I find out how C-standard functions like malloc() and
printf() are declared? I know that I can find on my computer their .h
files, but I can't find the .c files anywhere. Are they already in
object code or something? Can I disassemble them to see how they are
written? | | | | re: Sources for functions like malloc()
Blue Ocean <blueoceanz1@hotmail.com> scribbled the following:[color=blue]
> Hey all, I have another question. I may seem like a moron here, but
> where can I find out how C-standard functions like malloc() and
> printf() are declared? I know that I can find on my computer their .h
> files, but I can't find the .c files anywhere. Are they already in
> object code or something? Can I disassemble them to see how they are
> written?[/color]
This depends entirely on the compiler. The source might not be written
in C at all. Other than that, the compiler vendor might hold the source
as proprietary, and not publish it openly. The source is available for
some compilers, for example GCC, but not for others, like (I presume)
the Microsoft compilers.
--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"This isn't right. This isn't even wrong."
- Wolfgang Pauli | | | | re: Sources for functions like malloc()
In <3349b232.0407060656.746b38d7@posting.google.com > blueoceanz1@hotmail.com (Blue Ocean) writes:
[color=blue]
>Hey all, I have another question. I may seem like a moron here, but
>where can I find out how C-standard functions like malloc() and
>printf() are declared? I know that I can find on my computer their .h
>files,[/color]
Then, use them. They are usually in human readable format and they
contain exactly what you want: the declarations of the standard library
functions.
[color=blue]
>but I can't find the .c files anywhere.[/color]
You don't need them, as long as you're looking for function declarations.
[color=blue]
>Are they already in object code or something?[/color]
They are already compiled and bundled into libraries of object code.
Ready to be used by your linker.
[color=blue]
>Can I disassemble them to see how they are written?[/color]
You can use your librarian utility to extract modules from libraries and
you can even disassemble them, but it is highly unlikely that you will
learn anything about the implementation of the respective functions from
this exercise (except for very simple functions, like strlen or memcpy).
If you're an assembly wizard, you can even try your hand at more complex
functions, but printf and malloc are about the last you may want to try.
Better options are:
1. Get a copy of Plauger's "The Standard C Library". The code is clean,
well commented and documented. Even if it's not the same as the one
used by your implementation, it's much easier to understand how it
works.
2. Try to find out if the source code of your libraries is available.
Some implementations make it available, either for free or for a price.
Even if this is the case, the first option is generally better (reading
the glibc source code is certainly not for the faint of heart).
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de | | | | re: Sources for functions like malloc()
In <ccef8k$abp$1@oravannahka.helsinki.fi> Joona I Palaste <palaste@cc.helsinki.fi> writes:
[color=blue]
>This depends entirely on the compiler. The source might not be written
>in C at all. Other than that, the compiler vendor might hold the source
>as proprietary, and not publish it openly. The source is available for
>some compilers, for example GCC, but not for others, like (I presume)
>the Microsoft compilers.[/color]
Do you *really* expect the gcc sources to contain implementations of
printf and malloc? Are you unable to tell the difference between a
compiler and an implementation of the standard C library?
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de | | | | re: Sources for functions like malloc()
Blue Ocean said the following, on 07/06/04 10:56:[color=blue]
> Hey all, I have another question. I may seem like a moron here, but
> where can I find out how C-standard functions like malloc() and
> printf() are declared? I know that I can find on my computer their .h
> files, but I can't find the .c files anywhere. Are they already in
> object code or something? Can I disassemble them to see how they are
> written?[/color]
I think you have some slight confusion about terminology here. The '.h'
files _do_ contain _declarations_ of the standard library functions.
Depending on the compiler/platform you are using, some standard library
functions may not be implemented in C; they might, for example, be in
assembler. Some implementations, such as the GNU compiler 'gcc', have
source code available for their libraries. Others make available only
the compiled libraries suitable for linking with your programs.
If you would like to understand more about how the library is or can be
implemented, I recommend P.J. Plauger's excellent book, _The Standard C
Library_. It contains an implementation of the library in C, and also a
discussion of what the standard requires, and of portability
considerations. I think you would find it a very valuable aid to
understanding.
--
Rich Gibbs rgibbs@alumni.princeton.edu | | | | re: Sources for functions like malloc()
Dan Pop <Dan.Pop@cern.ch> scribbled the following:[color=blue]
> In <ccef8k$abp$1@oravannahka.helsinki.fi> Joona I Palaste <palaste@cc.helsinki.fi> writes:[color=green]
>>This depends entirely on the compiler. The source might not be written
>>in C at all. Other than that, the compiler vendor might hold the source
>>as proprietary, and not publish it openly. The source is available for
>>some compilers, for example GCC, but not for others, like (I presume)
>>the Microsoft compilers.[/color][/color]
[color=blue]
> Do you *really* expect the gcc sources to contain implementations of
> printf and malloc? Are you unable to tell the difference between a
> compiler and an implementation of the standard C library?[/color]
Dan(g), I wasn't thinking straight. Thanks for correcting me, Dan. Of
course the sources of GCC are free - but they don't contain the
standard library sources, as you said. However, at least Linux uses
"libc" as its standard library, and its sources are also free. libc
and GCC don't have anything to do with each other, at least legally.
--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"This isn't right. This isn't even wrong."
- Wolfgang Pauli | | | | re: Sources for functions like malloc()
Blue Ocean <blueoceanz1@hotmail.com> wrote:[color=blue]
> Hey all, I have another question. I may seem like a moron here, but
> where can I find out how C-standard functions like malloc() and
> printf() are declared? I know that I can find on my computer their .h
> files, but I can't find the .c files anywhere. Are they already in
> object code or something? Can I disassemble them to see how they are
> written?[/color]
Aside from the previous corrections and suggestions (a good book can be
invaluable), the OpenBSD sources (and I'm sure NetBSD) are pretty clean-cut: http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/
If you dive into GCC or glibc you might drown. You can get lost in the
assembly, I18N, SYSV & POSIX.2 & BSD emulation, Linux'isms, etc.
Keep in mind that the internals of malloc(), printf() and the like can
differ widely; you can easily screw yourself by getting chummy w/ them. Keep
a copy of the ISO C standard at hand to stay afloat.
Even better than looking at other sources is trying to emulate the behavior
of the above w/o peeking at what others did. Seeing somebody's answers can
undermine your ability to truly understand the problem(s) and to be readily
able to imagine alternatives. | | | | re: Sources for functions like malloc()
William Ahern <william@wilbur.25thandClement.com> wrote in message news:<fp8rr1-rcb.ln1@wilbur.25thandClement.com>...[color=blue]
> Blue Ocean <blueoceanz1@hotmail.com> wrote:[color=green]
> > Hey all, I have another question. I may seem like a moron here, but
> > where can I find out how C-standard functions like malloc() and
> > printf() are declared? I know that I can find on my computer their .h
> > files, but I can't find the .c files anywhere. Are they already in
> > object code or something? Can I disassemble them to see how they are
> > written?[/color]
>
> [snip]
>
> If you dive into GCC or glibc you might drown. You can get lost in the
> assembly, I18N, SYSV & POSIX.2 & BSD emulation, Linux'isms, etc.[/color]
Well, I'll take your advice there, since I only know what one of those
things actually is (assembly). I am a rising sophmore computer
science student at university. Any guesses as to when I'll pick up
the rest of that? I know it sounds silly but I desire to know
everything about programming, eventually. Or, if not everything, as
absolutely close as possible, and that includes knowing what it would
take to eventually be able to write my own operating system (not that
anything will ever come to that, but I want to come to the point where
I know I could if I had to, however rudimentary it might be).
[color=blue]
> Keep in mind that the internals of malloc(), printf() and the like can
> differ widely; you can easily screw yourself by getting chummy w/ them. Keep
> a copy of the ISO C standard at hand to stay afloat.[/color]
I don't plan to get chummy. =) I wanted to look for curiosity's
sake, because I'd like to see the algorithms.
[color=blue]
> Even better than looking at other sources is trying to emulate the behavior
> of the above w/o peeking at what others did. Seeing somebody's answers can
> undermine your ability to truly understand the problem(s) and to be readily
> able to imagine alternatives.[/color]
To do that, wouldn't I have to design a layer of abstraction? I'm
doing something like that right now in Java. I've got it to the point
where I have a rudimentary assembly language and a representation of
memory and registers, and a gui that can show all of that to the user.
I intend to eventually create a thread scheduler, dynamic memory
allocator, and some other things, but I don't know if I'll ever get
around to writing a scripting language to go on top of all of that
that could be compiled.
Also, to everyone else, forgive me for my incorrect use of the word
"declare." I did indeed mean "define." | | | | re: Sources for functions like malloc()
In <ccerbc$g1v$1@oravannahka.helsinki.fi> Joona I Palaste <palaste@cc.helsinki.fi> writes:
[color=blue]
>Dan(g), I wasn't thinking straight. Thanks for correcting me, Dan. Of
>course the sources of GCC are free - but they don't contain the
>standard library sources, as you said. However, at least Linux uses
>"libc" as its standard library, and its sources are also free. libc
>and GCC don't have anything to do with each other, at least legally.[/color]
You must be talking about glibc, as libc is the generic Unix name of the
main C library, containing most of the standard library functions, as well
as tons of Unix-specific stuff.
gcc is not connected to glibc (it works on many systems where glibc is not
available) but glibc is strongly tied to gcc, i.e. it is NOT written in C
and it uses even the most obscure and undocumented features of both GNU C
and gcc. Many parts of glibc look like complete gibberish to the
standard C programmer.
So, don't recommend things you're not familiar with!
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de | | | | re: Sources for functions like malloc()
Blue Ocean wrote:[color=blue]
> William Ahern <william@wilbur.25thandClement.com> wrote in message news:<fp8rr1-rcb.ln1@wilbur.25thandClement.com>...
>[color=green]
>>Blue Ocean <blueoceanz1@hotmail.com> wrote:
>>[color=darkred]
>>>Hey all, I have another question. I may seem like a moron here, but
>>>where can I find out how C-standard functions like malloc() and
>>>printf() are declared? I know that I can find on my computer their .h
>>>files, but I can't find the .c files anywhere. Are they already in
>>>object code or something? Can I disassemble them to see how they are
>>>written?[/color]
>>
>>[snip]
>>
>>If you dive into GCC or glibc you might drown. You can get lost in the
>>assembly, I18N, SYSV & POSIX.2 & BSD emulation, Linux'isms, etc.[/color]
>
>
> Well, I'll take your advice there, since I only know what one of those
> things actually is (assembly). I am a rising sophmore computer
> science student at university. Any guesses as to when I'll pick up
> the rest of that? I know it sounds silly but I desire to know
> everything about programming, eventually. Or, if not everything, as
> absolutely close as possible, and that includes knowing what it would
> take to eventually be able to write my own operating system (not that
> anything will ever come to that, but I want to come to the point where
> I know I could if I had to, however rudimentary it might be).
>
>[color=green]
>>Keep in mind that the internals of malloc(), printf() and the like can
>>differ widely; you can easily screw yourself by getting chummy w/ them. Keep
>>a copy of the ISO C standard at hand to stay afloat.[/color]
>
>
> I don't plan to get chummy. =) I wanted to look for curiosity's
> sake, because I'd like to see the algorithms.
>
>[/color]
There is a whole lot of information about memory allocation
strategies. For algorithms, use your favorite search engine
and these terms "memory allocation algorithm". Perhaps also
"garbage collection algorithm". After all, the malloc() family
are just memory allocation functions.
Many courses in Operating Systems will review some memory
allocation algorithms. There is no single memory allocation
strategy that fits all. There are a whole bunch so that one
can choose the ones that best fit the situation.
For algorithmic discussions, use news:comp.programming.
--
Thomas Matthews
C++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.html
Other sites: http://www.josuttis.com -- C++ STL Library book | | | | re: Sources for functions like malloc()
Dan Pop <Dan.Pop@cern.ch> scribbled the following:[color=blue]
> In <ccerbc$g1v$1@oravannahka.helsinki.fi> Joona I Palaste <palaste@cc.helsinki.fi> writes:[color=green]
>>Dan(g), I wasn't thinking straight. Thanks for correcting me, Dan. Of
>>course the sources of GCC are free - but they don't contain the
>>standard library sources, as you said. However, at least Linux uses
>>"libc" as its standard library, and its sources are also free. libc
>>and GCC don't have anything to do with each other, at least legally.[/color][/color]
[color=blue]
> You must be talking about glibc, as libc is the generic Unix name of the
> main C library, containing most of the standard library functions, as well
> as tons of Unix-specific stuff.[/color]
Yes, that's what I was thinking about.
[color=blue]
> gcc is not connected to glibc (it works on many systems where glibc is not
> available) but glibc is strongly tied to gcc, i.e. it is NOT written in C
> and it uses even the most obscure and undocumented features of both GNU C
> and gcc. Many parts of glibc look like complete gibberish to the
> standard C programmer.[/color]
Well, if we replace libc with glibc, I was right in saying that the
sources are freely available. But I was wrong in saying GCC and glibc
don't have anything to do with *each other*.
[color=blue]
> So, don't recommend things you're not familiar with![/color]
How else could I get you to correct my mistakes? =)
--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby."
- Kent Brockman | | | | re: Sources for functions like malloc()
In <cch60k$md5$1@oravannahka.helsinki.fi> Joona I Palaste <palaste@cc.helsinki.fi> writes:
[color=blue]
>Dan Pop <Dan.Pop@cern.ch> scribbled the following:[/color]
[color=blue][color=green]
>> So, don't recommend things you're not familiar with![/color]
>
>How else could I get you to correct my mistakes? =)[/color]
I'd rather spend my time posting answers than correcting other people's
incorrect answers.
If you *know* the answer to a question, post it. If you don't, let
other people do it, rather than assume that you're the OP's last hope.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de | | | | re: Sources for functions like malloc()
Blue Ocean <blueoceanz1@hotmail.com> wrote:[color=blue]
> William Ahern <william@wilbur.25thandClement.com> wrote in message news:<fp8rr1-rcb.ln1@wilbur.25thandClement.com>...
> <snip>
> Well, I'll take your advice there, since I only know what one of those
> things actually is (assembly). I am a rising sophmore computer
> science student at university. Any guesses as to when I'll pick up
> the rest of that?[/color]
When you need to, and no sooner ;) Patience is a virtue.
POSIX.2, "SYSV" and "BSD" are intersecting sets of Unix C programming APIs.
If you're on a Unix system thumb through the man pages and you'll see
references. I18N is short for internationalization, a generic moniker for
the process of making software portable across language and custom. http://en.wikipedia.org/wiki/POSIX http://en.wikipedia.org/wiki/I18n
GCC and glibc try to be lots of things to lots of people, and most of the
developers have been around the block quite a few times. Over time you'll
become acquainted w/ all the things its riddled w/, but for the
uninitiated--including myself--its usually not well suited for exhibition. http://www.gnu.org/doc/doc.html
[color=blue]
> I know it sounds silly but I desire to know
> everything about programming, eventually. Or, if not everything, as
> absolutely close as possible, and that includes knowing what it would
> take to eventually be able to write my own operating system (not that
> anything will ever come to that, but I want to come to the point where
> I know I could if I had to, however rudimentary it might be).[/color]
<snip>
Since we're on comp.lang.c, I'll just suggest to listen intently to these
folks. Some people mistake their preciseness as unneccessary pedantism, but
if you stick around long enough you learn that precision is important unto
itself.
As far as C is concerned, stick to the ANSI/ISO standards. http://rm-f.net/standards/ http://oakroadsystems.com/tech/c-predef.htm
If you can't solve your particular problem from there (and most of the time
you can), then drop down a level to another standard; something like
POSIX.2/SUSv3--Standard Unix Specification--if you're on a Unix. http://www.unix.org/version3/online.html
If you still can't solve your problem satisfactorily, then resort to
non-standard but reasonably portable interfaces. Those are sometimes
difficult to find. snprintf() was sort of in this boat prior to the
C99 standard.
Then resort to platform or environment specific features.
This is a rule of thumb (not only just for C programming). Ultimately the
point is that an affinity for standards, idioms and established practices
keeps you anchored and focused so that you can progress smoothly. They
equally provide a reference for things they never mention by virtue of
defining a landscape. Similarly--for somebody who has the faintest bit of
curiosity and can read--the extremely dangerous but standardized gets()
function in standard C provides an excellent, concrete example how _not_ to
do something. In this sense, a standard serves as an enabler of productive
discourse.
Of course, there's no substitute for coding, coding, coding. But it seems
you already recognize that. | | | | re: Sources for functions like malloc()
William Ahern wrote:[color=blue]
> Blue Ocean <blueoceanz1@hotmail.com> wrote:
>[color=green]
>> Hey all, I have another question. I may seem like a moron here,
>> but where can I find out how C-standard functions like malloc()
>> and printf() are declared? I know that I can find on my
>> computer their .h files, but I can't find the .c files anywhere.
>> Are they already in object code or something? Can I disassemble
>> them to see how they are written?[/color]
>
> Aside from the previous corrections and suggestions (a good book
> can be invaluable), the OpenBSD sources (and I'm sure NetBSD) are
> pretty clean-cut:
>
> http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/
>[/color]
.... snip ...
Piggybacking here, since the original is no longer available.
You can also find the source for malloc, free, realloc, and some
auxiliary optional debug modules at:
<http://cbfalconer.home.att.net/download>
Look for the "nmalloc.zip" package.
--
Replies should be to the newsgroup
Replace this line to publish your e-mail address. | | | | re: Sources for functions like malloc()
In <bhptr1-tjc.ln1@wilbur.25thandClement.com> William Ahern <william@wilbur.25thandClement.com> writes:
[color=blue]
>POSIX.2, "SYSV" and "BSD" are intersecting sets of Unix C programming APIs.[/color]
Last time I checked POSIX.2 was dealing with the command line utilities.
It is POSIX.1 that deals with the API.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|