我想按功能創建一個動態二維數組,但似乎有些東西是非常錯誤的。當我想在其中放入某些東西時,它會引發錯誤。動態二維數組無法正常創建
錯誤
Unhandled exception at 0x003a19c8 in p01.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd.
CreateDynamicArray()
short int** CreateDynamicArray(int row, int col)
{
// Creating variable
short int** dynamicArray;
// Creating rows
dynamicArray = (short int**)malloc(row * sizeof(short int*));
// Going trough every row and creating columns for them
for (int i = 0; i < row; i++)
{
dynamicArray[row] = (short int*)malloc(col * sizeof(short int));
}
// Returning created array
return dynamicArray;
}
主()
// Creating it
dynamicArray = CreateDynamicArray(row, col);
// Filling up with random numbers
for (i = 0; i < row; i++)
{
randomNumber = rand() % 20;
dynamicArray[i][n] = randomNumber; // Here it throws me exception
for (n = 0; n < col; n++)
{
randomNumber = rand() % 20;
dynamicArray[i][n] = randomNumber;
}
}
P.S. 是的,這是一門功課有點的,但我仍然需要幫助OM這件事:)
哈哈,謝謝。這是相當...愚蠢的我:) – sed 2011-03-18 13:04:58
@Qmal:你的歡迎:) – 2011-03-18 13:07:03
你的malloc演員是多餘的,這是C++的東西 – user411313 2011-03-18 18:00:03