gpt4 book ai didi

c++ - 为什么用 new 创建的 C++ 数组与 C 样式数组的行为不同?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:52:32 27 4
gpt4 key购买 nike

我正在自学 C++,因此一直在编写一些示例代码来真正巩固我对指针和数组的理解。

我是这样写的:

int myints[] = {20, 40, 60, 80, 100}; 
// C style array? should be stored on stack? is myint's type pointer to int or an array of int? how does it differ from myotherints?

int* myotherints = new int[5]{20, 40, 60, 80, 100}; // new always returns pointer, is this a C++ style array?
// does this pointer get created on stack while the elements themselves are created in free heap?

int j = 5; // should be stored on stack

cout << "myints: " << myints << endl; // decays to pointer, shows address array myints is stored at
cout << "*myints: " << *myints << endl; // myints decays to pointer and is dereferenced to return value stored at start of array myints
cout << "myints[0]: " << myints[0] << endl; // [] dereferences and returns value for element 0 (20)
cout << "myotherints: " << myotherints << endl; // some value?? this is totally unlike the others, why? what is this?
cout << "*myotherints: " << *myotherints << endl; // dereferences pointer myotherints to get address that holds value 20 for first element
cout << "myotherints[0]: " << myotherints[0] << endl; // [] dereferences pointer to get address that holds value 20 for first element
cout << "j: " << j << endl << endl; // 5, sure

cout << "&myints: " << &myints << endl; // array behaving as pointer, gives address of myints[0]
cout << "&myints[0]: " << &myints[0] << endl; // array behaving as pointer, gives address of myints[0]
cout << "&myotherints: " << &myotherints << endl; // address of myotherints, is this where the pointer to the array is stored?
cout << "&myotherints[0]: " << &myotherints[0] << endl; // [] dereferences the pointer that myotherints points to and returns element 0
cout << "&j: " << &j << endl; // address of j

/*
myints: 0x7fff096df830 <-- this makes sense to me, array decays to pointer to give first element address
*myints: 20 <-- this makes sense to me, dereference first element address for value
myints[0]: 20 <-- [] dereferences implicitly, returns value from pointer

myotherints: 0x2308010 <-- myotherints is a pointer to an array of ints, but its address is much lower compared to myints and j, why is that?
*myotherints: 20 <-- getting the value from above address returns 20
myotherints[0]: 20 <-- [] dereferences to address pointed to by pointer myotherints, returns value

j: 5

&myints: 0x7fff096df830 <-- same as below
&myints[0]: 0x7fff096df830 <-- same as above, meaning *myints and myints[0] are the same thing, this address

&myotherints: 0x7fff096df828 <-- how can the pointer to myotherints array be stored here when dereferencing it (*) returns 20 and...
&myotherints[0]: 0x2308010 <-- dereferencing this address with [] also returns 20, yet they are different memory locations unlike myints

&j: 0x7fff096df824
*/

myints 是一个“C 风格数组”而 myotherints 是一个“C++ 风格数组”是真的吗?

如果我没理解错的话,myotherints 是一个指针,而 myints 是一个数组,大多数时候它的行为就像一个指针?因此,虽然您可以使用 myint 做指针式的事情,但有时它的行为并不像指针,即使用 & 来显示其地址。这意味着 myints 与指针的类型不同。它的类型是“整数数组”吗?

myints(thing 是 myints,而不是其数组中的值)存储在哪里,如果它总是自动取消对数组存储位置的引用,我该如何揭示它的地址用 C++ 风格的新数组返回的指针?

这些在内存中是否以功能不同的方式表示?

任何可以真正巩固我对此处的理解的文档提示或说明将不胜感激。谢谢!

最佳答案

两者都是从 C 继承的东西(正确的 C++ 数组 东西是 std::array),但是你混淆了东西:

  • 第一个是C数组,也就是一个静态/自动存储时长的东西,代表一 block 内存。该 block 的大小和“位置”(地址)在编译时确定。

  • 第二个是指向动态分配内存块指针。换句话说,你使用一个指针来存储你向操作系统请求的内存块的地址。这让新手感到困惑,因为这个东西有时被称为动态数组。不是 C 数组意义上的数组,但实际上我们以相同的方式使用两者,但目的不同。

关于 C++:

  • C 数组的行为类似于指向内存块的指针(数组索引等),因此它们总是通过引用传递(因为我们拥有的是通过值传递的地址,而不是数组本身),事实上,在许多情况下,它们会自动退化为指针。这些问题使 std::array 成为更好的选择,因为它具有正确的值语义并且没有隐式衰减。

  • 在 C++ 中,应该避免手动内存管理,您应该使用标准库提供的容器(最著名的 std::vector)。 C++ 语言提供自动、确定性和安全的资源管理功能;包括内存资源。你应该使用这些。手动内存管理仅用于非常低级别的编程和您自己的资源管理处理程序的创建。

关于c++ - 为什么用 new 创建的 C++ 数组与 C 样式数组的行为不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25080849/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com