I am learning function pointers,I understand that we can point to functions using function pointers.Then I assume that they stay in memory.Do they stay in stack or heap?Can we calculate the size of them?
-
what prompted you to choose such a user name?why didn't with your own/more simple name? – mawia Jun 10 '12 at 09:48
-
2@mawia Are you having a laugh?? They're the default user-names if you don't choose one. – Jun 10 '12 at 10:20
-
1@mawia I thought that number is cool one,so continued using it. – Vaccum Jun 12 '12 at 08:37
-
Use `$ nm filename.o` or `$ nm filename.obj` then look at "text" section which is Hex... – Yousha Aleayoub Mar 12 '18 at 11:11
9 Answers
The space for code is statically allocated by the linker when you build the code. In the case where your code is loaded by an operating system, the OS loader requests that memory from the OS and the code is loaded into it. Similarly static data as its name suggests is allocated at this time, as is an initial stack (though further stacks may be created if additional threads are created).
With respect to determining the size of a function, this information is known to the linker, and in most tool-chains the linker can create a map file that includes the size and location of all static memory objects (i.e. those not instantiated at run-time on the stack or heap).
There is no guaranteed way of determining the size of a function at run-time (and little reason to do so) however if you assume that the linker located functions that are adjacent in the source code sequentially in memory, then the following may give an indication of the size of a function:
int first_function()
{
...
}
void second_function( int arg )
{
...
}
int main( void )
{
int first_function_length = (int)second_function - (int)first_function ;
int second_function_length = (int)main - (int)second_function ;
}
However YMMV; I tried this in VC++ and it only gave valid results in a "Release" build; the results for a "Debug" build made no real sense. I suggest that the exercise is for interest only and has no practical use.
Another way of observing the size of your code of course is to look at the disassembly of the code in your debugger for example.

- 88,407
- 13
- 85
- 165
Functions are part of text segment (which may or may not be 'heap') or its equivalent for the architecture you use. There's no data past compilation regarding their size, at most you can get their entry point from symbol table (which doesn't have to be available). So you can't calculate their size in practice on most C environments you'll encounter.

- 1,179
- 8
- 17
-
Sometimes, executable code can be on heap also (but those are code generated during execution of the program). – nhahtdh Jun 10 '12 at 07:10
-
@nhahtdh Yes, but that's outside the scope of C, being more of a compiler/runtime system environment. I'm pretty sure Zeta-C as well as the various JVM-based C implementations could calculate function size :) – p_l Jun 10 '12 at 07:25
They're (normally) separate from either the stack or heap.
There are ways to find their size, but none of them is even close to portable. If you think you need/want to know the size, chances are pretty good that you're doing something you probably ought to avoid.

- 476,176
- 80
- 629
- 1,111
-
But how will you find the size of a function? OR more importantly how do you define 'size of a function'? If there is anything like the size of the function, it should be its stack fram size. Do you agree with this? – mawia Jun 10 '12 at 09:51
-
@mawia: I don't know what he means for sure. I'd have guessed at the size of the code, which you can usually persuade the linker to tell you (e.g., with a map file). – Jerry Coffin Jun 10 '12 at 16:32
-
i tried and found the size of a function 1 , everytime i got the same result (1) by changing the return type, number of local variables etc .But Answer is still unknown that why does sizeof operator always return 1 when sizeof operator is applied to a function ? – Abhishek Jaiswal Apr 13 '21 at 06:57
-
@AbhishekJaiswal: I'd guess because you're using a non-conforming compiler. gcc, for one example, treats sizes of a number of things as 1, even though a conforming compiler should reject code that tries to get the sizes of those items. For example: `void *x; std::cout << sizeof(*x); ` shouldn't compile--but with gcc it'll compile and print out `1`. – Jerry Coffin Apr 13 '21 at 07:19
-
can you elaborate these two words conforming and non-conforming compiler please?? i did google but didn't find anything related to these two words.. – Abhishek Jaiswal Apr 13 '21 at 09:04
There's an interesting way to discover the size of the function.
#define RETN_empty 0xc3
#define RETN_var 0xc2
typedef unsigned char BYTE;
size_t FunctionSize(void* Func_addr) {
BYTE* Addr = (BYTE*)Func_addr;
size_t function_sz = 0;
size_t instructions_qt = 0;
while(*Addr != (BYTE)RETN_empty && *Addr != (BYTE)RETN_var) {
size_t inst_sz = InstructionLength((BYTE*)Addr);
function_sz += inst_sz;
Addr += inst_sz;
++instructions_qt;
}
return function_sz + 1;
}
But you need a function that returns the size of the instruction. You can find a function that finds the Instruction Length here: Get size of assembly instructions. This function basically keeps checking the instructions of the function until it finds the instruction to return (RETN)[ 0xc3, 0xc2], and returns the size of the function.

- 51
- 4
-
1Nice, but I don't think there's a rule that says return instructions can never be in the middle of a function. – Petr Skocik May 05 '22 at 16:45
To make it simple, functions usually don't go into the stack or the heap because they are meant to be read-only data, whereas stack and heap are read-write memories.
Do you really need to know its size at runtime? If no, you can get it by a simple objdump -t -i .text a.out
where a.out is the name of your binary. The .text
is where the linker puts the code, and the loader could choose to make this memory read-only (or even just execute-only). If yes, as it has been replied in previous posts, there are ways to do it, but it's tricky and non-portable... Clifford gave the most straightforward solution, but the linker rarely puts function in such a sequential manner into the final binary. Another solution is to define sections in your linker script with pragmas, and reserve a storage for a global variable which will be filled by the linker with the SIZEOF(...) section containing your function. It's linker dependent and not all linkers provide this function.

- 4,095
- 1
- 26
- 27
As has been said above, function sizes are generated by the compiler at compile time, and all sizes are known to the linker at link time. If you absolutely have to, you can make the linker kick out a map file containing the starting address, the size, and of course the name. You can then parse this at runtime in your code. But I don't think there's a portable, reliable way to calculate them at runtime without overstepping the bounds of C.
The linux kernel makes similar use of this for run-time profiling.

- 1,675
- 13
- 20
C has no garbage collector. Having a pointer to something doesn't make it stay in memory.
Functions are always in memory, whether or not you use them, whether or not you keep a pointer to them.
Dynamically allocated memory can be freed, but it has nothing to do with keeping a pointer to it. You shouldn't keep pointer to memory you have freed, and you should free it before losing the pointer to it, but the language doesn't do it automatically.

- 16,023
- 3
- 35
- 65
If there is anything like the size of the function it should be its STACK FRAME SIZE. Or better still please try to contemplate what exactly, according to you, should be the size of a function? Do you mean its static size, that is the size of all its opcode when it is loaded into memory?If that is what you mean, then I dont see their is any language provided feature to find that out.May be you look for some hack.There can be plenty.But I haven't tried that.

- 9,169
- 14
- 48
- 57
#include<stdio.h>
int main(){
void demo();
int demo2();
void (*fun)();
fun = demo;
fun();
printf("\n%lu", sizeof(demo));
printf("\n%lu", sizeof(*fun));
printf("\n%lu", sizeof(fun));
printf("\n%lu", sizeof(demo2));
return 0;
}
void demo(){
printf("tired");
}
int demo2(){
printf("int type funciton\n");
return 1;
}
hope you will get your answer, all function stored somewhere
Here the output of the code

- 2,962
- 6
- 36
- 57

- 45
- 5