指针已初始化,但我有错误"Access violation reading location 0xCCCCCCCC".为什么?

Pointer is initialized but I have error "Access violation reading location 0xCCCCCCCC". Why?

本文关键字:reading violation location 0xCCCCCCCC 为什么 Access 指针 初始化 有错误      更新时间:2023-10-16

我的函数zeroPadding有更多的选项,但我只放了两个,因为它们对本例很重要。当我调试main()函数时,我得到了这个错误

Access violation reading location 0xCCCCCCCC

为什么?


这来自类Signal,只是为了显示构造函数调用的内容

template <class T> Signal<T>::Signal(int width,int height){
N=width; 
M=height;
sig2D= new int*[N];
for(int i=0;i<N;i++){
sig2D[i]=new int[M];
}
t0=0;
deltaT=1;
for (int i=0;i<N;i++)
for (int j=0;j<M;j++){
sig2D[i][j]=t0+j*deltaT;
}
}

template <class T> Image<T>::Image(int width,int height): Signal(width,height) {}
template <class T> Image<T> Image<T>::zeroPadding(Image<T> im2){
Signal<T> s1= static_cast<Signal<T>>(*this);
Signal<T> s2= static_cast<Signal<T>>(im2);   
int *temp=new int[15];
for(int i=0;i<15;i++){
temp[i]=0;
}
if(s1.getWidth()>s2.getWidth() && s1.getHeight()==s2.getHeight()){
im2.setWidth(s1.getWidth());
im2.sig2D=new T*[im2.getWidth()];
for(int i=0;i<im2.getWidth();i++){
im2.sig2D[i]=new T[im2.getHeight()];
}
for (int i=s2.getWidth();i<s1.getWidth();i++)
for(int j=0;j<s2.getHeight();j++){
im2.sig2D[i][j]=temp[j];
}   
return im2;
}
else if(s1.getHeight()<s2.getHeight() && s1.getWidth()>s2.getWidth()){
setHeight(s2.getHeight());
sig2D=new T*[getWidth()];
for(int i=0;i<getWidth();i++){
sig2D[i]=new T[getHeight()];
}
for (int i=0;i<s1.getWidth();i++)
for(int j=s1.getHeight();j<s2.getHeight();j++){
sig2D[i][j]=temp[j];
}   
(*this).zeroPadding(im2);
}
}    
template <class T> Image<T> Image<T>::addImage(Image<T> im2){
Image<T> *r=new Image(80,80);

if ((*this).getWidth()==im2.getWidth() && (*this).getHeight()==im2.getHeight()) {
//r=new Image(im2.getWidth(),im2.getHeight());
for (int i=0;i<im2.getWidth();i++){
for(int j=0;j<im2.getHeight();j++)
(*r).sig2D[i][j]= (*this).sig2D[i][j]+im2.sig2D[i][j];
}
return(*r);
}
else {
(*r)= zeroPadding(im2); // here breaks why?
addImage(*r); 
}
int main() {
Image<int> *a= new Image<int>(5,6);
Image<int> *b= new Image<int>(4,7);
(*a).addImage(*b);
return 0;
}

代码中最严重的问题是,如果程序流入else,只返回第一个if-in-zeroPadding,这会导致未定义的返回值。

if(s1.getWidth()>s2.getWidth() && s1.getHeight()==s2.getHeight()){
im2.setWidth(s1.getWidth());
im2.sig2D=new T*[im2.getWidth()];
for(int i=0;i<im2.getWidth();i++){
im2.sig2D[i]=new T[im2.getHeight()];
}
for (int i=s2.getWidth();i<s1.getWidth();i++)
for(int j=0;j<s2.getHeight();j++){
im2.sig2D[i][j]=temp[j];
}   
return im2;  // <- return here
}
else if(s1.getHeight()<s2.getHeight() && s1.getWidth()>s2.getWidth()){
// what will it return if the code comes to here
setHeight(s2.getHeight());
sig2D=new T*[getWidth()];
for(int i=0;i<getWidth();i++){
sig2D[i]=new T[getHeight()];
}
for (int i=0;i<s1.getWidth();i++)
for(int j=s1.getHeight();j<s2.getHeight();j++){
sig2D[i][j]=temp[j];
}   
(*this).zeroPadding(im2);
}
// or here

但是你的代码也有很多问题

首先,同一对象中的属性和方法不需要this,只需要sig2D[i][j]zeroPadding(im2);。。。足够了。

通过在不释放内存的情况下分配内存,还会产生大量内存泄漏。在addImage功能中,您可以创建一个新的图像

Image<T> *r=new Image(80,80);

然后在中,如果您在没有释放前一个的情况下再次创建新图像

r=new Image(im2.getWidth(),im2.getHeight());

此外,您在使用分配的内存时没有检查NULL

sig2D= new int*[N];
for(int i=0;i<N;i++){
sig2D[i]=new int[M];
}
int *temp=new int[15];
for(int i=0;i<15;i++){
temp[i]=0;

也在addImage函数中,如果它转到其他

(*r)= zeroPadding(im2); // here breaks why?

如果新语句失败,则赋值也将失败。在代码中,你在上面一行说它失败了,而在评论中,你说它在(*this):失败了

I当我调用(*r)=(*r(*r)=零填充(im2)它通过了,所以r没有问题,问题是使用(*this)。我觉得(*this)是在main()中初始化的,是吗错误的

是的,你错了。this只是指向当前对象的指针,而不是实际变量,因此如果对象存在,则访问this总是成功的。但是如果r没有初始化,那么注释中的行将不会继续运行。

使用->而不是(*x).将使您的代码更短、更干净

最后,请重新填写代码的缩进