gpt4 book ai didi

c++ - 这两个代码之间的区别(为什么我的数组占用额外的空间,即使我给了它一个限制)

转载 作者:行者123 更新时间:2023-11-28 01:20:00 25 4
gpt4 key购买 nike

我用两种方式声明了一个大小为 10 的数组(一种是直接赋值,另一种是将值赋给一个变量(这里是大小)并将其传递给数组。问题是第一种情况,当我超过限制时对数组大小有限制,但在第二种情况下,数组大小超出了但我仍然没有收到错误)我能知道发生了什么吗?提前致谢! :)

PS:当我看到地址时,一切都是有序的。

#include<iostream>
using namespace std;

//global declaration
int top =-1;

void push(int array[],int value){
top = top+1;
array[top] = value;
cout<<"Pushed "<<value<<" at "<< top << " position"<< endl;

}

void pop(){
top = top -1;
}


void Gettop(int size){
cout<< "top is at "<< top << " of "<<size<<endl;
}

bool isEmpty(){
if(top == -1){
return true;
}
}

int GetTopval(int array[]){
return array[top];
}



int main(){
int size=10;

int oldArray[size];

push(oldArray,2);
push(oldArray,23);
push(oldArray,22);
push(oldArray,24);
push(oldArray,242);
push(oldArray,22);
push(oldArray,21);
push(oldArray,2);
push(oldArray,23);
push(oldArray,22);
push(oldArray,24);
push(oldArray,242);
push(oldArray,22);
push(oldArray,21);
push(oldArray,242);
push(oldArray,22);
push(oldArray,21);
pop();
push(oldArray,211);
int value = GetTopval(oldArray);

cout<< "VAlue : "<< value<<endl;

cout<< "printing the Array: "<<endl;
for(int i=0;i<15;i++){
int *temp = &oldArray[i];
cout<<oldArray[i]<<" with i as "<< i <<endl;
cout<<"at address : "<< temp <<endl;
}


return 0;
}

另一种情况是:

#include<iostream>
using namespace std;

//global declaration
int top =-1;

void push(int array[],int value){
top = top+1;
array[top] = value;
cout<<"Pushed "<<value<<" at "<< top << " position"<< endl;

}

void pop(){
top = top -1;
}


void Gettop(int size){
cout<< "top is at "<< top << " of "<<size<<endl;
}

bool isEmpty(){
if(top == -1){
return true;
}
}

int GetTopval(int array[]){
return array[top];
}



int main(){
int size=10;

int oldArray[size];

push(oldArray,2);
push(oldArray,23);
push(oldArray,22);
push(oldArray,24);
push(oldArray,242);
push(oldArray,22);
push(oldArray,21);
push(oldArray,2);
push(oldArray,23);
push(oldArray,22);
push(oldArray,24);
push(oldArray,242);
push(oldArray,22);
push(oldArray,21);
push(oldArray,242);
push(oldArray,22);
push(oldArray,21);
pop();
push(oldArray,211);
int value = GetTopval(oldArray);

cout<< "VAlue : "<< value<<endl;

cout<< "printing the Array: "<<endl;
for(int i=0;i<15;i++){
int *temp = &oldArray[i];
cout<<oldArray[i]<<" with i as "<< i <<endl;
cout<<"at address : "<< temp <<endl;
}


return 0;
}

案例 2 的输出:

Pushed 2 at 0 position
Pushed 23 at 1 position
Pushed 22 at 2 position
Pushed 24 at 3 position
Pushed 242 at 4 position
Pushed 22 at 5 position
Pushed 21 at 6 position
Pushed 2 at 7 position
Pushed 23 at 8 position
Pushed 22 at 9 position
Pushed 24 at 10 position
Pushed 242 at 11 position
Pushed 22 at 12 position
Pushed 21 at 13 position
Pushed 242 at 14 position
Pushed 22 at 15 position
Pushed 21 at 16 position
Pushed 211 at 16 position
VAlue : 211
printing the Array:
2 with i as 0
at address : 0x7ffffbb707c0
23 with i as 1
at address : 0x7ffffbb707c4
22 with i as 2
at address : 0x7ffffbb707c8
24 with i as 3
at address : 0x7ffffbb707cc
242 with i as 4
at address : 0x7ffffbb707d0
22 with i as 5
at address : 0x7ffffbb707d4
21 with i as 6
at address : 0x7ffffbb707d8
2 with i as 7
at address : 0x7ffffbb707dc
23 with i as 8
at address : 0x7ffffbb707e0
22 with i as 9
at address : 0x7ffffbb707e4
24 with i as 10
at address : 0x7ffffbb707e8
242 with i as 11
at address : 0x7ffffbb707ec
22 with i as 12
at address : 0x7ffffbb707f0
13 with i as 13
at address : 0x7ffffbb707f4
242 with i as 14
at address : 0x7ffffbb707f8


案例 1 的输出:

Pushed 2 at 0 position
Pushed 23 at 1 position
Pushed 22 at 2 position
Pushed 24 at 3 position
Pushed 242 at 4 position
Pushed 22 at 5 position
Pushed 21 at 6 position
Pushed 2 at 7 position
Pushed 23 at 8 position
Pushed 22 at 9 position
Pushed 24 at 10 position
Pushed 242 at 11 position
Pushed 22 at 12 position
Pushed 21 at 13 position
Pushed 242 at 14 position
Pushed 22 at 15 position
Pushed 21 at 16 position
Pushed 211 at 16 position
VAlue : 211
printing the Array:
2 with i as 0
at address : 0x7fff8be0b780
23 with i as 1
at address : 0x7fff8be0b784
22 with i as 2
at address : 0x7fff8be0b788
24 with i as 3
at address : 0x7fff8be0b78c
242 with i as 4
at address : 0x7fff8be0b790
22 with i as 5
at address : 0x7fff8be0b794
21 with i as 6
at address : 0x7fff8be0b798
2 with i as 7
at address : 0x7fff8be0b79c
23 with i as 8
at address : 0x7fff8be0b7a0
22 with i as 9
at address : 0x7fff8be0b7a4
24 with i as 10
at address : 0x7fff8be0b7a8
242 with i as 11
at address : 0x7fff8be0b7ac
22 with i as 12
at address : 0x7fff8be0b7b0
21 with i as 13
at address : 0x7fff8be0b7b4
242 with i as 14
at address : 0x7fff8be0b7b8
*** stack smashing detected ***: <unknown> terminated
Aborted (core dumped)

最佳答案

没有“额外空间”。您有一个大小为 10 的数组,但您越界访问它并由此调用未定义的行为。

调用超过 10 次后:

void push(int array[],int value){
top = top+1;
array[top] = value;
cout<<"Pushed "<<value<<" at "<< top << " position"<< endl;

}

您将访问已经越界的 array[10]

简而言之,未定义行为意味着:编译器不需要警告您或产生错误,您的代码可能看起来有效,但在运行时可能会发生任何事情。 C++ 标准没有定义当你违反规则时会发生什么。

关于c++ - 这两个代码之间的区别(为什么我的数组占用额外的空间,即使我给了它一个限制),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56850047/

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