1

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!

Community
  • 1
  • 1
MHZ
  • 77
  • 1
  • 6

2 Answers2

3

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.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • Hi, thank you for your response. I need to clarify something. I am not allowed to use allocation utilities besides the one I am creating. So no new, delete, free, etc... What I am more so looking to be able to do is create a pointer, point to some address, then create variables and data structures at the pointed address referenced by my pointer. – MHZ Jan 19 '11 at 04:21
  • Let me follow this further with a question. Is it possible to de-refence an address and cast it as a data type? – MHZ Jan 19 '11 at 04:26
  • @user580887- Placement new is not a memory allocation technique; it does just what you've described. If you can point somewhere in memory, you can use placement new to construct an object at that location. It doesn't invoke the standard freestore allocator to get space and completely trusts that sufficient space exists. – templatetypedef Jan 19 '11 at 04:28
  • @user580887- Yes, you can do that, but the behavior is undefined unless you are just storing POD types (things without ctors, dtors, or copy functions). Using placement new allows you to do this while allowing for constructors and destructors that handle setup and cleanup. – templatetypedef Jan 19 '11 at 04:29
  • @user580887- I really don't know what to say if your professor says that placement new is illegal because it does allocation; that's just factually wrong. – templatetypedef Jan 19 '11 at 05:11
  • +1 for the awesomeness of this even though I'm pretty sure it is 100% irrelevant to his assignment. – MK. Jan 19 '11 at 05:21
0

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?

MHZ
  • 77
  • 1
  • 6
  • You will want to write `example* ptr = (example*)0x5000;` to get this working. This bypasses the conversion to `void*`, since, after all, what you're trying to get at the end of the day is a pointer to an `example` object. – templatetypedef Jan 19 '11 at 05:30
  • Thanks a lot for your help. Once I figure out how to vote up people, I will do so for you. – MHZ Jan 19 '11 at 05:33