la stessa struttura in array e mappature
Oct 06 2020
Diciamo che ho un file struct.
struct test {
uint x;
}
test[] all;
mappings(uint => test) map;
function bla(){
test memory structTest = test(5);
all.push(structTest);
map[10] = structTest;
}
Dopo il codice, verrà structTestsalvato in un posto e quindi il mio array e la mia mappa salveranno i riferimenti ad esso o verranno memorizzati separatamente in mappa e array?
In quest'ultimo caso, come posso creare struct e memorizzarlo in mapping e array in modo da non sprecare spazio?
Risposte
2 goodvibration Oct 06 2020 at 20:58
Una copia di structTestviene salvata separatamente nell'array e nella mappatura.
Per eliminare questa ridondanza, puoi mantenere un array di chiavi invece di valori:
uint[] all; // instead of `test[] all;`
mappings (uint => test) map;
function bla() {
test memory structTest = test(5);
all.push(10); // instead of `all.push(structTest);`
map[10] = structTest;
}