Memory management - How/why/when

This is a discussion on Memory management - How/why/when within the C Programming forums, part of the General Programming Boards category; Originally Posted by matsp Not so. If you don't have a prototype [at least a basic "return type" declaration - ...

  1. #16
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    Quote Originally Posted by matsp View Post
    Not so. If you don't have a prototype [at least a basic "return type" declaration - arguments isn't important for the discussion here] on a system that uses different registers for different types [this applies to for exampl floating point returns in FPU registers too], then the compiler will not KNOW that the return value from malloc() is a pointer, and thus assume that it's an integer type operation and use the wrong register. How is the compiler going to know what the return type is if it's not been told - the compiler doesn't KNOW intrinsically that malloc return a pointer. For that matter, if you don't actualy need the malloc function in the C library, you could very well write another function called malloc() that does somethign completely different - the compiler should be able to compile this too.

    --
    Mats
    I think there is a confusion with the term I used 'compiler'.
    I meant to say that the total process of compilation & linking at once. (Ex: cc -o test test.c)

    Well, then lets talk in real terms of 2 stage building compiling & linking..

    Still if I make my own malloc and call it in my program without prototyping
    and in the end if I link it with the libc malloc.. that should work and malloc code will be generated. but the problem is that our call stack is not the way we wanted and the system dosent know that. It continues to execute the instructions until it violates some system's rules, so the error is undefined as it depends on the stack of that program.

    i.e: if I use my custom void* malloc(int,int)
    and I link it with std malloc
    the compiler generates the code for malloc in the ASM level handling what to copy where.. etc..
    but the problem is that malloc only used only one argument that we provided.
    the next stack entry is still there and that will mess up the remaining of the stack..
    so the next stack frame interprets it wrong again and do its own job untill it does something illegal as per the systems rules..

    correct me if I am wrong..

  2. #17
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    I tryed quickly and it seems that if I do not include stdlib.h I get:

    prova.c: In function 'main':
    prova.c:7: warning: incompatible implicit declaration of built-in function 'malloc'

    both with and without casting

  3. #18
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    Quote Originally Posted by sloppy View Post
    Just another question. From the faq:

    "...There is nothing wrong with this except in the event that stdlib.h, the header which declares malloc, is not included. If the return of malloc is cast then the error which would be flagged is hidden..."

    why doesn't the compiler give an error if I do not include stdlib.h? Shouldn't it complain about not knowing funcion "malloc"?
    ya.. it does but not an error it will gives a warning..
    as malloc is built-in function.. and its the same for all the built-in functions.

    but if you use a different function that is not built in it will throw error and not warning

    I think this is for the ease of programming that built-in functions are trivial to declare until something else is explicitly stated to be the overriding function of these built-ins.
    I dont know what the technical reason is..

  4. #19
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    @sloppy
    Which compiler, and what command line did you use?

    > but the problem is that malloc only used only one argument that we provided.
    > the next stack entry is still there and that will mess up the remaining of the stack..
    In C, it is the caller's responsibility to clean up the space used by parameters. The caller will know two values were pushed, and therefore knows to pop off two values.

    > Still if I make my own malloc and call it in my program without prototyping
    > and in the end if I link it with the libc malloc.. that should work and malloc code will be generated.
    Only if you ignore what matsp wrote in post 11.
    The real malloc returns a pointer, but in the absence of any prototype your code will think malloc will return an int. Even if you assume that the same register is used, you're also assuming that int and pointer have the same size.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sloppy View Post
    Just another question. From the faq:

    "...There is nothing wrong with this except in the event that stdlib.h, the header which declares malloc, is not included. If the return of malloc is cast then the error which would be flagged is hidden..."

    why doesn't the compiler give an error if I do not include stdlib.h? Shouldn't it complain about not knowing funcion "malloc"?
    Most compilers probably only give a warning about using a function without prototype [and there's of course no guarantee that there's no other header file that drags in the appropriate header - e.g. stdio.h may drag in stdlib.h by "fluke" - and then it stops working when you move to a different system where stdio.h doesn't drag in stdlib.h].

    If you use gcc , add -Wall. In Visual Studio, you should use -W4 or -W3.

    --
    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.

  6. #21
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    @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.

  7. #22
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by matsp View Post
    ...
    If you use gcc , add -Wall. In Visual Studio, you should use -W4 or -W3.
    ...
    Mats
    I use gcc. What I am saying is that I get the same warning both casting or not casting (adding -Wall doesn't give me error... just warning). Son I don't think (***at least on gcc4***) that it may

    "...resulting in a difficult to find bug..." (from faq)

  8. #23
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by sloppy View Post
    why doesn't the compiler give an error if I do not include stdlib.h? Shouldn't it complain about not knowing funcion "malloc"?
    The compiler doesn't need to "know" about a function in order to let you call it. If there is no function prototype, the compiler ASSUMES that the prototype is:

    Code:
    int func()
    In other words, it assumes the function takes any and all parameters (meaning the compiler will not do any argument type checking for you), and it assumes that it returns an int.

    The compiler will usually print a warning when you call a function that hasn't been prototyped. But it won't stop you from doing it.

    Because malloc() actually returns a void *, not an int, this means that if you fail to include stdlib.h, you will have to explicitly cast the result. If you explicitly cast anyway, this can mask the fact that you forgot to include stdlib.h.

    It has nothing to do with malloc() being a "built-in" or anything like that. It's like this for all functions.

  9. #24
    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.

  10. #25
    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

  11. #26
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #27
    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)

  13. #28
    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’

  14. #29
    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 11:12 AM.

  15. #30
    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

Page 2 of 2 FirstFirst 12
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, 11:27 AM
  3. Valgrind says I suck at memory management :)
    By carrotcake1029 in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 07:10 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21