Skip to content

Instantly share code, notes, and snippets.

@maurelian maurelian/ArrayPops.sol Secret
Last active Mar 22, 2017

Embed
What would you like to do?
// fun with arrays
pragma solidity ^0.4.10;
contract ArrayPops {
// create some arrays to check out
uint[] public uint_arr;
string[] public string_arr;
bytes32[] public bytes32_arr;
function ArrayPops() {
// fill our arrays with arbitrary data
uint_arr = [1,2,3,4,5];
string_arr = ['one', 'two', 'three', 'four', 'five'];
bytes32_arr = [bytes32(0), bytes32(1), bytes32(2), bytes32(3), bytes32(4)];
}
// returns an empty value
function broken_pop_string () returns (string) {
string member = string_arr[string_arr.length-1];
string_arr.length--;
return member;
}
function pop_string () returns (string member) {
member = string_arr[string_arr.length-1];
string_arr.length--;
return member;
}
function pop_uint () returns (uint member) {
member = uint_arr[uint_arr.length-1];
uint_arr.length = uint_arr.length-1;
return member;
}
// not actually broken
function broken_pop_uint () returns (uint) {
uint member = uint_arr[uint_arr.length-1];
uint_arr.length = uint_arr.length-1;
return member;
}
function pop_bytes32 () returns (bytes32 member) {
member = bytes32_arr[bytes32_arr.length-1];
bytes32_arr.length = bytes32_arr.length-1;
return member;
}
// not actually broken
function broken_pop_bytes32 () returns (bytes32) {
bytes32 member = bytes32_arr[bytes32_arr.length-1];
bytes32_arr.length = bytes32_arr.length-1;
return member;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.