Thread: c library function conceptual doubt

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33

    c library function conceptual doubt

    When i write a c program using a c library function say malloc , is the machine code for the malloc (which includes system calls or similar calls) present in the executable ?
    or does every OS has to support the c library standard and the code for malloc is present here ?

    I firmly believe that the first one is true ...

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It depends on whether or not your program was linked statically or dynamically. This site has some information on the differences:

    http://sunsite.uakom.sk/sunworldonli...l-02-perf.html

    For instance, using this program:
    Code:
    itsme@itsme:~/C$ cat malloc.c
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      void *p;
    
      if((p = malloc(100)))
        free(p);
    
      return 0;
    }
    I get the following executable sizes.

    Dynamic linking:
    Code:
    itsme@itsme:~/C$ gcc -Wall malloc.c -o malloc
    itsme@itsme:~/C$ ls -l malloc
    -rwxr-xr-x  1 itsme users 10665 2006-05-11 08:09 malloc*
    Static linking:
    Code:
    itsme@itsme:~/C$ gcc -Wall -static malloc.c -o malloc
    itsme@itsme:~/C$ ls -l malloc
    -rwxr-xr-x  1 itsme users 467278 2006-05-11 08:09 malloc*
    itsme@itsme:~/C$
    You can see that the statically linked executable is much, much larger.
    Last edited by itsme86; 05-11-2006 at 09:15 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    Regardless, chances are you're going to have to make a syscall. In windows it has to pass through the memory manager, which is not present in your executable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM