Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what data type is an Address?

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

like image 695
conterio Avatar asked Dec 11 '22 20:12

conterio


2 Answers

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
like image 183
Galik Avatar answered Dec 13 '22 09:12

Galik


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) { }
};
like image 22
Jack Avatar answered Dec 13 '22 09:12

Jack