简体   繁体   English

清除内存地址中的数据

[英]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? 假设我的内存地址值为:0x12345我需要写什么代码来清理该空间中的内容?

Preferably C or C++ 最好是C或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. 要了解您需要编写什么代码,首先您需要知道(1)该地址中存储了哪种对象。 Then, you must decide (2) what "cleanup" means in the context of that object. 然后,您必须确定(2)在该对象的上下文中“清除”是什么意思。 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: 示例:内存地址0x12345包含一个int类型的对象,我想通过将该整数的值设置为0来“清理”该内存。然后可以编写:

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

If you don't know the answer to the questions 1 and 2, then you simply cannot "cleanup". 如果您不知道问题1和2的答案,那么您根本无法进行“清理”。


Note using an integer as a memory address is very dubious. 请注意,使用整数作为内存地址是非常可疑的。 How do we know there is an int object in that memory address? 我们如何知道该内存地址中有一个int对象? Usually you get addresses of objects by using the addressof operator. 通常,您使用addressof运算符获取对象的地址。 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) 大锤回答您的问题(但我闻到了XY问题)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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