How to I remove a varible from memory? A friend and I were talking and he seems to think that just resetting it to "" would be enough. Is he correct? If not, how do I remove varibles when I'm done with them?

Dani AI

Generated

Short answer: no — setting a variable to "" (or assigning an empty value) does not remove its memory. That only changes the value or the pointer; it does not free heap storage or erase bytes that previously held data.

asked the right question; and pointed toward scope and sentinel values. Clarification: in C, automatic (stack) variables simply cease to be valid at the end of their scope and the stack memory is reused by the program. C has no destructors — that is a C++ feature (see C++ destructor semantics) (cppreference - destructor). Static or global data exists for the program lifetime and cannot be “freed” during runtime. Dynamically allocated memory (malloc/calloc/realloc) must be returned with free() or the program leaks memory (free(3) man page).

If memory contains sensitive data or you want the bytes cleared before returning them to the allocator, explicitly overwrite the buffer and then free it. Use platform-provided secure functions when available; explicit_bzero() or memset_s() resist compiler optimizations that might remove a plain memset used only for scrubbing (explicit_bzero(3)). Example pattern:

/* preferred when available */
explicit_bzero(p, len);
free(p);
p = NULL;

If explicit_bzero/memset_s are not available, use a volatile-writer loop to reduce the chance the compiler optimizes the wipe away. After free() set the pointer to NULL to avoid a dangling pointer and do not reuse freed memory. For leak detection and correctness in long-running processes, run Valgrind or AddressSanitizer to find forgotten frees and use-after-free bugs.

Recommended Answers

All 2 Replies

How to I remove a varible from memory? A friend and I were talking and he seems to think that just resetting it to "" would be enough. Is he correct? If not, how do I remove varibles when I'm done with them?

That is not the right way.....
We can't even apply delete to statically allocated objects.....but we can restrict their existence within scope so that whenevr the scope ends...destructors of the local objects are called automatically.
Example

int main()
{
int x=20;
{
   int y=10;
}//compiler automatically destroys variable after the scope..i.e y is destroyed
return 0;
}//x is also destroyed now

sunnypalsingh is right. Variables you declared will go away after you exit the scope they're declared in.

If you want to do something slightly different, where you treat the variable as deleted, you can set the variable to an unused value and keep going. A common example of this is to set an integer to a positive value if some condition is true and a negative value if false.

Some times, doing this is more useful than getting completely rid of the variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.