Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么C++;使用新创建的数组与C样式数组的行为不同? 我正在教C++,因此编写了一些示例代码来真正理解指针和数组。_C++_C_Arrays_Pointers - Fatal编程技术网

为什么C++;使用新创建的数组与C样式数组的行为不同? 我正在教C++,因此编写了一些示例代码来真正理解指针和数组。

为什么C++;使用新创建的数组与C样式数组的行为不同? 我正在教C++,因此编写了一些示例代码来真正理解指针和数组。,c++,c,arrays,pointers,C++,C,Arrays,Pointers,我写过这样的话: 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 re

我写过这样的话:

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 
*/
intmyints[]={20,40,60,80,100};
//C风格的数组?应该存储在堆栈上吗?myint的类型指针是指向int还是指向int的数组?它与myotherints有何不同?
int*myotherints=newint[5]{20,40,60,80,100};//新的总是返回指针,这是C++样式数组吗?
//这个指针是在堆栈上创建的,而元素本身是在空闲堆中创建的吗?
int j=5;//应该存储在堆栈上

CUT

两个都是从C继承的东西(适当的C++数组是“代码> STD::数组< /代码>”),但你会混淆事物:

  • 第一个是C数组,即具有静态/自动存储持续时间的东西,它表示一块内存。该块的大小和“位置”(地址)在编译时确定

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

关于C++:

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

  • >P>在C++手册内存管理<强>应避免>,您应该使用标准库提供的容器(最有名的代码>代码:STD::向量< /代码>)。C++语言提供了自动、确定性和安全资源管理的特性;包括内存资源。你应该用这些。手动内存管理仅用于非常低级的编程和创建您自己的资源管理处理程序


new
操作符实际上是一种特殊的方法,简而言之,它要求操作系统提供一些空闲内存,并返回新分配内存的地址。因此,它返回标准内存地址<简而言之,code>myints
只是一个地址。未在堆栈上分配存储它的内存。您可以对它执行基本指针代数,但不能修改它的地址。如果您熟悉asm,可以将
myints
看作一个简单的标签。在C和C++中,你可以在方法中定义标签:
...
some_label:
    /* some code here*/
    goto some_label;

编译器应生成指令处理器使用某种跳转指令的代码。一些
jmp堆栈\u指针+一些\u标签
。类似地,尝试修改
myints
第四个值编号会指示编译器生成类似于上一个示例的调用,例如“在地址
堆栈指针+myints+4*sizeof(myints成员)
”或类似的内容

C++从C获得了类型衰减

在C语言中,很少有有效使用整个数组的方法。您不能将它们传递给函数、从函数返回、执行
[]
=
+
或几乎任何操作,至少不能直接执行

相反,只要你看得很有趣,数组就会“衰减”为指向其第一个元素的指针。(基本上,当您在所有情况下使用它时,除了少数情况下将其视为实际数组外,它都会衰减为指针)

因此
arr[3]
变成
(&(arr的第一个元素))[3]
*(&(arr的第一个元素))+3)
(这意味着相同的事情)


当您
返回arr时,也会发生类似的情况或将其传递给函数(在C中)。你的
cout听起来你对指针v数组的疑问比什么都多,不过我会试着把你所有的问题都问一遍

int myints[] = {20,40,60,80,100};
C风格的数组

是的,这就是声明C样式数组的方式

这在书堆上吗

是的,整个数组(所有5个变量)都位于堆栈上,这意味着当其块超出范围时,数据本身也超出范围,通过移动堆栈指针“释放”,这并不意味着您不能使用&运算符在其他函数中使用此数组指针

这与myOtherInts有何不同

这是堆栈上的一个值,而myotherints是堆上的一个值,堆栈上有一个指针

int* myOtherInts = new int[]{20,40,60,80,100};
这个指针是在堆栈上创建的,而元素本身是在空闲堆中创建的吗

是,声明
int*
意味着您在堆栈上声明一个指针(指针不是值是在堆栈上创建的),并且当您将该变量分配给
new
函数返回的内容时(应该是堆上分配的内存,以后必须释放)然后有一个指向堆存储值的堆栈存储指针

cout << "&myotherints: " << &myotherints << endl;
回想一下,myotherints是您在堆栈上分配的指针,但是该指针指向堆上的内存。因此,您应该期望得到一个看起来像来自堆栈的值(即,通常更高的值)

现在,
myotherints
是一个指向堆上某个值的指针,当您使用
[0]
解除对它的引用时,您会得到存储在堆上的第一个元素的值,因此当您请求存储在堆上的某个元素的地址时,您应该期望得到与存储在堆栈上的某个元素不同的结果。(即,表示堆数据的一个相当低的数字)

编辑:用于t
//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: 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
give me the address of myotherints
give me the address of the first element of myotherints