Windows/MFC

동적 메모리 생성

aucd29 2013. 10. 2. 18:07
void CImageControl::DeleteMemory(void)
{
    // 동적 메모리 생성을 지웁니다.
    if (m_nCreateMemory)
    {
        int nCntImage = GetCutValue();
        int i;

        for (i=0; i<nCntImage; ++i)
        {
            delete[] (*m_prtSliceImage[i]);
        }

        delete[] m_prtSliceImage;

        m_nCreateMemory = false;
    }
}

void CImageControl::NewMemory(void)
{
    int nCntImage = GetCutValue();
    int i,j;

    try
    {
        // 3 * 3짜리 배열 생성시.
        // [**]->[*][*][*]
        //        | | |
        //        | | ㄴ──[][][]
        //        | ㄴ───[][][]
        //        ㄴ────[][][]

        for (i=0; i<nCntImage; ++i)
        {
            m_prtSliceImage = new CRect* [nCntImage];
        }

        for (i=0; i<nCntImage; ++i)
        {
            m_prtSliceImage[i] = new CRect[nCntImage];
        }

        int nJumpWidth    = m_ptImageSize.x / nCntImage;
        int nJumpHeight = m_ptImageSize.y / nCntImage;
        int nWidth = 0, nHeight = 0;
        int nWidth2 = nJumpWidth, nHeight2 = nJumpHeight;

        for (i=0; i<nCntImage; ++i)
        {
            for (j=0; j<nCntImage; ++j)
            {
                m_prtSliceImage[i][j].SetRect(nWidth, nHeight, nWidth2, nHeight2);
                nWidth    += nJumpWidth;
                nWidth2 += nJumpWidth;
            }

            nWidth     = 0;
            nWidth2     = nJumpWidth;
            nHeight += nJumpHeight;
            nHeight2 += nJumpHeight;
        }

        m_nCreateMemory = true;
        TraceNewMemory();
    }
    catch (CMemoryException* e)
    {
        e->ReportError();
        e->Delete();
        exit(1);
    }
}

void CImageControl::TraceNewMemory(void)
{
    int nCntImage = GetCutValue();
    int i,j;

    TRACE(L"----------------- NEW MEMORY -----------------\n");

    for (i=0; i<nCntImage; ++i)
    {
        for (j=0; j<nCntImage; ++j)
        {
            CCommon::PrintRect(m_prtSliceImage[i][j]);
        }
    }

    TRACE(L"----------------------------------------------\n");
}