How can I store a value at a specific location in

2019-01-11 20:06发布

问题:

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.

回答1:

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


回答2:

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); 
}


回答3:

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).



回答4:

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