static int count = 1 ;
static int m = 0;
printf("請輸入字串\n");
char **array1;
array1 = new char *[80];
while (true)
{
out:
for (static int i = 0; i <= count; ++i)
{
array1[i] = new char[count];
memset(array1[i], 0, count * sizeof(char));
}
cin.getline(array1[m], 80);
m++;
count ++ ;
ai:
char *str=new char[80];
memset(str, 0, 80);
printf("回車繼續輸入,按c結束輸入\n");
cin.getline(str, 80);
if (*str == 'c')
{
if(str!=NULL)
delete[]str;
break;
}
else if (*str == 0)
{
if(str!=NULL)
delete[]str;
goto out;
}
else
{
if(str!=NULL)
delete[] str;
printf("請按要求輸入\n");
goto ai;
}
}
for (int i = 0; i<count; i++)
{
if(array1[i]!=NULL)
delete[] array1[i];
}
delete[] array1;
return;
uj5u.com熱心網友回復:
判斷是否越界訪問,可以在陣列的最后一個元素之后對應的地址處設定資料讀寫斷點。如果該地址對應其它變數干擾判斷,可將陣列多宣告一個元素,并設定資料讀寫斷點在該多出元素對應的地址上。#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main() {
int a,b[11];//本來是b[10],為判斷哪句越界,故意宣告為b[11]
srand((unsigned int)time(NULL));//按兩次F11,等黃色右箭頭指向本行時,除錯、新建斷點、新建資料斷點,地址:&b[10],位元組計數:4,確定。
while (1) {//按F5,會停在下面某句,此時a的值為10,b[10]已經被修改為對應0..4之一。
b[(a=rand()%11)]=0;
Sleep(100);
b[(a=rand()%11)]=1;
Sleep(100);
b[(a=rand()%11)]=2;
Sleep(100);
b[(a=rand()%11)]=3;
Sleep(100);
b[(a=rand()%11)]=4;
Sleep(100);
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/115441.html
標籤:基礎類
