Reputation: 16865
Is it possible to deallocate a function? Say you have a huge initialisation function that only gets run once, is it possible to deallocate it after you called it, and use that extra storage? I'm mainly interested in C, but feel free to post up for any other language if that language offers interesting behaviour.
I understand that functions aren't allocated on the heap and thus you probably cannot recycle their memory, but this does seem like a waste to me.
Upvotes: 1
Views: 124
Reputation: 396
With python you can create classes and functions during the lifetime of the program, and also remove them. You lose memory due to the fact that there's some boilerplate on top of the implementation language (cPython uses C) though, so it wouldn't be a particularly useful concept in this application
Upvotes: 1
Reputation: 65
You can define functions at runtime using JavaScript and assign them to variables. Later you can delete the function or just leave it to the garbage collector when defined as local variables.
var MyImplementation = {};
MyImplementation.test = function() {
console.log("hello");
};
MyImplementation.test();
delete MyImplementation.test;
MyImplementation.test();
This produces:
hello
TypeError: undefined is not a function
Upvotes: 0
Reputation: 1
You can do that with shared libs (eg dll). Use them and unload them...
Upvotes: 0
Reputation: 91119
There is no way to do so using the language features.
But depending on the system you work on, the OS might do that on its own by the normal memory management techniques. (But if so, then by "accident".)
Let me explain a bit: the program gets mapped into memory and run. Code which hasn't been used for a certain time will get thrown out of memory. Data is put into the swap area on disk in order to be able to "recycle" it later, but code is always present in the executable file and can be fetched from there if needed again.
Of course, this does not happen on all systems, but Linux and AFAIK Windows as well works this way.
Upvotes: 1