C++ & 符号 在新阵列前面
C++ Ampersand In front of new array
所以我认为我理解了地址、指针和与号,然后这发生在我身上。如果我以简单的方式创建一个数组,并尝试以多种方式打印出它的地址,例如......
string textArray[5] = {"test1", "test2", "test3", "test4", "test5"};
cout << "Address of string &array: " << &textArray << endl;
cout << "Address of string array: " << textArray << endl;
cout << "Address of string &array[0]: " << &textArray[0] << endl;
正如我预期的那样,所有 3 行打印相同的内存地址。但是,如果您将第一行更改为
string *textArray = new string[5] {"test1", "test2", "test3", "test4", "test5"};
cout << "Address of string &array: " << &textArray << endl;
cout << "Address of string array: " << textArray << endl;
cout << "Address of string &array[0]: " << &textArray[0] << endl;
&textArray 的地址不同!所以我的问题是为什么?通常在数组中,地址只是指向第一个元素的指针。但在动态分配的数组中不是这样吗?我从 &textArray 返回的地址是什么?
string textArray[5]
是一个包含 5 个字符串的数组,而 string *textArray
是一个指向字符串的指针。
当您打印数组名称时,它会衰减为指向第一个元素的指针,方法是提供与第一个和第三个元素相同的地址。
如果是指针,当您打印其值时,它将打印数组的基址,但当您打印其地址时,它将打印指针的地址本身。同一实体不会从一种类型衰减到另一种类型;在这里,指针和数组是不同的实体。
因为'char *textArray'根本不是一个数组。它是一个指针,有自己的地址。
事实上这些打印语句
string textArray[5] = {"test1", "test2", "test3", "test4", "test5"};
cout << "Address of string array: " << textArray << endl;
和
string *textArray = new string[5] {"test1", "test2", "test3", "test4", "test5"};
cout << "Address of string array: " << textArray << endl;
是等效的,因为数组隐式转换为指向其第一个元素的指针,并且字符串 *textArray 也指向动态分配数组的第一个元素。
另一方面,当您使用定义时
string *textArray = new string[5] {"test1", "test2", "test3", "test4", "test5"};
然后对于变量 textArray 本身,编译器会分配一个内存。它不会与动态分配的数组占用的内存相冲突。它是一个单独的记忆。所以当你写的时候
string *textArray = new string[5] {"test1", "test2", "test3", "test4", "test5"};
cout << "Address of string &array: " << &textArray << endl;
你得到这个内存的地址的输出,编译器放置变量textArray。
*textArray
是一个指针,而不是一个数组。
&textArray
是指textArray
的内存地址,即pointer textArray
所在的内存位置。
textArray
是指存储在pointer textArray
中的数据,实际上是它所指向的数组的起始地址。
&textArray[0]
再次指textArray[0]
的内存地址,它是数组的起始地址,与textArray
相同。
应该读作:
cout <<"Address of a pointer itself: " << &textArray << endl;
cout <<"Address of array pointed to by this pointer: " << textArray << endl;
cout <<"Address of array pointed to by this pointer: " << &textArray[0] << endl;
&textArray 的地址不同!所以我的问题是为什么?
因为表达式
string *textArray = new string[5];
创建一个数组和一个指向它的指针。 &textArray
是此指针本身的地址。请注意,在第一种情况下
string textArray[5]
您没有将数组分配给某个指针(但是textArray
指针本身就是指针)。如果你这样做
string textArray[5];
string *textArrayPtr = &textArray;
然后,当您尝试打印textArrayPtr
时,您将看到与第二种情况相同的结果差异:
cout <<"Address of a pointer iself: " << &textArrayPtr << endl;
cout <<"Address of array pointed to by this pointer: " << textArrayPtr << endl;
cout <<"Address of array pointed to by this pointer: " << &textArrayPtr[0] ;
我们有两个内存部分,我的变量保存到其中。第一个存储是堆栈,在下面的代码中textArray
保存在堆栈存储中,在编译之前它们的大小很清楚的变量可以保存在堆栈存储中。
string *textArray = new string[5] {"test1", "test2", "test3", "test4", "test5"};
第二个存储是堆,您可以在程序运行时使用此存储。您可以使用关键字 从此存储中保留内存new
。在上面的代码中,您为 5 个字符串保留了内存。请注意,当您从堆存储中保留内存时,您必须在使用完毕后delete
它。
因此textArray
是保存在堆栈存储中的变量的名称。textArray
变量的类型是指针,因此,当您打印&textArray
时,输出是保存在堆栈部分中的变量的地址。打印textArray
时,输出是textArray
变量的值,该变量是字符串在此部分中保存的堆部分中的地址。 &textArray[0]
告诉编译器我的意思是&(*(textArray+0))
,这与textArray
- OpenMP阵列性能较差
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- 当我的阵列太大时出现分段错误
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 位阵列上的快速AND运算
- 移除前面有空格的前2个字符串
- 阵列必须使用大括号封闭的初始器进行初始化
- 没有从阵列<float>到阵列<int>的可行转换
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- C++动态安全 2D 交错阵列
- 如何将一个窗口保留在另一个应用程序窗口的前面
- 如何重写全局方法名称以在调用原始方法之前将我的代码推到前面
- 将平面阵列重塑为复杂的特征类型
- 如何使用英特尔 PIN 捕获阵列的所有负载?
- 为什么我能够为阵列分配比计算机实际拥有的内存更多的内存
- 库特<<恩德尔;不适用于打印 2D 阵列
- 表观调用前面的表达式必须具有指向 func 类型的指针
- 数组类 阵列的打印输出
- 从较小的阵列到较大的阵列的元素级转换
- C++ & 符号 在新阵列前面