[問題] C++嘗試讀取或寫入受保護的記憶體
開發平台(Platform): (Ex: Win10, Linux, ...)
Win10
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
VC++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
NO
問題(Question):
當我在使用指標寫入資料時,出現了嘗試讀取或寫入受保護的記憶體。
這通常表示其他記憶體已損毀。這個Exception
餵入的資料(Input):
NO
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
程式碼(Code):(請善用置底文網頁, 記得排版)
Bitmap^ openImg;
openImg=gcnew Bitmap(pictureBox1->Image);
Bitmap^ temp=gcnew Bitmap(openImg ->Width,openImg->Height);
String^ M=textBox4->Text;
int m, r, y, tem=0;
int *cub;
if(String::IsNullOrEmpty(M))
m=0;
else
m=System::Convert::ToInt32(M);
int masksize=m*m;
int u=m/2;
if(m%2 != 0){
for(int i=u;i<temp->Height-u;i++){
for(int j=u;j<temp->Width-u;j++){
cub=new int[masksize];
r=0;
y=0;
for(int z=0;z<m^2;z++){
Color RGB=openImg->GetPixel(j-u+r,i-u+y);
*(cub+z)=RGB.B; //出錯於這行
r++;
if(r == m){
r=0;
y++;
}
}
for(int k=0;k<m;k++){
for(int q=0;q<8;q++){
if(*(cub+q) > *(cub+(q+1))){
tem=*(cub+(q+1));
*(cub+(q+1))=*(cub+q);
*(cub+q)=tem;
}
}
}
int sommedian=*(cub+(m^2/2));
delete [] cub;
temp->SetPixel(j,i,Color::FromArgb(sommedian,sommedian,sommedian));
}
pictureBox2->Image=temp;
}
}
補充說明(Supplement):
請問這是哪裡出錯? 需要怎麼解決
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.116.234.231
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1490339377.A.4F1.html
→
03/24 15:40, , 1F
03/24 15:40, 1F
→
03/24 15:41, , 2F
03/24 15:41, 2F
→
03/24 15:45, , 3F
03/24 15:45, 3F
→
03/24 15:47, , 4F
03/24 15:47, 4F
→
03/24 15:48, , 5F
03/24 15:48, 5F
→
03/24 15:49, , 6F
03/24 15:49, 6F
→
03/24 15:58, , 7F
03/24 15:58, 7F
推
03/24 16:48, , 8F
03/24 16:48, 8F
→
03/24 16:48, , 9F
03/24 16:48, 9F
推
03/24 23:32, , 10F
03/24 23:32, 10F
推
03/25 03:56, , 11F
03/25 03:56, 11F
推
03/25 22:30, , 12F
03/25 22:30, 12F