I have a map that will store a string, and a corresponding address(memory) that contains a integer value or char value.
map<string,address> myMap; //What datatype would address be?
I'm not sure what datatype I would put here any help would be great
An address is stored in a compound type known as a pointer type.
In order to get the pointer type that points to an int
you use the '*' symbol:
int i; // integer
int* ip; // pointer to integer (stores the address of an int)
map<string, int*> myMap; // pointer type pointing to an int
So basically you want to map type generic addresses inside an unordered_map
. Then just define your own datatype which is able to manage it safely, something like:
class Address
{
public:
virtual uintptr_t getAddress() = 0;
virtual ~Address() { }
};
template<typename T>
class RealAddress : public Address
{
private:
T* address;
public:
RealAddress(T* address) : address(address) { }
uintptr_t getAddress() override { return address; }
}
std::unordered_map<std::string, std::unique_ptr<RealAddress>> labels;
labels["foo"] = std::unique_ptr<Address>(new RealAddress<int>(address));
This is just to give you the idea, it's unclear if you have your data section of the assebly stored in the program itself (so you really have an int*
or a char*
) or if you just need a numeric address which is then relocated inside your assembler.
You can enrich the behavior, use an enum class
or RTTI to distinguish between types and so on.
A really simpler solution would be to have something like
union Address
{
char* addressChar;
int* addressInt;
Address(int* addressInt) : addressInt(addressInt) { }
Address(char* addressChar): addressChar(addressChar) { }
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With