0

Recently I posted a question: "Do functions occupy memory space?"
The best answer I got was:

Yes, the functions occupy space in memory but its size is entirely dependent on the function.

Now I am not asking about exact amount they occupy, but what in the function is occupying memory space?

for exmaple:-

void demo()
{ }

The above function compiles without any error, but this function is totally empty, it is not having even a return statement. So, what is occupying the space in the memory for this function?

Community
  • 1
  • 1
kevin gomes
  • 1,775
  • 5
  • 22
  • 30
  • 2
    A good compiler should optimize and remove this function even if you calls it. But suppose if you prints deom's address then function definition will exist in executable. – Grijesh Chauhan Feb 13 '14 at 15:12

4 Answers4

4

EDIT: Thank you to Pascal for his comment which is indeed correct! I have changed the answer as a result of this.


Using any optimization setting other than none, the compiler would optimize this function away if it was statically defined i.e., local to this compilation unit. With no optimizations turned on it might not. In that case the function prologue and epilogue, which are code that sets up the stack frame etc on entering the function, and destroys it on leaving, may still be emitted by the compiler.

As Pascal points out when it has external linkage this is not possible as the compiler doesn't generally "reach" across modules. However, in this case the linker should optimize it out if it detects that the function is not called.

Also it is completely possible to determine how much memory a function takes up. You can look at the elfdump/objdump, or similarly appropriate utility, for the object that contains the function :)


To answer your question

If the function is called, then what is occupying the memory space?

A function must be compiled into machine code. This is a set of instructions of architecture-dependent length/value etc that reside in memory. The computer fetches instructions from memory. Therefore your function must reside somewhere in memory for the CPU to be able to fetch it and then execute it. The C code your write is compiled into this machine code and it is this that takes up memory space...

Community
  • 1
  • 1
Jimbo
  • 4,352
  • 3
  • 27
  • 44
  • 3
    “Using any optimization setting other than none, the compiler should optimize this function away” Absolutely not. A function `demo` has external linkage. It cannot be removed by the compiler. – Pascal Cuoq Feb 13 '14 at 15:02
  • @Jimbo : If the function is called, then what is occupying the memory space? – kevin gomes Feb 13 '14 at 15:07
1

Compilers usually will skip this functions while optimization. So the answer is no memory will reserve for call to an empty function.

Ajay
  • 18,086
  • 12
  • 59
  • 105
dinomoth
  • 86
  • 6
1

If we ignore that the compiler may or may not optimize the function away, then there are many different kinds of memory occupied by a function.

Each time a function is called, it is likely that the program has to store away various CPU registers on the stack, registers such as the status/condition code register. Also, the program counter will be stored on the stack, or the CPU wouldn't know where to return to, once the function is done executing.

What kind of registers that are saved, and where, and how, depends on the system. The CPU likely does a lot of this automatically, upon encountering an "execute function" instruction in the program.

If there are any parameters or return values, they will also be stored on the stack/in registers. In that case, they may be saved by the caller or by the function, it is system specific and known as calling convention.

In addition to the above, the actual code that is the body of the function is of course stored somewhere too: in the program memory (often referred to as .text).

Lundin
  • 195,001
  • 40
  • 254
  • 396
1

Let's assume compiler doesn't omit out the given function, since it does nothing. And assume that compiler won't inline it either, since function is small. Your assertion that function doesn't do anything, doesn't call another function, no local variables, and not argument and return value - then why does it would require any memory?

First thing, the size occupied by function would be at two locations. One, at the generated binary (executable), and second, in the call stack of the thread which calls this function. An empty function would be sort of like a zero-byte file or a directory. Both seemingly doesn't occupy any storage in disk (since it is shown as '0 bytes'). But, they take some memory is the disk.

Analogy of empty file is not exactly same as an empty function, just for understanding purpose, I wrote. A function would be called by some function (caller), hence return-address of that function would be kept in call stack (of current thread). When function ceases to exist, that return address would be looked up, and ESP would point to that location for next instruction.

Calling conventions would also add up more instructions, for empty functions, to clean up the call stack. Pascal calling convention, for instance, would have some more instructions to clean up the call stack when function exits.

The bit-ness of the program (32-bit or 64-bit) would also change the size required for all this book keeping.

Ajay
  • 18,086
  • 12
  • 59
  • 105