6

Since memory is already allocated on (array[0] = value;), I just want to change its pointing key value. Such as array[1] will allow me to access value now.

Is it possible to change the key of already mapped memory?

mapping(uint => uint) array;
uint value   = 10;

array[0] = value;              //memory is allocated, pointed by [0]. 

array[1] = array[0]'s_address; //I do want [1] to point same memory location as [0] without creating new memory space. 
//or
uint value_addr;
assembly { value_addr := value }
array[1] -> value_addr; //if possible, at this stage array[1] should also point to the value's memory space.

array[0] -> NULL; //`array[0]` should point empty memory space.

As a result, when I make a change on array[1] it should also make change on the array[0].

Thank you for your valuable time and help.

1

Just realized that I answered the wrong question.

What you're suggesting should work. The only real concern is that you might not understand the semantics of how some of this works.

uint value = 10; mapping(uint => uint) public array;

array[0] = value; // Either of the following will result in what you want because // ultimately you aren't pointing to a memory location, but assigning // a value. array[1] = array[0]; array[1] = value; // In order to "unassign" the mapping value at the previous // index, simply change the value of the mapping to its unassigned value array[0] = 0;


I'm not sure if this is exactly what you want, but you can work backwards to get specifically what you need with this example.

struct Meta { string data; uint64 timestamp; } public uint256 meta_count; mapping (bytes32 => Meta) meta; mapping (uint256 => bytes32) history; function create(bytes32 id, string data) { meta[id] = Meta( { data: data, timestamp: uint64(now) }); history[meta_count] = id; meta_count += 1; } function get_data_by_id(bytes32 id) constant returns(string data) { return meta[id].data; } function get_data_by_index(uint256 index) constant returns(string data) { return meta[history[index]].data; }

Basically my workaround is to just have mappings that point to each other instead of storing the same data twice. As long as you can always convert into the base mapping value it doesn't make a difference to Solidity and you don't have to pay twice the cost for storage.

  • Thanks mate, sorry but I did not get what your are suggesting. Yep it does works but array[1] = array[0] allocates additional memory space for array[1] as well which I don't want. @cryptihkal – alper Feb 8 '17 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.