Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find size of a function in C

Tags:

c

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?

like image 939
Vaccum Avatar asked Jun 10 '12 06:06

Vaccum


People also ask

What is the size of function in C?

The sizeof operator is the most common operator in C. It is a compile-time unary operator and used to compute the size of its operand. It returns the size of a variable. It can be applied to any data type, float type, pointer type variables.

What is the use of size () operator?

The sizeof operator applied to a type name yields the amount of memory that can be used by an object of that type, including any internal or trailing padding. The result is the total number of bytes in the array. For example, in an array with 10 elements, the size is equal to 10 times the size of a single element.

What is the size of function pointer?

A function-pointer is a 4-byte elementary item . Function-pointers have the same capabilities as procedure-pointers, but are 4 bytes in length instead of 8 bytes. Function-pointers are thus more easily interoperable with C function pointers.

What is sizeof return in C?

sizeof() operator is used in different way according to the operand type. 1. When operand is a Data Type. When sizeof() is used with the data types such as int, float, char… etc it simply returns the amount of memory is allocated to that data types.


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

like image 136
Clifford Avatar answered Nov 04 '22 16:11

Clifford


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.

like image 38
p_l Avatar answered Nov 04 '22 17:11

p_l