数组如何在 c/c++ 内部工作 [英] how do arrays work internally in c/c++

查看:41
本文介绍了数组如何在 c/c++ 内部工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道数组在 c 中是如何工作的.我最终得出了一个假设,我想知道我是否正确.

I was wondering how do arrays work in c. I end up with an hypothesis and I'd like to know if I am right or not.

我们知道数组是一系列相邻的内存案例(盒子),其中每个盒子都有它所存储类型的大小(即如果 INT 一个盒子的大小 = sizeof(int) 并且一个包含 3 个 INT 的数组3 sizeof(int) ) 的内存相邻位置

We know arrays are a sequence of adjacent memory cases(boxes), where each box has the size of the type it stocks (i.e if INTs one box has a size = sizeof(int) and an array of 3 INTs takes in memory adjacent places of 3 sizeof(int) )

现在我们也知道我们可以为某个类型的数组动态分配内存(C 中的 malloc,C++ 中的 new).

Now we also know that we can dynamically allocate memory for an array of a certain type (malloc in C, new in C++).

让我想知道的是,当用括号 [0] 调用数组时,数组的第一个框的地址和第一个值(后一个框中的值)是数组 [0] == *(array+0) == *array(数组是否被声明为type * array"或type array[]"或type array[size]")和array"这样调用是否定义为指针或数组(type * array"或type array[]"或type array[size]")是第一个框的地址.

what makes me wonder is the fact that an array has for origin the the address of the first box of the array and the first value (the value in the later box) when calling it with the bracket [0] is array[0] == *(array+0) == *array (whether array was declared "type * array" or "type array[]" or "type array[size]") and "array" called that way whether define as a pointer or an array ("type * array" or "type array[]" or "type array[size]") is the address of the first box.

我最终想到了这一点,我想确认一下:即使使用方括号 ([]) 声明的数组实际上也在内存中包含 n 个指针的序列,每个指针都包含(作为值而不是地址)包含实际值的内存盒 Bi 的地址 + 那些内存盒(B0,...,Bn 每个都包含实际值).这样,在声明int array[5]"时,程序实际上分配了 5 个相邻的 int 指针框 P0,P1,..,P4 和 5 个 int 大小的内存位置,这些内存位置分散在计算机内存 B0,B1, 各处...,B4 其中 Pi 的值是 Bi 的地址

I end up thinking and I'd like a confirmation on this: arrays when even declared with the square brackets ([]) are actually in memory a sequence of n pointers each containing (having as a value not as an address) the address of a memory box Bi containing the actual value + those memory boxes (B0,...,Bn each containing the actual values). such that in the and when one declares "int array[5]" the program actually allocate 5 adjacent boxes of int pointers P0,P1,..,P4 and 5 int sized memory places scattered all over the computer memory B0,B1,...,B4 where the value of Pi is the address of Bi

我是对还是错!!!?谢谢!

Am I right or wrong!!?? Thank you!

推荐答案

即使使用方括号 ([]) 声明的数组实际上也在内存中是一个由 n 个指针组成的序列,每个指针包含 [...] 包含实际值的内存盒 Bi 的地址 + 那些内存盒

arrays when even declared with the square brackets ([]) are actually in memory a sequence of n pointers each containing [...] the address of a memory box Bi containing the actual value + those memory boxes

没有.

听起来您很困惑 array[0] == *(array+0) == *array 对于声明为 int array[10 的数组来说如何为真];int *array = ...;.一个完全合理的问题;我们被告知对于指针 ptr 表达式 *ptr 获取指针指向的值,所以当我们对数组使用相同的语法时,地址在哪里我们正在取消引用?

It sounds like you're puzzled how array[0] == *(array+0) == *array could be true both for an array declared as int array[10]; and int *array = ...;. A perfectly reasonable question; We're told that for a pointer ptr the expression *ptr gets the value the pointer is pointing at, so when we use the same syntax with an array where are the addresses that we're dereferencing?

这里有一个秘密:数组索引运算符 ([]) 不适用于 C 和 C++ 中的数组.当您将其应用于数组时,语言会隐式地将数组转换为指向数组第一个元素的指针.因此,添加到数组或取消引用数组似乎与添加或取消引用指针的行为相同.

Here's the secret: The array index operator ([]) does not work on arrays in C and C++. When you apply it to an array the language implicitly converts the array into a pointer to the array's first element. Thus adding to an array or dereferencing an array appears to behave the same as adding or dereferencing a pointer.

int array[10];

// These lines do exactly the same thing:
int *ptr1 = &array[0]; // explicitly get address of first element
int *ptr2 = array;     // implicitly get address of first element

因此数组实际上是内存中的一组连续元素,其中每个元素实际上是值,而不是指向包含该值的另一​​个位置的指针.只是数组的定义方式意味着它们经常隐式转换为指针,因此看起来好像有指针,而实际上只有隐式转换.

So arrays really are a contiguous set of elements in memory where each element really is the value, not a pointer to another location containing the value. It's just that the way arrays are defined means that they often convert to a pointer implicitly and so it seems like there are pointers when really there's just an implicit conversion.

这篇关于数组如何在 c/c++ 内部工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆