Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C is a function loaded into memory when it is first called or when the program starts? And can it be unloaded from memory?

Tags:

c

function

memory

For example:

If I have a function named void Does_Stuff(int arg) and call it in the main function, is void Does_Stuff loaded into memory ONLY when it is first called? Or is it loaded into memory during program initialization?

And after calling Does_Stuff in main, can I manually unload it from memory?

For reference the operating system I am running is Windows 7 and I am compiling with MinGW.

like image 413
getynge Avatar asked Dec 25 '22 15:12

getynge


1 Answers

In simple terms (with the usual depends-on-various-platform-things caveat), the code for your normal, global C function is "loaded into memory" at the time the program is loaded. You cannot request that it be "unloaded".

That said, as Hans mentions in a comment, the OS at a lower level is in charge of what bits of stuff are important enough to be present in physical RAM, and may choose to "page out" memory that isn't being used frequently. This isn't per-function, and has no knowledge of the structure of your code. So in that sense the function's code may happen at various times exist in actual RAM or not. But this is a level below the application's execution, where a C function is always "present and available".

like image 59
Ben Zotto Avatar answered May 19 '23 14:05

Ben Zotto