怎么办阵列的C / C内部工作++ [英] how do arrays work internally in c/c++

查看:224
本文介绍了怎么办阵列的C / C内部工作++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道该怎么办数组在C工作。我结束了一个假设,我想知道我是否正确。

我们知道阵列的相邻的存储器箱(盒),其中,每个箱具有该类型的大小的序列它的股票(即,如果INT的一个方框的大小= sizeof的(int)和3的INT阵列取入3 sizeof的内存相邻的地方(INT))

现在我们也知道,我们可以动态某种类型的数组(malloc的在C,新的C ++)分配内存。

是什么让我纳闷的是一个数组有渊源的阵列和第一个值(在后来框中的值)的第一个框的地址与支架调用时,[0]是数组[事实0] == *(数组+ 0)== *阵列(数组是否被宣布类型的数组*或类型的数组[]或阵列类型[大小])和阵列之称这种方式是否定义为指针或数组(类型的数组*或类型的数组[]或阵列类型[大小])是第一个箱子的地址。

我结束了思考,我想在此确认:阵列时,甚至用方括号([])中声明实际上是在内存中的每个包含n指针的序列(如具有价值而不是作为一个地址)存储器盒的Bi包含实际值+那些存储器盒的地址(B0,...,BN每个包含的实际值)。使得当一个宣称int数组[5]的方案实际分配分散在计算机内存B0 INT指针P0,P1,...,P4 5相邻箱和5 INT大小的内存的地方,B1的和,. ..,B4其中Pi的值是Bi的地址

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


解决方案

  

阵列时,甚至用方括号([])实际上是在内存中n个指针的序列声明的每个含有[...]包含实际值+内存的那些箱子内存盒碧的地址


不。

这听起来像你如何疑惑数组[0] == *(数组+ 0)== *阵列既能声明为一个数组是真实的 int数组[10]; 为int *阵列= ...; 。一个完全合理的问题;我们被告知,对于一个指针 PTR 除权pression * PTR 得到值指针指向在,所以当我们使用相同的语法与一个数组,其中是我们提领该地址?

这里的秘密:数组索引运算符( [] )不会在C和C ++数组。当你把它应用到阵列中的语言中数组到一个指针数组的第一个元素隐式转换。因此,加入到一个数组或访问一个数组似乎有同样的表现,添加或提领的指针。

  int数组[10];//这行做同样的事情:
为int * ptr1的=安培;数组[0]; //明确获得第一个元素的地址
为int * PTR2 =阵列; //隐含获得第一个元素的地址

所以阵列真的是在内存中的一组连续的元素,其中每个元素真的是价值,而不是一个指向包含该值的其他位置。这只是该数组定义的方式意味着他们经常转换为指针,含蓄,所以它看起来像有指针时真的在那里只是一个隐式转换。

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.

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) )

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

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.

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!

解决方案

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

Nope.

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?

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天全站免登陆