1

Since I started to learn c and c++, it has always confused me about some special functions such as "printf", "malloc","free","fork","new" and the standard iostream "cout" and "cin",whenever I try to go to definition of these members, it ends up with nothing or a simple declaration, I could never find the definition of them.Recently I'm learning compliers and the process of linking and it still seems unclear for these things. suppose a simple a.c

#include<stdlib.h>
int main()
{
    int* a = malloc(sizeof(int));
}

the function "malloc" was declared in "stdlib.h" but with has not defined. now I use gcc to compile it:

gcc -Wall a.c -o out;

However, this command execute successfully, so I am wondering since there is no link options, how does it resolve the "malloc" function? And I know it was implemented through OS-specified system call, but how does the call-tree looks like? I don't care much about the algorithm it uses, because it's too complicated for me, but I want to have a general idea of how these c standard library function works, Thanks!

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 6
    gcc adds the *standard C runtime* library to the link by default. That's where the definition of malloc is found. Other compilers will vary. – john Jul 11 '22 at 13:12
  • 1
    C and C++ are two *very* different languages. The code you show isn't valid C++. Please only tag the language you're actually programming in. – Some programmer dude Jul 11 '22 at 13:14
  • @john where can I find the run-time library and the source code for it? – Used To Love Jul 11 '22 at 13:15
  • The source code for GCC is available online. Therein you can read the implementation of those functions. – Eljay Jul 11 '22 at 13:15
  • 1
    Also please don't use [*magic numbers*](https://en.wikipedia.org/wiki/Magic_number_(programming)). For example when it comes to the size of types, unless you use the fixed-size integers (like `int32_t`) they don't have a fixed specified size. The size of `int` may be different from `4`. Always use `sizeof` to get the actual size, like in `int *a = malloc(isizeof *a);`. – Some programmer dude Jul 11 '22 at 13:15
  • As for your problem, I'm not even sure what the problem is. As long as you link with the standard C library (which is conveniently called `c`) you can access all standard C functions. You don't really need to know the definitions or have the source for them. – Some programmer dude Jul 11 '22 at 13:18
  • Pick one, either "C" or "C++" they are two different languages. In C++ you will be hard pressed to even find a reference to malloc/free (and even new/delete are no longer recommended there). – Pepijn Kramer Jul 11 '22 at 13:19
  • @ Some programmer dude Yes,it is. I use 4 just to mention that in my computer the Integer is 4 bytes – Used To Love Jul 11 '22 at 13:19
  • @Pepijn Kramer I think c and c++ are similar, you can use c library functions in c++, malloc is allowed in c++ – Used To Love Jul 11 '22 at 13:22
  • 1
    `malloc()` is part of libc, not gcc. For GNU libc malloc() is implemented in `glibc/malloc/malloc.c` – dimich Jul 11 '22 at 13:25
  • libc.so and libstdc++.so are two different libraries. – stark Jul 11 '22 at 13:25
  • @UsedToLove not the source code for GCC - more like the source code for glibc – user253751 Jul 11 '22 at 13:26
  • @UsedToLove No C++ can use "C" libraries, mostly for backward compatibility. The philosophies behind them are totally different. C++ has classes/templates/concepts, can model things like pointer ownership (std::make_unique), you will learn about RAII and excpetion safe programming. So no modern C++ is nothing like "C". – Pepijn Kramer Jul 11 '22 at 13:30
  • possible duplicate of https://stackoverflow.com/questions/33422487/when-do-printf-and-scanf-functions-are-linked-statically-or-dynamically-to-a/33422836#33422836 (not dupehammering because it's my answer) – zwol Jul 11 '22 at 13:40
  • @zwol I think this question here is mostly just asking where to find the source of the standard lib functions rather than how they are linked. – Lundin Jul 11 '22 at 13:44
  • Thanks for your replys! I will read the referenced link one by one. – Used To Love Jul 11 '22 at 13:44

0 Answers0