Solidity Tutorial – Data Location and Data Type

Solidity Tutorial - Data Location and Data Type
  1. Data Location

The Ethereum Virtual Machine has 4 areas where it can store items.

1.1 Memory which is used to hold temporary values. It is erased between (external) function calls and is cheaper to use.

ex: h(uint[] memory hArray) internal {}

1.2 Storage which is used to hold permanent values. All the contract state variables will be stored in storage. Every contract has its own storage and it is persistent between function calls and quite expensive to use.

ex: function g(uint[] storage gArray) internal {}

1.3 Calldata: All function calls use calldata, which includes function parameters. Calldata is read-only memory area

1.4 Stack which is used to hold small local variables. It is almost free to use, but can only hold a limited amount of values.

There are defaults for the storage location depending on which type of variable it concerns:

  • State variables are always in storage

Ex: uint[] storageArray;

  • Function arguments are always in memory
  • Local variables of struct, array or mapping type reference storage by default
  • Local variables of value type (i.e. neither array, nor struct nor mapping) are stored in the stack

2. Data Type

2.1 Bool: It can hold either true or false

EX: bool isComplete = false;

2.2 Signed Integer: they are used to hold signed integers of 8 bits, 16 bits … 256 bits. EX: int8, int16 … int256
Ex: int32 mediumNegativeNumber = -450;

2.3 Unsigned Integer: They are used to hold unsigned integers of 8 bits, 16 bits, 24 bits … 256 bits . Ex: uint8, uint16, uint24 … uint256
Ex: uint16 smallPositiveNumber = 678;

2.4 Strings: In Solidity, there are two ways to create strings: using bytes and string. Bytes is used to create a raw string, whereas string is used to create a UTF-8 string.

string webname = “biastek.com”;

2.5 Var: var is decided dynamically depending on the first value assigned to it. Once a value is assigned, the type is fixed, so if you assign another type to it, it will cause type conversion.

2.6 Address

The length of Ethereum address is 20 bytes (160 bit)

address ownerAddress = 0x10abb5EfEcdC09581f8b7cb95791FE2936790b4E;
Functions provided by the address type:

+ send(): To send Ether in Wei—If the transaction fails on the receiving side, a value of false is returned to the sender but the payment isn’t reverted

if (!destinationAddress.send(10))
revert();

Another way

require(destinationAddress.send(10));

+ call(): To invoke a function on the target contract associated with the address (the target account is assumed to be a contract)

destinationContractAddress.call(“contractName”,
“functionName”);

+ transfer(): To transfer Ether in Wei—If the transaction fails on the receiving side, an exception is thrown to the sender and the payment is automatically reverted

2.7 bytes

Fixed byte: ByteN indicates fixed-size byte array and the range of N is from 1 to 32

bytes1 aa = 0x75;
bytes1 bb = 10;
bytes1 ee = -100;

2.8 Structs: A struct is a user-defined type that contains a set of elements that in general are each of a different type

struct UserInfo {
address account;
string name;
string surname;
UserType uType;
}

2.9 Arrays

+ Static Array

int32[5] memory fixedSlots;
fixedSlots[0] = 5;
//

or

int32[5] memory fixedSlots = [int32(5), 9, 1, 3, 4];

+ Dynamic Array

int32[] unlimitedSlots;

+ To append items to a dynamic array by calling the push member function :

unlimitedSlots.push(10);
unlimitedSlots.push(13);

+ To resize a dynamic array with data location is storage

int32[] unlimitedSlots; 1
//…
unlimitedSlots.length = 5;

+ To resize a dynamic array with data location is memory

int32[] memory unlimitedSlots;
//…
unlimitedSlots = new int32[](5);

2.10 Mappings: implementation of a hash table, which stores values against keys, must declare the type of the key and the type of the value at its declaration

mapping (_KeyType => _ValueType) mapName

Example

uint nextCampaignId;
mapping(uint256 => CampaignData) campaigns; //ValueType is a structure
function start(address recipient, uint256 goal, uint256 deadline) returns (uint id) {
    var campaign = campaigns[nextCampaignId];
    campaign.recipient = recipient;
    campaign.goal = goal;
    campaign.deadline = deadline;
    nextCampaignId ++;
    id = nextCampaignId;
}

2.11 Enums: is a custom data type including a set of named values

enum InvestmentLevel {High, Medium, Low}

Leave a Reply

Your email address will not be published. Required fields are marked *