【发布时间】:2014-08-01 12:58:53
【问题描述】:
我正在自学 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 是一个数组,大多数时候它的行为就像一个指针?因此,虽然你可以用 myints 做指针的事情,但有时它的行为不像指针,即使用 & 来显示它的地址。这意味着 myints 与指针的类型不同。它的类型是“整数数组”吗?
myints(myints 的 事物 存储在哪里,而不是其数组中的值),如果它总是自动取消对数组存储位置的引用,我该如何显示它的地址?使用 C++ 样式的新数组返回的指针?
这些在内存中是否以功能不同的方式表示?
任何可以真正巩固我的理解的文档提示或说明将不胜感激。谢谢!
【问题讨论】:
-
不,关于
myotherints的唯一远程C++ 类似的事实是您使用new来初始化它。你可以很容易地使用malloc。 C++ 方法是使用容器类,例如std::vector。 -
"是 [myotherints] 一个 C++ 风格的数组吗?"不,这不对。它是 c 风格的 dynamic 数组(或者更确切地说是指向此类数组的指针),但使用 new[] 运算符分配,这是 c++ 风格的动态分配(malloc 将在 c 中使用)。
-
谢谢你们。所以这为我澄清了术语,C++ 风格是指使用带有 RAII 的容器。上面的两个数组都是“C-style”,除了第一个只是在编译时确定的存储空间的标签。第二个是动态的,它的元素存储在空闲堆上,因此地址不同。