0

I just wanted to know the various reasons that might affect the size of a function pointer from memory perspective.

I'd like to emphasize that my question isn't about how functions pointers could be used and how they could be type-cast. It's just about if I declare a function pointer like

 int (*fptr)(int, int);

and create a variable of it

fptr fun1;

what is the size I'm expected to get if I run it on a 32/64 bit machine?

Is it possible to determine the size of a function pointer simply by looking at its declaration, like I could tell a void pointer would size 4 bytes on a 32-bit machine.

rango
  • 311
  • 1
  • 13
  • 2
    Maybe you should emphasize the problem you are actually having and trying to solve? Please read about [the XY problem](http://xyproblem.info/). – Some programmer dude Jul 06 '16 at 16:44
  • only depends on target CPU architecture - 32 or 64 bits (on some older systems it was 20, 16) – mvidelgauz Jul 06 '16 at 16:46
  • it's the same like your void pointer - 32 or 64 bits depending on for what system your are compiling - can be determined at compile time. There are pre-defined compiler `#define`s for it. What compiler are you using? – mvidelgauz Jul 06 '16 at 17:00
  • 2
    Are you familiar with `sizeof` or perhaps `PTRDIFF_MAX`? – 2501 Jul 06 '16 at 17:00
  • Related: [What is guaranteed about the size of a function pointer?](http://stackoverflow.com/questions/3941793/what-is-guaranteed-about-the-size-of-a-function-pointer). – Martin R Jul 06 '16 at 17:06
  • 1
    Relevant: http://stackoverflow.com/questions/1473935/can-the-size-of-pointers-vary-between-data-and-function-pointers and http://stackoverflow.com/questions/916051/are-there-any-platforms-where-pointers-to-different-types-have-different-sizes – Alok-- Jul 06 '16 at 17:07
  • Why do you need to know this? – dbush Jul 06 '16 at 17:15
  • @mvidelgauz gcc could you please provide any refernce to support your answer. – rango Jul 06 '16 at 17:21
  • If you work in IBM iSeries machines, I believe you'll find that function pointers are 128-bit values, even though data pointers are 64-bit values. Odd modes for Intel 80286 can have different sizes for data and function pointers in some modes (small data, large code; or large data, small code; etc.) – Jonathan Leffler Jul 06 '16 at 17:21
  • @2501 What has it got to do with the question asked? sizeof can specify the size of function pointer after I run the code. Is it possible to tell the size without running the code? – rango Jul 06 '16 at 17:22
  • @Martin R I've already read the link and the question I've asked is different – rango Jul 06 '16 at 17:22
  • 1
    @rango: you can look at the compiler manual — or you can run code that evaluates `sizeof(void (*)(void))` or similar. Main two choices. The other one is hear-say from the programmers working on the machine too, but they're apt to have slightly blinkered perspectives. – Jonathan Leffler Jul 06 '16 at 17:23
  • @JonathanLeffler Do you mean to say that they are always fixed for a fixed architecture type irrespective of the arguments or return type passed? – rango Jul 06 '16 at 17:23
  • It's a reasonable inference. A pointer to function can be converted from one type to another type and then back again without loss of information. ISO/IEC 9899:2011 §6.2.3.2 ¶8 _A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer. If a converted pointer is used to call a function whose type is not compatible with the referenced type, the behavior is undefined._ – Jonathan Leffler Jul 06 '16 at 17:26

3 Answers3

2

Rango, look here:

__SIZEOF_POINTER__ // Pre-defined GCC macro

and for MS Visual C/C++ compiler you can check macro _WIN64, _M_IX86 and others that indicate target processor architecture. Full list here.

P.S. After some thinking I have to admit that my statement that function pointer will have the same size as void* was wrong (I can imagine architectures where memory for data and for code may be addressed differently). Anyway, as for the question "Is it possible to determine the size of a function pointer" and taking into account clarification I got in the comment that OP works with gcc, __SIZEOF_POINTER__ may serve as an answer. Although answer provided by @2501 in their comment is much better - sizezeof(fptr) should work with any compiler

mvidelgauz
  • 2,176
  • 1
  • 16
  • 23
  • That's called a 'link-only' answer and isn't popular. And `__SIZEOF_POINTER__` is a GCC-specific feature; it is not a general feature of standard C. – Jonathan Leffler Jul 06 '16 at 17:27
  • @JonathanLeffler I changed to line of code and link – mvidelgauz Jul 06 '16 at 17:28
  • Agreed — you changed it. You didn't explain what it does, what it's for, or what its limitations are. (And, JFTR, not my down-vote — yet, at any rate.) – Jonathan Leffler Jul 06 '16 at 17:29
  • @JonathanLeffler Please read comment, OP asked for GCC – mvidelgauz Jul 06 '16 at 17:29
  • 1
    The question doesn't mention GCC. If the OP commented about GCC (not seen it yet, but I assume it is there), then the OP needs to update the question to say so (tag and content). – Jonathan Leffler Jul 06 '16 at 17:30
  • @mvidelgauz http://stackoverflow.com/questions/3941793/what-is-guaranteed-about-the-size-of-a-function-pointer The top answer states that "no guarantee that a void * can hold a function pointer. " – rango Jul 06 '16 at 17:31
  • JFTR I don't care about down-votes, I think OP got their answer - that what is important – mvidelgauz Jul 06 '16 at 17:32
  • 1
    @mvidelgauz I stated that I used gcc. I didn't intend this question to be gcc specific. Apologies if it meant to you that way. – rango Jul 06 '16 at 17:34
  • @rango you asked about **size of pointer**, where does my answer imply that void* can hold function pointer? – mvidelgauz Jul 06 '16 at 17:34
  • @mvidelgauz Even if you meant to imply that __SIZE_OF_POINTER__ is equal to number of bytes of void *, its gcc specific. For other compilers it's not always true. http://stackoverflow.com/questions/1473935/can-the-size-of-pointers-vary-between-data-and-function-pointers – rango Jul 06 '16 at 17:38
1

In any one program, all function pointers in C have the same size. A function pointer need not have the same size as void * according to the C standard. For that reason, you can't portably assign a function pointer to void *. However, Posix in effect does require that the two have the same size, else dlopen(2) wouldn't work.

what is the size I'm expected to get

That depends on what you expect? Pointer size -- data or function -- is up to the compiler (and its options). If you're programming on a popular, general-purpose platform, likely pointer size will be sizeof(size_t). But if you're doing something specialized, the only safe answer is RTFM.

James K. Lowden
  • 7,574
  • 1
  • 16
  • 31
0

A pointer has only one size. It does not matter what it points to. It is large enough to hold the address of a location in the process's virtual memory that can be accessed. This is typically 64 bits (8 bytes) on current CPUs and 32 bits on older ones.

  • 2
    *A pointer has only one size.* [No.](http://stackoverflow.com/questions/916051/are-there-any-platforms-where-pointers-to-different-types-have-different-sizes) – Andrew Henle Jul 06 '16 at 17:19
  • @Bruce Please read the following post. http://stackoverflow.com/questions/1473935/can-the-size-of-pointers-vary-between-data-and-function-pointers – rango Jul 06 '16 at 17:25