Primrose Docs
  • Home
    • ⛓️Blockchain
      • Avalanche
        • What is AVAX?
      • Ethereum
        • Ethereum Cancun Upgrade Explained(draft)
        • go-ethereum: gas estimate
        • Blockchain Transaction Lifecycle
        • Mempool
        • Gas optimization in Solidity, Ethereum
      • Solidity DeepDive
        • Meta transaction
        • solidity: patterns
        • UUPS vs Transparent
        • Solidity Interface
        • Smart contract storage
        • ERC-2981 Contract
        • Solidity modifier
        • Solidity delete keyword
        • How To Make NFTs with On-Chain Metadata - Hardhat and JavaScript
        • How to Build "Buy Me a Coffee" DeFi dapp
        • How to Develop an NFT Smart Contract (ERC 721) with Alchemy
        • Upgradeable Contract
        • Smart Contract Verification
      • Common
        • Eigenlayer
        • MultiSig(draft)
        • Chain-Based Proof-of- Stake, BFT-Style Proof-of-Stake
        • Byzantine Fault Tolerance
        • Zero-knowledge
        • Hierarchical Deterministic Wallet
        • Maker DAO
        • Defi
        • Uniswap
        • IBC
        • Cosmos
        • Gossip Protocol
        • Tendermint
        • UTXO vs Account
        • Blockchain Layer
        • Consensus Algorithm
        • How does mining work?
        • Immutable Ledger
        • SHA256 Hash
        • Filecoin
        • IPFS - InterPlanetary File System
        • IPFS와 파일코인
        • Livepeer
        • Layer 0
      • Bitcoin
        • BIP for HD Wallet
        • P2WPKH
        • Segwit vs Native Segwit
    • 📖Languages
      • Javascript/Typescript
        • Hoisting
        • This value in Javascript
        • Execution Context
        • About Javscript
        • tsconfig.json
        • Nest js Provider
        • 'return await promise' vs 'return promise'
      • Python
        • Pythonic
        • Python: Iterable, Iterator
        • Uvicorn & Gunicorn
        • WSGI, ASGI
        • Python docstring
        • Decorator in Python
        • Namespace in Python
        • Python Method
      • Go
        • GORM+MySQL Connection Pool
        • Context in golang
        • How to sign Ethereum EIP-1559 transactions using AWS KMS
        • Mongo DB in golang(draft)
        • Golang HTTP Package
        • Panic
        • Golang new/make
        • golang container package
        • errgroup in golang
        • Generic Programming in Golang
        • Goroutine(draft)
    • 📝Database
      • MongoDB in golang
      • Nested loop join, Hash join
      • DB Query plan
      • Index
      • Optimistic Lock Pessimistic Lock
    • 💻Computer Science
      • N+1 query in go
      • Web server 를 구성할 때 Thread, Process 개수를 어떻게 정할 것인가?
      • CAP
      • Socket programming
      • DNS, IP
      • URL, URI
      • TLS과 SSL
      • Caching(draft)
      • Building Microservices: Micro Service 5 Deploy Principle
      • Red Black Tree
      • AOP
      • Distributed Lock
      • VPC
      • Docker
      • All about Session and JWT
      • Closure
      • Singleton Pattern
      • TCP 3 way handshake & 4 way handshake
      • Race Condition
      • Process Address Space 
      • Call by value, Call by reference, Call by assignment
      • Zookeeper, ETCD
      • URL Shortening
      • Raft consensus
      • Sharding, Partitioning
    • 📒ETC
      • K8S SIGTERM
      • SQS
      • Git Branch Strategy: Ship / Show / Ask
      • Kafka
      • Redis Data Types
      • CI/CD
      • How does Google design APIs?
      • Minishell (42 cursus)
      • Coroutine & Subroutine
      • Redis
Powered by GitBook
On this page
  1. Home
  2. Blockchain
  3. Solidity DeepDive

Solidity delete keyword

Solidity의 delete

solidity에는 delete() 키워드가 있다.

다음과 같이 사용할 수 있다.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

contract MyContract {
    uint public number;
    uint[] public dynamicArr;
    uint[5] public fixedArr;
    mapping(uint => uint) public map;

    constructor() {
        number = 5;
        dynamicArr.push(5);
        fixedArr[0] = 5;
        map[0] = 5;
    }

    function deleteAll() external {
        delete number; // reset value to zero
        delete dynamicArr; // reduces size to 0
        delete fixedArr; // reset all values to zero
        // delete map; // error
        delete map[0]; // ok
    }
}

uint, uint[], uint[5], mapping 자료구조에 사용한 것을 볼 수 있다.

어떤 자료형에 사용하느냐에 따라서 당연히 적용되는 방식이 다르다.

그러나 핵심은 하나이다.

delete 키워드는 삭제가 아닌 재설정이다.

변수 값을 초기 기본 상태로 재설정할 수 있는 키워드이다.

당연히 초기 기본 상태는 0이다. 마치 free() 함수를 이용해 메모리를 해제하는 것처럼 NULL(0)으로 만드는 것이다.

  1. 정수(signed/unsigned)의 경우 변수를 0으로 설정한다.

  2. bool 변수의 경우 변수를 false로 설정한다.

  3. address 변수의 경우 변수를 0(기본값) 주소로 설정한다.

  4. 고정 크기 배열 및 바이트의 경우 배열의 각 요소를 기본값으로 설정한다.

  5. 동적 배열의 경우 모든 요소를 ​​제거하고 길이를 0으로 설정한다.

  6. 구조체의 경우 각 멤버를 기본값으로 설정한다.

PreviousSolidity modifierNextHow To Make NFTs with On-Chain Metadata - Hardhat and JavaScript

Last updated 1 year ago

⛓️