这两个代码之间的差异(为什么我的数组也有额外的空间,即使我限制了它)

Difference between this two codes(Why is my array taking extra space even if i gave a limit to it)

本文关键字:空间 数组 代码 两个 之间 我的 为什么      更新时间:2023-10-16

我以两种方式声明了大小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;
}

case2的输出:

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

case1的输出:

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 标准无法定义当您违反规则时会发生什么。