Thread: Quick Question

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    Question Quick Question

    I was just wondering why, when I use the sizeof() function on a function name, it says the size is only one byte.

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    You can't simply get the size of your function by doing that. I'm not sure what's but I'm guessing it's giving you the size for the location where the function's address is being stored which should be 4 bytes.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    hmmm... my compiler says:
    Code:
    Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
    junk1.c 8: sizeof may not be applied to a function in function main
    *** 1 errors in Compile ***
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    MSVC++ returned 4 bytes when I passed in main(). Behavior must be undefined heheh.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>MSVC++ returned 4 bytes when I passed in main().
    Good old Microsoft..!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer
    >>MSVC++ returned 4 bytes when I passed in main().
    Good old Microsoft..!
    Well it should give you that. Think about it: sizeof( main )

    What exactly is the name of a function when used by itself? It's a pointer to that function. Thus, you could end up with the size of a pointer, which in any 32bit OS, is always 4 bytes.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Well it should give you that.
    No, it shouldn't. And thats already been proved by the fact the first three posters got three different results.

    WG14/N869 Committee Draft — January 18, 1999 81
    6.5.3.4 The sizeof operator
    Constraints
    1 The sizeof operator shall not be applied to an expression that has function type or an
    incomplete type, to the parenthesized name of such a type, or to an expression that
    designates a bit-field member.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    sizeof ((*funcptr)()) is ok
    size of (func) isnt
    hello, internet!

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer
    >>Well it should give you that.
    No, it shouldn't. And thats already been proved by the fact the first three posters got three different results.
    I didn't mean literally. I meant if you looked at it as to what it was applied to logicly. It was applied to the name of a function. Logicly then, since the name of a function itself, is a pointer to the function, it should give you the size of a function pointer.

    Thus, logicly, it should give you 4 on a 32bit compiler/OS.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM