9

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.

Bree
  • 91
  • 1
  • 1
  • 3
  • 4
    you really really really should read some tutorials about pointers – Karoly Horvath Jul 12 '11 at 08:07
  • 8
    Want you want to do is not legal. Unless you understand the OS and memory allocator (which obviously you don't) then it is just going to crash or do something else unexpected. Tell us what are you really trying to achieve not what you think you need to do. – Martin York Jul 12 '11 at 08:13

4 Answers4

11

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
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • I'd use a `reinterpret_cast`, rather than a C style cast here. It's rather worth drawing attention to the low level, machine dependent nature of the code. – James Kanze Jul 12 '11 at 09:12
  • This is true of *some* embedded/direct memory systems (drivers, etc.). But I think the OP is not in such a situation -- they seemed confused on the concept of process memory. Thus I don't think this will actually help them (on linux you can't do this at all) – edA-qa mort-ora-y Jul 12 '11 at 09:19
  • @edA: you may be right - the question in its original form was rather vague/confused but the OP seems to have subsequently added more detail about what he wants to achieve. I'm not sure why my answer deserves a downvote though ? – Paul R Jul 12 '11 at 09:53
  • @James: the question is tagged as both C and C++ so I'm assuming lowest common denominator here. – Paul R Jul 12 '11 at 09:55
  • @Paul, I'm not the one who down-voted. From the full question I'm guessing they are just looking for shared memory. – edA-qa mort-ora-y Jul 12 '11 at 10:33
  • @Paul R I'd missed that. Yes, if he wants the code to work in C, he'd best avoid `reinterpret_cast`. – James Kanze Jul 12 '11 at 10:53
  • @edA: I'm not sure - from the added last paragraph it sounds like he wants to mess with the malloc heap internals, i.e. stash a size value immediately preceding a malloc'd block ? – Paul R Jul 12 '11 at 13:00
  • @Paul, I interpreted that to mean he wants to do his own memory size tracking for some other process to use it. I think we'll just have to remain confused. – edA-qa mort-ora-y Jul 12 '11 at 14:56
9

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); 
}
Oleg
  • 798
  • 3
  • 7
2

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

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
0

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

Alok Save
  • 202,538
  • 53
  • 430
  • 533