zoukankan      html  css  js  c++  java
  • [Other] An Overview of Arrays and Memory

    One integer takes 32bit in memory, 1 byte = 8bits, therefore one integer takes 4 bytes.

    Now let's assume we have an array: [1,2,3]

      4bytes .   4bytes .  4bytes

    | . | . | . | . | . | . | . | . | . | . | . | . |

         1 .            2 .          3

    It tooks 4 * 3 bytes for three integers in an array. What if we want to add two more integers into the array, how the memory allocate?

       A. it appends another 8 bytes in the end

       B. it recreate an new array with 4 * 5 bytes size. 

    B. is the correct answer. It always create a new larger array and delete the old array. The simple reason for this is because we never know after array, it might have other code:

    var a = [1,2,3]
    var c = 4

      4bytes .   4bytes .  4bytes     4bytes

    | . | . | . | . . | . | . | . | . | . | . | . | . | . | . | . |

         1 .            2 .          3 .        c=4

    It assign 4 bytes to variable c in memory. We cannot simply append more memory after the old array. 

  • 相关阅读:
    删除指定日期的文件
    pytorch加载数据集
    pytorch ResNet
    pytorch GoogLeNet
    pytorch实现VGG
    pytorch训练AlexNet
    序列化.Net对象到JSON
    c#对象序列化 用来保存对象数据
    Wpf设置listview样式
    wpf listview添加自增序号
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10236848.html
Copyright © 2011-2022 走看看