如何将值存储在内存中的特定位置?

发布于 2024-11-19 14:33:27 字数 529 浏览 2 评论 0原文

也许这是一个简单的问题,但我真的很想知道它。

如果我想将一个值(例如 int)存储在内存中(堆上)的特定地址,我该怎么做?

比如说,我想将 int 值 10 存储在 0x16 处。我想通过调用 new 或 malloc 来做到这一点: int *p=new int(10);然后我想将存储值的地址设置为0x16。起初我以为只是像 &p=0x16 这样的东西,但这不起作用。我需要这样做来在内存中的某个值(之前由 malloc 或 new 分配的内存空间)前面存储一些附加信息。

我正在使用 linux 和 C++(但 C 也可以)。

我想要实现的是:一个进程调用大小为 x 的 malloc,并且我想在分配的内存前面存储某个值(大小),以便稍后(调用 free 时)可以访问该大小。由于调用了 malloc,我知道操作系统为值分配空间的指针,我只想将分配的内存的大小存储在分配的内存前面的 4 个字节中。我所做的(在我编写的 malloc 挂钩中)是分配更多内存(通过内部 mallok 调用),但我还需要能够将此大小值存储在特定位置。

我感谢所有的帮助。

Maybe this is an easy question question but I would really like to know it for sure.

If I want to store a value, say an int, at a specific address in the memory (at the heap), how do I do it?

Say, I want to store the int value 10 at 0x16. I guess do so by calling new or malloc: int *p=new int(10); and then I want to set the address of the stored value to 0x16. At first I thought just something like &p=0x16 but this doesn't work. I need to do this to store some additional information in front of a certain value in the memory (that was previously assigned memory space by malloc or new).

I am using linux and C++ (but C would work as well).

What I want to achieve is: one process calls malloc with size x and I want to store a certain value (the size) in front of the allocated memory, so I can access the size later (when free is called). Since malloc was called, I know the pointer where the OS assigned space for the value and I just want to store the size of the assigned memory in the 4 bytes in front of the assigned memory. What I do (in the malloc hook that I wrote) is to assign more memory (by an internal mallok call) but I also need to be able to store this size value in the specific location.

I am thankful for all help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

━╋う一瞬間旳綻放 2024-11-26 14:33:28

您可以这样做:

*(int *)0x16 = 10;  // store int value 10 at address 0x16

请注意,这假设地址 0x16 是可写的 - 在大多数情况下这将生成异常。

通常,您只会对没有操作系统的嵌入式代码等执行此类操作,并且您需要写入特定的内存位置,例如寄存器、I/O 端口或特殊类型的内存(例如 NVRAM)。

您可以像这样定义这些特殊地址:

volatile uint8_t * const REG_1 = (uint8_t *) 0x1000;
volatile uint8_t * const REG_2 = (uint8_t *) 0x1001;
volatile uint8_t * const REG_3 = (uint8_t *) 0x1002;
volatile uint8_t * const REG_4 = (uint8_t *) 0x1003;

然后在您的代码中您可以像这样读取写入寄存器:

uint8_t reg1_val = *REG_1; // read value from register 1
*REG_2 = 0xff;             // write 0xff to register 2

You can do it like this:

*(int *)0x16 = 10;  // store int value 10 at address 0x16

Note that this assumes that address 0x16 is writeable - in most cases this will generate an exception.

Typically you will only ever do this kind of thing for embedded code etc where there is no OS and you need to write to specific memory locations such as registers, I/O ports or special types of memory (e.g. NVRAM).

You might define these special addresses something like this:

volatile uint8_t * const REG_1 = (uint8_t *) 0x1000;
volatile uint8_t * const REG_2 = (uint8_t *) 0x1001;
volatile uint8_t * const REG_3 = (uint8_t *) 0x1002;
volatile uint8_t * const REG_4 = (uint8_t *) 0x1003;

Then in your code you can read write registers like this:

uint8_t reg1_val = *REG_1; // read value from register 1
*REG_2 = 0xff;             // write 0xff to register 2
痴梦一场 2024-11-26 14:33:28

我相信实现你的目标的最好方法是实现你自己的 malloc,它将多分配 4 个字节并存储内存块的大小
喜欢:

void* mymalloc(int size)    
{
    char* ptr = malloc(size+sizeof(int));
    memcpy(ptr, &size, sizeof(int));
    return ptr+sizeof(int); 
}

I beleieve that the best way to achive your goal is implement your own malloc which will allocate 4 bytes more and store size of memory block
like:

void* mymalloc(int size)    
{
    char* ptr = malloc(size+sizeof(int));
    memcpy(ptr, &size, sizeof(int));
    return ptr+sizeof(int); 
}
笑看君怀她人 2024-11-26 14:33:28

我想要实现的是:一个进程调用大小为 x 的 malloc,并且我想在分配的内存前面存储某个值(大小),以便稍后(调用 free 时)可以访问该大小。由于调用了 malloc,我知道操作系统为值分配空间的指针,我只想将分配的内存的大小存储在分配的内存前面的 4 个字节中。

这是行不通的。您只能合法地写入由库分配给您的内存地址。在 C 中,这意味着 malloc 及其朋友。在 C++ 中,这还意味着 malloc(尽管在 C++ 中应该避免这种情况)和 new

任何尝试写入这些分配方案分配的显式空间之外的任何内存都会导致未定义的行为。这通常意味着“坏事可能会发生”。

例如,malloc 返回的地址之前的 4 个字节可能是堆的一部分。即,mallocfree 用于完成其工作的数据结构。通过写信给他们,你现在已经破坏了堆;现在,每次内存分配或释放都充满危险,并且可能会严重失败。

或者,地址之前的 4 个字节可能超出了虚拟地址空间。在这种情况下,操作系统将立即终止您的程序。这就是当出现“一般保护故障”或“分段故障”时会发生的情况。好消息是,这通常是立即发生的,因此您可以在调试器中看到它发生的位置。与堆损坏不同,除非您知道堆如何工作(或者没有堆调试工具),否则您无法判断出了什么问题。

What I want to achieve is: one process calls malloc with size x and I want to store a certain value (the size) in front of the allocated memory, so I can access the size later (when free is called). Since malloc was called, I know the pointer where the OS assigned space for the value and I just want to store the size of the assigned memory in the 4 bytes in front of the assigned memory.

That is so not going to work. You are only legally allowed to write to memory addresses that you have been assigned by your libraries. In C, that means malloc and its friends. In C++, that also means malloc (though you should avoid that in C++) and new.

Any attempt to write to any memory outside of the explicit space allocated by these allocation schemes results in undefined behavior. Which generally means "bad stuff can happen."

For example, the 4 bytes before an address returned by malloc may be part of the heap. That is, the data structures that malloc and free use to do their job. By writing to them, you have now corrupted the heap; every memory allocation or deallocation is now fraught with peril and can fail spectacularly.

Or, maybe the 4 bytes before the address is outside of your virtual address space. In which case, the OS will kill your program posthaste. That's what happens when you have a "general protection fault" or a "segmentation fault". The good news is that this is typically immediate, so you can see in a debugger where it happened. Unlike heap corruption, where you can't tell what's going wrong unless you know how your heap works (or without heap debugging tools).

绝情姑娘 2024-11-26 14:33:28

您可以使用 新展示位置

You can place a type at particular memory location on freestore(a.k.a heap) by using Placement New.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文