I am wondering if it is possible to delete, that is remove from use, a C variable, say, for example, once the variable is used once?
I was reflecting some time ago on this subject, and one question arose in my mind.
C has many data types and we can create a variable of data type, say integer by the simple code
int i;
Once we have created a variable in this manner, how do we 'delete' it if we do not require it in future use?
I searched on the net, but did not find any C command that does this. By 'k is deleted', I mean 'k has ceased to exist'. I mean, once we do not need the variable, it is a waste, and should be removed.
C does provide the free()
function, but it only works on memory allocated using calloc(), malloc()
, or realloc()
.
So how does one remove, say, an int
variable, once it has been used?
You don't. The object's lifetime ends once it goes out of scope (it is known as an automatic object, because it is automatically cleaned up).
e.g.
void foo() {
int j = 3;
while (blah) {
int i = 5;
}
// i no longer exists
}
// j no longer exists