【发布时间】:2021-12-12 21:06:21
【问题描述】:
代码:-
#include <iostream>
using namespace std;
int main() {
int r,c,*p;
cout<<"Rows : ";
cin>>r;
cout<<"Columns : ";
cin>>c;
p=new int[r*c];
cout<<"\nEnter array elements :"<<endl;
int i,j,k;
for( i=0;i<r;i++){
for( j=0;j<c;j++){
cin>>k;
*(p+i*c+j)=k;
}
}
cout<<"\nThe array elements are:"<<endl;
for( i=0;i<r;i++){
for( j=0;j<c;j++){
cout<<*p<<" ";
p=p+1;
}
cout<<endl;
}
cout<<endl;
delete[]p;
return 0;
}
输出:-
错误:-
munmap_chunk(): 无效指针进程以退出代码-6结束。
谁能解释为什么会出现上述错误?
【问题讨论】:
-
当你最后
delete[]p时,p不再指向数组的开头,所以你删除的内容是无效的。 -
[]OT
*(p+i*c+j)可能是p[i * c + j]。