C++ & 符号 在新阵列前面

C++ Ampersand In front of new array

本文关键字:阵列 前面 新阵列 符号 C++      更新时间:2023-10-16

所以我认为我理解了地址、指针和与号,然后这发生在我身上。如果我以简单的方式创建一个数组,并尝试以多种方式打印出它的地址,例如......

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