Reputation: 87
For a class I'm taking, we need to develop allocation utilities.
Safety aside, I will be using far pointers to receive and assign addresses to some structures. I am also not allowed to use "memalloc" in this assignment.
I referred to: Linked Lists in C without malloc to learn how to make a linked structure without using memalloc. However, I need to know how I can assign a new instantiation to a particular address. For example, if I have a starting pointer to address 0x5000, I would like to create my list so that I can use an incremental offset (let's say 256 for example) to start my next structure 256 bytes ahead (in terms of address) of my starting point.
This assignment is based on becoming familiar with segmentation and the x86 architecture.
Any help is appreciated!
Upvotes: 1
Views: 627
Reputation: 87
I just received an e-mail from my professor, and I'm not allowed to use the "new" operator. Just to double check with what you've told me, I may ask again with this example:
struct example{
int a;
struct example *next;
};
void *ptr;
ptr = 0x5000;
ptr = &((example)*ptr);
Or something along the lines of that?
Upvotes: 0
Reputation: 373082
Depending on what you want to do, you'll probably want to use a combination of the following tools:
Placement new: You can construct objects at specific memory locations by using the following syntax:
new (reinterpret_cast<void*>(0x500)) MyObject(/* ...ctor args... */);
This does not allocate any new memory; instead it just calls the constructor using the specified memory address as the receiver object. Be careful with alignment restrictions!
Custom allocators. You can define a new class along the lines of std::allocator<T>
that reserves memory in the spots you've indicated. You can then create STL containers that automatically use memory in the locations you'd like.
Upvotes: 3