Results 1 to 13 of 13

Thread: Function pointers...

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Function pointers...

    Can anyone explain the usefullness of function pointers? I understand the point when it comes to callbacks, but is there any other good use for using them? Is there less overhead with them or something?

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    More overhead, actually (you have to read the pointer value from memory rather than it being written directly by the compiler).

    Other than callbacks, no, there's not much use for them. But when you need them, they're really good.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    jim mcnamara
    Guest
    Other than callbacks:
    A lot of standard C library routines use them. qsort() for instance.
    They allow a kind of generalization you cannot get any other way.

    They are also used extensively when writing parsers. OS functions also use them - those numbers you see in aliases in API declarations in VB are offsets into a masthead (array) of function pointers.

    -- This is not in defense of them 'cause they can be a colossal pain to work with.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    As an aside to Jim's post...

    If you're using C++ you can avoid using them in some situations by using functors/function objects. I'll try and find an example if I can...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Junior Member
    Join Date
    Jul 2002
    Posts
    16
    I would like to ask ( as this thread is on pointers ) that where do we require the use of linklist?

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    In a lot of places.

    If you need to insert into the middle of a sequence a lot, then lists are good because you only need to "relink" the surrounding nodes. For a flat array you have to copy everything after that point, and perhaps *everything*.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    DaoK
    Guest
    In "Sams teach yourself C++ in 21 days" it have a chapter (the chapter 13) on linked list and it's so hard to understand Sam's example that I have skipped that section. Linked list is really important to understand or not ? Because if it's important I will spend time on that but if not I will simply continue to learn something other.

  8. #8
    Junior Member
    Join Date
    Jul 2002
    Posts
    16
    Hi DaoK,

    Linklist , I agree, are very difficult topics to understand .But once you get a strong hold of pointers and are clear with the idea of passing arguments by reference....then they become little easier to understand.
    As far as their importanceand usage is concerned you may get some idea from parksie reply.
    I studied linklist from Schaum' Series the book called 'Programming in C'.Its really a good book and I use it frequently.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    as parksie said insertion into a linked list takes a constant time, and is preferable when you need to throw in and remove elements randomly in the list. sometimes you want to store dynamic length items which isn't really possible in an array either. The drawbacks are that Doublelinked lists requires 8 extra bytes per element while Singlelinked lists would require 4, and random access is linear, unless you use an iterator to work on a range of elements.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Junior Member
    Join Date
    Jul 2002
    Posts
    16
    I have read sumwhere that an alternative to linklist and array is map .It provides power of both i.e array and linklist.But I don't know much about them. Any links explaining them?
    As far as I know they are a part of STL.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You probably want deque. It's like vector, but "blocked", so you have lots of different contiguous segments which are linked together.

    FYI, deque is usually the underlying medium to set/map/etc.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12
    Lively Member
    Join Date
    Jun 2002
    Posts
    81
    linked list is pretty easy to understand...
    you got variable - lets call it a link (defined by struct)
    link.next is a pointer which will store the value of the next link.
    link.data is the data the list holds (it up to you what you'll save there)
    you start with only one link at the begining, and its .next pointer has NULL value, when you want to add another link you just use malloc. new or whatever and the returned value (which is the address of the new link) is assigned to the .next of the previouse link.
    for example:
    you decalre my_link and you got:
    (my_link)->
    then you add the next one and you get:
    (my_link)->(2nd link)->
    then you add the next one...

    the use for such a list as many of the above people said is changing the size of it at run time, the add/remove of first link is a constant time (and if you use another pointer to the last link its constant there too), so its best for pipe, FIFO, queue, or whatever you want to call it, it also is the best for stack (LIFO)
    and the only drawback is the linear access time... (which make you need binary trees when you need a structure with the lists functionality and fast search ability)

    list is also the best way to avoid recurtion (which is the most memory consuming thing you can program ;-))

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Other uses for function pointers:
    Polymorphism (vtables) - although you don't notice, those are function pointers. Same goes for COM.
    One-time decisions: used in DX. Look at this:
    Code:
    // code to blit an image:
    bool blit(...)
    {
    if(card_supports_hardware_blitting)
      hardware_blit(...);
    else
      software_blit(...);
    }
    // code to blit 5 images:
    blit(img1, surf, x1, y1);
    blit(img2, surf, x2, y2);
    blit(img3, surf, x3, y3);
    blit(img4, surf, x4, y4);
    blit(img5, surf, x5, y5);
    there are many ifs there, which is SLOW!
    now function pointers:
    Code:
    typedef bool (*BLIT)(...);
    BLIT blit;
    void init()
    {
      if(card_supports_hardware_blitting)
        blit = hardware_blit;
      else
        blit = software_blit;
    }
    This only requires one if in the whole app, at a non-time-critical time.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width