这两个代码之间的差异(为什么我的数组也有额外的空间,即使我限制了它)
Difference between this two codes(Why is my array taking extra space even if i gave a limit to it)
我以两种方式声明了大小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 标准无法定义当您违反规则时会发生什么。
相关文章:
- 数组不会存储超出第一个空间的范围
- 将数组类型更改为 Int16 会节省存储空间吗?
- 这两个代码之间的差异(为什么我的数组也有额外的空间,即使我限制了它)
- 为多维数组创建内存空间
- 从数组中添加值并插入空间
- 字符串的变体数组包含替代空间
- 读取文件中的数字数量无限,存储在数组中(无空间)
- 动态分配缓冲空间为二维数组 - 为什么失败
- 赋值运算符复制数组中除可用空间之外的所有内容
- C++在具有O(n)空间的数组中查找第一个非重复整数
- 如何为字符串数组实例变量动态分配空间
- 数组的指针,该数组的每个元素的内存空间信息存储在哪里
- 如何将一个巨大的二维数组初始化为类中的空间
- 删除数组中的对象,并通过让其他数组对象相应地向左移动来摆脱空数组空间
- 为什么C++为我的动态数组分配这么大的内存空间
- 看似有效的算法,用于在不浪费空间的情况下将二叉搜索树存储在数组中
- 文本文件读取和动态数组中的 EMTY 空间传递问题
- 将动态空间分配给字符串数组时出现问题
- 追加到具有可用空间的字符数组
- 如何在不分配更多存储空间的情况下从数组初始化向量