如何使用malloc从1D数组转换为2D数组

时间:2015-04-25 11:21:56

标签: c++ arrays malloc

我正在尝试将1D数组转换为2D数组

4 个答案:

答案 0 :(得分:3)

如果你想分配一个实际的2D数组,而不是一个指针数组,语法会有点棘手:

int X = 16, Y = 8;
char (*pt)[X][Y] = malloc(X*Y);

在上方,pt是指向X Y charfor (int i = 0 ; i != X ; i++) { for (int j = 0 ; j != Y ; j++) { (*pt)[i][j] = (char)(i*X+j); } } 数组的指针。因为它是一个指针,访问它的元素也需要一个星号和括号:

free(pt);

当然,一旦完成使用数组,你需要释放指针:

print ('Ask me some abbreviations :)')

v_file = open('abbreviations.txt','r')
v_abbrv = v_file.read()
v_exit = 'yes'

while v_exit != 'no':
    v_word = str(raw_input('Abbreviation ? '))
    if v_word == 'LOL':
        print 'LOL means:',(v_abbrv[5:19])
    elif v_word == 'ROLF':
        print 'ROLF means :',(v_abbrv[26:55])
    elif v_word == 'ACE':
        print 'ACE means: ',(v_abbrv[61:78])
    elif v_word == 'AD':
        print 'AD means: ',(v_abbrv[83:95])
    elif v_word == 'AFAIR':
        print 'AFAIR means: ',(v_abbrv[103:123])
    elif v_word == 'AFK':
       print 'AFK means:',(v_abbrv[129:147])
    elif v_word == 'ANI':
       print 'ANI means:',(v_abbrv[153:170])
    elif v_word == 'CUL':
       print 'CUL means:',(v_abbrv[175:189])
    elif v_word == 'CWYL':
       print 'CWYL means:',(v_abbrv[196:216])
    elif v_word == 'IQ':
       print 'IQ means:',(v_abbrv[220:238])
    elif v_word == 'BA':
       print 'BA means:',(v_abbrv[243:259])
    elif v_word == 'BS':
       print 'BS means:',(v_abbrv[264:284])
    elif v_word == 'MA':
       print 'MA means:',(v_abbrv[288:302])
    elif v_word == 'JD':
       print 'JD means:',(v_abbrv[307:319])
    elif v_word == 'DC':
       print 'DC means:',(v_abbrv[324:346])
    elif v_word == 'PA':
       print 'PA means:',(v_abbrv[346:369])
    elif v_word == 'MD':
       print 'MD means:',(v_abbrv[371:391])
    elif v_word == 'VP':
       print 'VP means:',(v_abbrv[396:410])
    elif v_word == 'SVP':
       print 'SVP means:',(v_abbrv[416:437])
    elif v_word == 'EVP':
       print 'EVP means:',(v_abbrv[443:467])
    elif v_word == 'CMO':
       print 'CMO means:',(v_abbrv[473:497])
    elif v_word == 'CFO':
       print 'CFO means:',(v_abbrv[502:524])
    elif v_word == 'CEO':
       print 'CEO means:',(v_abbrv[531:555])
    v_exit = str(raw_input('Ask more?yes\\no: '))

答案 1 :(得分:2)

您需要分别分配矩阵的每一行:

unsigned char** index;

index = malloc(X * sizeof(unsigned char*)); // Allocate an array of pointers

for(int i = 0; i < X; i++)
    index[i] = malloc(Y * sizeof(unsigned char)) // Allocate each row

另请参阅有关malloc指针转换的this answer

答案 2 :(得分:1)

编辑后,您似乎正在使用C ++。

在C ++中,你应该更喜欢 std::vector< std::vector< unsigned char > >来获得动态二维数组。

std::vector< std::vector< unsigned char > > index(X, std::vector<unsigned char>(Y));

现在您可以将其用作index[i][j]并自动清除,因此需要明确地释放/删除。

Live demo here

如果您想以传统方式分配符合c的数组,请使用

unsigned char** index;

try {
  index = new int*[X];
  for(int i = 0; i < X; ++i)
    index[i] = new int[Y];
} catch(...) {...}

对于删除,您还需要单独删除每个元素。

旧答案

unsigned char        **index;
index = malloc(X * sizeof *index);
if(!index) fail();
for(xi = 0; xi < X; ++xi) {
  index[xi] = malloc(Y * sizeof **index);
  if(!index[xi]) free_and_fail();
}

首先为X指针分配空间,然后在所有X指针中分配一个数组。

释放内存时,您需要单独释放每一行:

if(index) for(xi = Y-1; xi >= 0; --xi) { /* xi is signed */
  free(index[xi];
}
free(index);
index = NULL;

答案 3 :(得分:1)

/build

错了。 unsigned char **index; index = (unsigned char**)malloc( X*Y ); index。您需要unsigned char**两次才能获得2D数组。如果您要分配malloc行,每行包含X列,请使用

Y

您应该检查unsigned char **index; index = (unsigned char**)malloc( X * sizeof(unsigned char*) ); //Allocating number of rows for(int i=0 ; i<X ; i++ ) { index[i] = (unsigned char*)malloc( Y * sizeof(unsigned char)); // Allocate each column } 是否没有失败。在使用之后,您还应该malloc以避免内存泄漏