Skip to content

【141-week01】-关于数组的新的理解 #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
smz198181 opened this issue Apr 18, 2019 · 0 comments
Open

【141-week01】-关于数组的新的理解 #34

smz198181 opened this issue Apr 18, 2019 · 0 comments

Comments

@smz198181
Copy link

                                                 关于数组的一些新的理解

数组是数据结构中最常见也最基础的一种数据结构,在所有编程语言中都有数组的实现。 作为一种随机访问的数据结构,对于初学者来说非常困惑的一点是数组是从下标0开始计数的,这似乎非常的反常规思维。从语言学的历史来讲,这种设计方式始于C语言,之后为了兼容历史,各种语言都选择了和C语言相通的设计方式。那么C语言的设计者为什么要这么做呢。
我们知道,在C语言诞生的年代,硬件的局限性非常大,为了有效的利用那有效的CPU, 内存,IO,语言设计者们往往绞尽脑汁。那么假设我们让数组从1开始计数,那么数组寻址公式是这样的:
K address = base address + (k – 1) * typesize
那么如果从0开始计数呢,寻址公式是这样的:
K address = base address + k * typesize
这样就节约了一次CPU操作, 那么大量的计算累积下来,在有限的硬件背景下,带来的性能提升是非常可观的。 这大概就是数组从0开始计数的原因吧。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant