function overloading in C

This is a discussion on function overloading in C within the C Programming forums, part of the General Programming Boards category; how does printf work internally inside C? it is evident that printf funtions can have any number of arguments. does ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    7

    Lightbulb function overloading in C

    how does printf work internally inside C? it is evident that printf funtions can have any number of arguments. does c support function overloading?? if yes how

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    no, it does not support function overloading. It puts all of the values at the end of the argument list into one list.
    Last edited by Polymorphic OOP; 12-12-2002 at 10:06 PM.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    7
    any idea on how it does that? coz i am trying to implement my own printf function.

  4. #4

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    7
    Thnx a lot...!! It really helped

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Do you guys think it's a blessing or not that C does not support function overloading? Sometimes I find that it could make things alot easier, but then I also think it's good, in some cases, to make a function that says in it's name what types are in use, like addIntToList, and addStringToList, etc etc.

    What are your thoughts?

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    doesn't dmalloc overload the malloc and free functions in c?

    i think by placing the header file at the end of the includes as a specified library. just like local variables take precidence over global variables of the same name.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    doesn't dmalloc overload the malloc and free functions in c?
    No, function overloading doesn't exist in C

    > What are your thoughts?
    I think this has already been done, and the result is C++
    I wasn't asking if it has been done before or not, I was asking if thought it was good that it is not in C or not.

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >Do you guys think it's a blessing or not that C does not support
    >function overloading?

    I don't think it is a blessing. One of the problems with not being able to have things like function overloading is that you'll have a lot of functions like the ones you already mentioned, addIntToList and addStringToList, functions which do almost the same, but only the types on which they work are different. Such namespace pollution could be avoided by function overloading.

    Or, you could make use of void-pointers. I recently saw an implementation by someone using this struct:

    Code:
    struct node
    {
        void *node1;
        void *node2;
        void *node3;
    };
    He used it to implement many datastructures, like trees, graphs and lists.

    Another implementation I have seen some time ago was by using typedef. The programmer used defines in his code and if other types were needed in some function, he changed the define.
    Like:

    Code:
    #define my_type int
    
    my_type *add_to_list (my_type var)

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    With function overloading you still have to write all the functions though, you just have them all the same name.

  11. #11
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    not nessecarily; templating comes in there.
    hello, internet!

  12. #12
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    >>With function overloading you still have to write all the functions though, you just have them all the same name.

    by this definition, dmalloc overloads the malloc and free functions in c
    Last edited by rotis23; 12-13-2002 at 05:30 PM.

  13. #13
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    no, it doesn't. There is no function overloading in C. Post up a quick example of dmalloc and explain why you think it's overloading. If it has a different function name, then you aren't overloading it.

  14. #14
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    ok, by ortbitz previous definition

    consider the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
     char *test;
    
     test = malloc(sizeof(char) * 15)
     strcpy(test,"this is a test");
     printf("test : %s\n");
     free(test);
    }
    installing dmalloc and inserting the line below other includes:
    Code:
    #include "dmalloc.h"
    the dmalloc library's own dmalloc and free functions are called instead of those in the stdlib.h library.

  15. #15
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    2 things -- first that wouldn't be overloading even if it were true because overloading is when you have 2 different versions of the function that take different arguments

    So right off the bat, it's not overloading.

    As for what IS happening, here's the most likely possibility

    They simply just defined their own versions of the function. There's nothing wrong with that. If that is what they did, then try this:

    #include BOTH stdlib.h and dmalloc.h If you get an error saying that malloc and free were defined twice, then all they did was provide theiur own definitions of the function.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM

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