Thread: Memory management - How/why/when

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sloppy View Post
    @Salem
    I use gcc4 under linux (command line bash... does it depend on command line?)

    @gibsosmat
    it seems to me that the error doesn't come for built-in funcions even if I do not cast. but maybe it depends on my compiler...


    PS:
    Using "gcc -S" vs "g++ -S" to get assembly output of a custom malloc(int,int) functions it seems that gcc calls malloc, whereas g++ renames the custom malloc and calls the custom malloc.
    if you add -Wall, gcc/g++ will give you warnings when you "do things wrong". This is what Salem means by "command line" - what you give gcc as a command-line, not which shell you may be using.

    As to "if you define your own malloc with another interface", the difference you see with malloc in C and C++ is that C++ takes into account the arguments if you don't use
    Code:
    extern "C"
    around the function. So the "renaming" you see is the fact that it's being "decorated" (aka "mangled") to indicate the argument types of the function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #2
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    Quote Originally Posted by sloppy View Post
    @Salem
    I use gcc4 under linux (command line bash... does it depend on command line?)

    @gibsosmat
    it seems to me that the error doesn't come for built-in funcions even if I do not cast. but maybe it depends on my compiler...


    PS:
    Using "gcc -S" vs "g++ -S" to get assembly output of a custom malloc(int,int) functions it seems that gcc calls malloc, whereas g++ renames the custom malloc and calls the custom malloc.
    am using the same.. so things dont change much..
    but its like a habit that few ppl learn how not to before learning how to
    I am one of them..

    btw I dont like that .s file.. and I didnt do 2 of my assignments which wanted me to comment whats going on in sparc & PC architecture
    I found it to be the worst thing to be done and calling it a programming assignment.
    still I will learn it latter

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Doesn't gcc4 default to C99 behaviour, which makes implicit declarations of functions illegal?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by Salem View Post
    Doesn't gcc4 default to C99 behaviour, which makes implicit declarations of functions illegal?
    I do not know the default. But If I add to the command line "-std=c99" I get one more warning:
    prova.c:10: warning: implicit declaration of function 'malloc'

    (always both with and without casting)

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Salem View Post
    Doesn't gcc4 default to C99 behaviour, which makes implicit declarations of functions illegal?
    Not with GCC 4.1.2:
    Code:
    main(void) {
      return 0;
    }
    Running "gcc foo.c" gives no warnings. I have to use -Wall to get

    foo.c:1: warning: return type defaults to ‘int’

  6. #6
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    C99 is not the default behavior. From "man gcc":

    std=gnu89
    Default, ISO C90 plus GNU extensions (including some C99 fea-
    tures).
    Last edited by sloppy; 11-07-2007 at 12:12 PM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    It's not likely that C99 will be the default until the support for it is essentially complete.

    http://gcc.gnu.org/c99status.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  3. Valgrind says I suck at memory management :)
    By carrotcake1029 in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 08:10 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM