当前位置:网站首页>Solidity from introduction to practice (6)
Solidity from introduction to practice (6)
2022-06-22 08:54:00 【Rookie counter attack】
Solidity From introduction to practice ( 6、 ... and )
Be careful : This column is mainly from https://www.bilibili.com/video/BV1St411a7Pk?p=11&spm_id_from=pageDriver Study notes and https://blog.csdn.net/weixin_45067603/article/details/105751748
memory( Memory storage ) And storage( Blockchain storage )
Specific differences can be accessed :『0007』- Solidity State variables 、 Local variables and memory 、storage Love and hate between

pragma solidity ^0.4.0;
contract memoryTest{
uint[] arrx; // This state variable is stored on the blockchain network
// When we call this function , Will create a mutable array , Will allocate space for it in memory
function test(uint[] arry) returns(uint){
arrx =arry;// Will the memory of arry Copied to the blockchain arrx Variable
// When we define a variable length array inside a function , actually , Its default type is storage type , It points to the blockchain arrx, So when I modify z Of the elements , I am actually operating on the blockchain arrx
uint []storage z =arrx;
// The pointer actually modifies the arrx The length of , explain z and arrx It's the same , operation z When , Will change arrx Value
z[0] =100;
z.length =100;
}
// return arrx First element
function test2() returns(uint){
return arrx[0];
}
// return arrx The length of
function test3() returns(uint){
return arrx.length;
}
}

Structure
Case study 1
Simply use the structure
pragma solidity ^0.4.0;
contract structTest{
// Defining structure
struct student{
uint grade;
string name;
}
struct student2{
uint grade;
string name;
//student stu; A structure cannot contain itself , But it can be an array of dynamic lengths , It can also be a mapping
student2[] stu;
mapping(uint =>student2) test;
}
// Structure initialization
function init()view returns(uint,string){
student memory s = student(100," Wu Yanzu ");
return(s.grade,s.name);
}
// The second method of initializing a structure
function init2()view returns(uint,string){
student memory s = student({
grade:100,name:" Wu Yanzu "});
return(s.grade,s.name);
}
}

Case study 2
In structure mapping characteristic
pragma solidity ^0.4.0;
contract structTest{
struct student{
uint grade;
string name;
mapping(uint =>string) map;
}
student wyz;// The default is storage type , Only enough storage Type to manipulate... In a structure mapping type
//memory Objects of cannot be manipulated directly struct In structure mapping
function init() view returns(uint,string,string){
// When initializing the structure , Ignore mapping type
student memory s = student(100," Wu Yanzu ");
// Put... In memory s Object is assigned to wyz In this way storage object
wyz =s;
// We can only pass storage Object to operate mapping attribute
wyz.map[0] ="helloworld";
return(s.grade,s.name,wyz.map[0]);
}
}

Case study 3 (memory turn storage)
summary
- storage Acceptable memory Value
- memory Changes to do not affect storage
- storage Changes to do not affect memory
pragma solidity ^0.4.0;
contract structTest{
struct student{
uint grade;
string name;
}
student stu;
// The formal parameter of the function passes a pointer reference
// If a function takes a structure as an argument , Then the function modifier must have private/internal
function test(student memory s)internal{
// take s The value of is assigned to... On the blockchain stu
stu =s;
// Modify function parameters s, Just modify the space in memory , The space on the blockchain has not been modified , Because they are completely independent of each other
s.name=" Wu Yanzu ";
}
function test2(student memory s1)internal{
stu =s1;
// Modify the value in the blockchain
stu.name =" Hu Ge ";
}
function call() returns(string){
//tmp Make room in memory , Point to student
student memory tmp = student(100,"tmp");
test(tmp);
return stu.name;
}
function call2() returns(string){
student memory tmp = student(100,"tmp");
test2(tmp);
return tmp.name; // But the value does not change
}
}
Memory address map


Case study 4(storage turn memory)
summary
- storage Acceptable memory Value
- memory Changes to do not affect storage
- storage Changes to do not affect memory
pragma solidity ^0.4.0;
contract structTest{
struct student{
uint grade;
string name;
}
// Defining structure , And initialization
student stu=student(100," Li-ying zhao ");
//storage Type as an argument to the function
function test(student storage s)internal view returns(string){
student memory lina = s;
lina.name=" Wu Yanzu "; // Change the name in memory , Observe whether the name in the blockchain changes
return s.name;
}
function test2(student storage s1)internal view returns(string){
student memory liming =s1;
s1.name =" Hu Ge "; // Change the name in the blockchain , Observe whether the name in the memory changes
return liming.name;
}
function call() view returns(string) {
return test(stu); // No change
}
function call2() view returns(string){
return test2(stu); // No change
}
}

Case study 5(memory turn memory)
pragma solidity ^0.4.0;
contract structTest{
struct student{
uint grade;
string name;
}
student stu=student(100," Li-ying zhao ");
//memory Conversion between types , because solidity The optimization of the , Is passed through a pointer
function test(student memory s)internal view returns(string){
student memory lina = s;
lina.name=" Wu Yanzu ";// It's all in memory , because lina Point to s, modify lina Of name attribute , It's equivalent to modifying s Of name attribute
return s.name;
}
function test2(student memory s1)internal view returns(string){
student memory liming =s1;
s1.name =" Hu Ge ";// It's all in memory , because liming Point to s1, modify liming Of name attribute , It's equivalent to modifying s1 Of name attribute
return liming.name;
}
function call() view returns(string) {
return test(stu);// Wu Yanzu
}
function call2() view returns(string){
return test2(stu);// Hu Ge
}
}

Case study 6(storage turn storage)
pragma solidity ^0.4.0;
contract structTest{
struct student{
uint grade;
string name;
}
student stu=student(100," Li-ying zhao ");
function test(student storage s)internal view returns(string){
student storage lina = s;
lina.name=" Wu Yanzu ";
return s.name;
}
function test2(student storage s1)internal view returns(string){
student storage liming =s1;
s1.name =" Hu Ge ";
return liming.name;
}
function call() view returns(string) {
return test(stu);
}
function call2() view returns(string){
return test2(stu);
}
}

enumeration
Format :enum Variable name {x,y,z} There's no semicolon
pragma solidity ^0.4.0;
contract enumTest{
enum men{
xiaoming,xiaowang,xiaozhang}
men studyMen = men.xiaoming;
function getEnum()public pure returns(men){
return men.xiaozhang;
}
function oneDayStudy()public payable returns(string){
// According to the video , There will be errors , There is no mistake in this way
require(studyMen == men.xiaoming);
studyMen = men.xiaowang;
return "oneDayStudy with xiaoming";
}
function twoDayStudy()public view returns(string){
require(studyMen == men.xiaowang);
return ("twoDayStudy with xiaowang");
}
}

边栏推荐
- 14 responsibility chain mode
- 17 迭代器模式
- Golang 开发 常用的第三方库 没有最全只有更全
- 07 adapter mode
- 面试突击59:一个表中可以有多个自增列吗?
- 10 装饰模式
- 12 yuan sharing mode
- 【目标检测】|检测错误机制 Why Object Detectors Fail: Investigating the Influence of the Dataset
- Top ten of the year! Saining network security was once again shortlisted in the top 100 report on China's digital security
- Bit group sort
猜你喜欢
随机推荐
【Tensorboard】所有雷区都踩,一文解决你所有问题
Matlab内数据及数据类型转换
12 yuan sharing mode
20 状态模式
深度学习——(1)ResNet实现
Alibaba cloud ~ simply send SMS
Deeply analyze the usage of final keyword
Luogu p5406 [thupc2019] find tree
Thread. Source code analysis of start() method
Basic knowledge and practical application of redis
Golang 开发 常用的第三方库 没有最全只有更全
Web Knowledge 2 (request+response)
09 组合模式
17 迭代器模式
Epidemic situation of novel coronavirus
Chapter II exercise | MNIST dataset | Titanic dataset | image enhancement
11 外观模式
Prompt installremove of the Service denied when installing MySQL service
07 adapter mode
【自适应控制】最小二乘法离线辨识








