2D阵列的连续内存分配---释放内存

时间:2014-02-19 07:28:21

标签: c multidimensional-array dynamic-memory-allocation

当我运行此程序时,我在free()期间遇到堆崩溃,但如果我使用F10进行调试(在Visual Studio 2010中),我就会成功。

我的代码:

以下代码用于免费。在这种情况下,有人可以解释2D阵列的解除分配吗?

void Deallocate2D ( SINT32  **pAccess, UINT8 i_ubyNoOfRows )
{
    int i;
    UINT8 *elem;
    if ( NULL == pAccess )
    {
        printf ( "%d>Invalid Params\n", __LINE__ ); 
        return;
    }

    printf ( "\nDeallocate 2D Array..................\n" );

    /* Include the code to deallocate the 2D array */
    {   
            free(pAccess);  

    }
}

以下代码用于内存分配:

SINT32 Allocate2D ( 
                SINT32  ***pAccess, 
                UINT8   i_ubyNoOfRows, 
                UINT8   i_ubyNoOfCols )
{
    SINT32  nResult = -1;
    SINT32  **pAc = NULL;
    UINT8   *pubyPositioner;
    UINT32  unTotalSize;
    int i;

    //unTotalSize = i_ubyNoOfRows * i_ubyNoOfCols;
    printf ( "\nAllocate 2D Array..................\n" );

    /* Include the code to allocate the 2D array */
    {

    /*Contiguous memallocation  pAc*/
    pAc = (SINT32**)malloc(sizeof(SINT32*) * i_ubyNoOfRows);
    if(pAc)
        pubyPositioner = (UINT8*)malloc(sizeof(UINT8) * i_ubyNoOfCols);
    if(pubyPositioner)
        for(i = 0; i < i_ubyNoOfCols; ++i)
            pAc[i] = (SINT32*)pubyPositioner + (i * i_ubyNoOfCols);             

    *pAccess = pAc;

    /*Non contugious allocation*/
    /*  
        pAc = (SINT32**)malloc(sizeof(SINT32*) * i_ubyNoOfRows );
        *pAccess = pAc;
        if(pAc)
         for(i= 0; i < i_ubyNoOfRows; ++i){
            pAc[i] = (SINT32*)malloc(sizeof(SINT32*) + (i * i_ubyNoOfRows) );
            if(pAc[i])
                for(j = 0; j < i_ubyNoOfCols; ++j)
                    pAc[i][j] = (UINT8)malloc(sizeof(UINT8) +   (i_ubyNoOfRows * i_ubyNoOfCols) );

         }

     */
        if(*pAccess != NULL)
            nResult = 0;


    }

    if ( NULL == pAccess )
    {
        printf ( "%d>Invalid Params\n", __LINE__ ); 
        return -1;
    }

    return nResult;
}

填充数组和打印数组的代码:

SINT32 Fill2D ( 
                SINT32  **pnAccess,
                SINT32  *pnData,
                UINT8   i_ubyNoOfRows, 
                UINT8   i_ubyNoOfCols )
{
    SINT16  wRIndex, wCIndex, wDataIndex = 0;   



    printf ( "\nFill 2D Array..................\n" );

    /* Include the code to fill the 2D array with the 1D values */
    {
        for(wRIndex = 0 ; wRIndex < 3; ++wRIndex)
            for(wCIndex = 0; wCIndex < 3; ++wCIndex)
                pnAccess [wRIndex][wCIndex] = pnData[wDataIndex++];

    }

    if ( ( NULL == pnAccess ) \
            || ( NULL == pnData ) )
    {
        printf ( "%d>Invalid Params\n", __LINE__ ); 
        return -1;
    }

    return 0;
}

SINT32 Print2D ( 
                SINT32  **pnAccess,
                UINT8   i_ubyNoOfRows, 
                UINT8   i_ubyNoOfCols )
{
    SINT16  wRIndex, wCIndex;

    if ( NULL == pnAccess )
    {
        printf ( "%d>Invalid Params\n", __LINE__ ); 
        return -1;
    }

    printf ( "\n2D Array..................\n\n" );

    /* Include the code to Print the 2D array in matrix format */
    {
        for(wRIndex = 0 ; wRIndex < i_ubyNoOfRows; ++wRIndex)
        {
            if(wRIndex % 2 == 0)
                printf("\n");
            for(wCIndex = 0; wCIndex < i_ubyNoOfCols; ++wCIndex)
                printf("%d ", pnAccess[wRIndex][wCIndex]); 

        }
    }

    return 0;
}

以下代码是一种切入点:

void Test2DArray ( void )
{
    SINT32  **pnData = NULL;
    SINT32  nData1[] = { 10, 15, 20, 15, 20, 25, 10, 25, 20 };
    SINT32  nData2[] = { 70, 75, 80, 65, 90, 25, 30, 35, 80 };

    printf ( "\n==================================\n" );
    printf ( "Test 2D Array..................\n" );
    printf ( "==================================\n\n" );

    if ( Allocate2D( &pnData, 3, 3 ) != -1 )
    {
        if ( Fill2D( pnData, nData1, 3, 3 ) != - 1 )
        {
            if ( NULL != pnData )
            {
                Print2D ( pnData, 3, 3 );
                Deallocate2D ( pnData, 3 );
            }
        }
    }

    if ( Allocate2D( &pnData, 3, 3 ) != -1 )
    {
        if ( Fill2D( pnData, nData2, 3, 3 ) != - 1 )
        {
            if ( NULL != pnData )
            {
                Print2D ( pnData, 3, 3 );
                Deallocate2D ( pnData, 3 );
            }
        }
    }
}

3 个答案:

答案 0 :(得分:3)

 /*Contiguous memallocation  pAc*/
    pAc = (SINT32**)malloc(sizeof(SINT32*) * i_ubyNoOfRows);
    if(pAc)
        pubyPositioner = (UINT8*)malloc(sizeof(UINT8) * i_ubyNoOfCols);
    if(pubyPositioner)
        for(i = 0; i < i_ubyNoOfCols; ++i)
            pAc[i] = (SINT32*)pubyPositioner + (i * i_ubyNoOfCols);   

这不是连续的内存分配!它是一个基于指针的查找表,在一个段中分配,指向在另一个段中分配的一块内存。

分配真正的2D数组的正确代码是:

SINT32 (*pAc)[ubyNoOfCols] = malloc( sizeof(SINT32[i_ubyNoOfCols][i_ubyNoOfRows]) );
...
free(pAc);

请注意,这只会在C编译器上编译,因此您无法使用Visual Studio。


  

在这种情况下,有人可以解释2D阵列的解除分配吗?

free(pAccess); 

看起来代码只释放指针查找表而不是实际数据。所以我认为这是一个错误:代码包含内存泄漏。

答案 1 :(得分:0)

让我们来看看您的分配代码:

pAc = (SINT32**)malloc(sizeof(SINT32*) * i_ubyNoOfRows);
if(pAc)
    pubyPositioner = (UINT8*)malloc(sizeof(UINT8) * i_ubyNoOfCols);
if(pubyPositioner)
    for(i = 0; i < i_ubyNoOfCols; ++i)
        pAc[i] = (SINT32*)pubyPositioner + (i * i_ubyNoOfCols);             

所以,你首先要为i_ubyNoOfRows指针分配内存。这很好。
然后继续分配i_ubyNoOfCols 字节,(测试用例中为3个字节),但是i_ubyNoOfCols*i_ubyNoOfRows 整数需要空间(3 * 3) * 4 =在测试用例中为36个字节,假设int是32位类型) 当您继续初始化内存时,您可以使用malloc()free()所需的数据来正常运行。

你错误的一半的一个迹象就是行pAc[i] = (SINT32*)pubyPositioner + (i * i_ubyNoOfCols);中的演员。它表明您对pubyPostitioner使用了错误的类型。

Ps:你在3中有两个Fill2D()硬编码,即使这个函数获得正确的宽度和高度作为参数传递。

答案 2 :(得分:0)

  

在我的问题中,我想为2D数组分配内存然后释放它。   经过一番研究,我已经解释了我的理解如下

     
      
  1. 使用指针数组分配内存
  2.   
double **p;
int i;
p= malloc(rows * sizeof(*p));
for(i= 0; i<rows;++i)
p[i] = malloc(cols * sizeof(double));

...  ...

for(i=0;i<rows;i++)
    free(p[i]);
 free(p);
  
      
  1. 使用指针查找表进行分配
  2.   
double **p;
double *elem;

elem = malloc(rows * cols * sizeof(double));
p = malloc(cols * sizeof(double*));
for (i = 0; i<rows;++i)
p[i] =  elem + ( i*cols);

free(p);
  
      
  1. 连续分配
  2.   
double **p;
p = malloc( rows * cols * sizeof(double*));
/*Accessing / printing values ... assume values are assinged to this array*/
for(i=0; i <rows; ++i)
 for(j = 0; j< cols; ++j)
  printf("%d",p[j + (i * cols)]);

free(p)