尝试打印指针值时出现堆异常
抱歉我的英语不好。
我刚刚开始做我的学校期末项目,我在代码中遇到了一个错误...
该程序是用 C 语言编写的,它生成了一个矩阵(带有起始指针、行数和列数)。第一个函数应该创建一个矩阵,增加行数和列数,并将所有值归零(稍后它将用于不同的用途,但没关系)。后来有一个函数可以打印出矩阵。
当程序到达“printf”时,它会中断。“image_pross.exe 中 0x7789ea27 处出现未处理的异常:0xC0000374:堆已损坏。”
这是代码:
#include <stdio.h>
#include <stdlib.h>
struct matrix
{
int* ptr;
int row;
int column;
};
matrix ZFMatrix(matrix preMtx,int nColumn,int nRow);
void printMatrix (matrix mtx);
void main( int argc, char* argv[])
{
int matrixAdd[3][3]={{1,1,1},{1,-8,1},{1,1,1}};
matrix mtx;
mtx.ptr=&matrixAdd[0][0];
mtx.row=3;
mtx.column=3;
mtx= ZFMatrix(mtx,2,2);
printMatrix(mtx);
}
matrix ZFMatrix(matrix preMtx,int nColumn,int nRow)
{
matrix newMtx;
newMtx.column=nColumn*2+preMtx.column;
newMtx.row=nRow*2+preMtx.row;
newMtx.ptr= (int*) malloc((newMtx.row)*(newMtx.column));
int i,j,*tmp=newMtx.ptr;
//zero out the matrix
for (i=0; i<newMtx.column;i++)
{
for(j=0;j<newMtx.row;j++)
{
*newMtx.ptr=0;
newMtx.ptr++;
}
}
newMtx.ptr=tmp;
return newMtx;
}
void printMatrix (matrix mtx)
{
int i=0,j=0;
for (;i<mtx.column;i++)
{
for(;j<mtx.row;j++)
{
printf("%d, ", *mtx.ptr);
mtx.ptr++;
}
printf("\n");
}
}
Sorry for my bad English.
I just started working on my school final's project and I encountered an error in my code...
The program is in C and it makes a matrix struck (with a starting pointer, num of rows and columns). The first function should make a matrix with an enlargement of the num of rows and columns and zero out all the values(later it will be used for a diffrent perpece but nevermind that). Later there is a function that prints the matrix out.
When the program get to the "printf" it breaks.. "Unhandled exception at 0x7789ea27 in image_pross.exe: 0xC0000374: A heap has been corrupted."
Here's the code:
#include <stdio.h>
#include <stdlib.h>
struct matrix
{
int* ptr;
int row;
int column;
};
matrix ZFMatrix(matrix preMtx,int nColumn,int nRow);
void printMatrix (matrix mtx);
void main( int argc, char* argv[])
{
int matrixAdd[3][3]={{1,1,1},{1,-8,1},{1,1,1}};
matrix mtx;
mtx.ptr=&matrixAdd[0][0];
mtx.row=3;
mtx.column=3;
mtx= ZFMatrix(mtx,2,2);
printMatrix(mtx);
}
matrix ZFMatrix(matrix preMtx,int nColumn,int nRow)
{
matrix newMtx;
newMtx.column=nColumn*2+preMtx.column;
newMtx.row=nRow*2+preMtx.row;
newMtx.ptr= (int*) malloc((newMtx.row)*(newMtx.column));
int i,j,*tmp=newMtx.ptr;
//zero out the matrix
for (i=0; i<newMtx.column;i++)
{
for(j=0;j<newMtx.row;j++)
{
*newMtx.ptr=0;
newMtx.ptr++;
}
}
newMtx.ptr=tmp;
return newMtx;
}
void printMatrix (matrix mtx)
{
int i=0,j=0;
for (;i<mtx.column;i++)
{
for(;j<mtx.row;j++)
{
printf("%d, ", *mtx.ptr);
mtx.ptr++;
}
printf("\n");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该是:
当您需要整数时,您正在分配
newMtx.row * newMtx.column
字节此外,当您有
malloc时()
你应该有一个相应的free()
- 否则你会泄漏内存。Should be:
You're allocating
newMtx.row * newMtx.column
bytes when you want integersAlso, when you have a
malloc()
you should have a correspondingfree()
- Or you'll leak memory.