Size of a function pointer?

This is a discussion on Size of a function pointer? within the C Programming forums, part of the General Programming Boards category; What is the size of a general function pointer? The function it is pointing to returns void. Note: I want ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    51

    Size of a function pointer?

    What is the size of a general function pointer? The function it is pointing to returns void.

    Note: I want to put the function pointer in a 2-d array of chars, so I want to make sure it will be able to fit in there.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, putting function pointers in an array of char would be a rather silly thing to do, as it would make much more sense to have an array of function pointers - that way you don't get problems with things that could trip you up. Such as alignment of function pointers that is different from char alignment on certain machines.

    However, if you insist of doing silly things, sizeof() will tell you what the size is of a function pointer. Generally it's 1, 2, 4, 8 or 16 bytes - but there's no guarantee for any of those numbers.

    Can you explain a bit more why you want to put function pointers in a char-array - it may be that we can come up with a better solution?

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

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    The sizeof operator can tell you that and it would be either 4 or 8 bytes depending on whether you're using the ILP32 or LP64 model.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    If you got a function pointer like
    Code:
    void (*printhello)(void);
    then the size of it is
    Code:
    sizeof(printhello)
    .
    If you have multiple function pointers than the array should be the size of the biggest function pointer.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    The sizeof operator can tell you that and it would be either 4 or 8 bytes depending on whether you're using the ILP32 or LP64 model.
    Correct, however, the range of the answer is not limited to 4 or 8 - there are several other possible options - for example we could have a 32-bit segmented X86 model (I know of one compiler that can/could produce such code), in which case a function pointer is 6 bytes (but possibly aligned to 8 bytes), just as an example.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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