简体   繁体   中英

Clearing data at a memory address

I had a look around and couldn't find much information on how to do this, but how can I clear the memory at a specific memory address?

Let's say I have a memory address value of: 0x12345 What code would I need to write to cleanup whatever is in that space?

Preferably C or C++

What code would I need to write to cleanup whatever is in that space?

It depends.

To understand what code you need to write, first you need to know (1) what kind of object is stored in that address. Then, you must decide (2) what "cleanup" means in the context of that object. Once you have answers to those questions, then you can know what code needs to be written.

Example: Memory address 0x12345 contains an object of type int , and I want to "cleanup" that memory by setting the value of that integer to 0. Then You could write:

auto ptr = (int*)0x12345;
*ptr = 0;

If you don't know the answer to the questions 1 and 2, then you simply cannot "cleanup".


Note using an integer as a memory address is very dubious. How do we know there is an int object in that memory address? Usually you get addresses of objects by using the addressof operator. In that case the pointer should already be of appropriate type, so casting would be needed.

sledgehammer answer to yr question (but I smell an XY problem)

unsigned char * p = (unsigned char*)0x12345;
*p = '\0'; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM