Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. out of memory error

out of memory error

Scheduled Pinned Locked Moved C / C++ / MFC
databaseperformancehelpquestion
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vc programmer 1
    wrote on last edited by
    #1

    when i run this code,message box appear "out of memory error". someone please explain me.Why? following my code--->> void CIPAddressDlg::OnButton1() { CListBox *pList = (CListBox*) GetDlgItem(IDC_LIST2); pList->ResetContent(); PMIB_IPNETTABLE pIPNetTable = NULL; PMIB_IPNETROW pIPNetRow; in_addr ia; char *szTypes[4] = {"Other", "Invalid", "Dynamic", "Static"}; GetIpNetTable((PMIB_IPNETTABLE) m_pBuffer, &m_ulSize, TRUE); m_pBuffer = new BYTE[m_ulSize]; if (NULL != m_pBuffer) { pList->AddString("Start get ip net table"); m_dwResult = GetIpNetTable((PMIB_IPNETTABLE) m_pBuffer, &m_ulSize, TRUE); if (m_dwResult == NO_ERROR) { pIPNetTable = (PMIB_IPNETTABLE) m_pBuffer; for ( unsigned int x = 0; x < pIPNetTable->dwNumEntries; x++) { pIPNetRow = &(pIPNetTable->table[x]); m_strText.Format(" Index: %lu\r\n", pIPNetRow->dwIndex); pList->AddString(m_strText); m_strText.Format("MAC address length: %lu\r\n", pIPNetRow->dwPhysAddrLen); pList->AddString(m_strText); m_strText.Format(" MAC address: %02x-%02x-%02x-%02x-%02x-%02x\r\n", pIPNetRow->bPhysAddr[0], pIPNetRow->bPhysAddr[1], pIPNetRow->bPhysAddr[2], pIPNetRow->bPhysAddr[3], pIPNetRow->bPhysAddr[4], pIPNetRow->bPhysAddr[5]); pList->AddString(m_strText); ia.S_un.S_addr = pIPNetRow->dwAddr; m_strText.Format(" IP address: %s\r\n", inet_ntoa(ia)); pList->AddString(m_strText); m_strText.Format(" Type: %s\r\n", szTypes[pIPNetRow->dwType - 1]); pList->AddString(m_strText); } } else { m_strText.Format("GetIpNetTable() failed.Result = %lu\r\n", m_dwResult); pList->AddString(m_strText); } delete [] m_pBuffer; } }

    L M 2 Replies Last reply
    0
    • V vc programmer 1

      when i run this code,message box appear "out of memory error". someone please explain me.Why? following my code--->> void CIPAddressDlg::OnButton1() { CListBox *pList = (CListBox*) GetDlgItem(IDC_LIST2); pList->ResetContent(); PMIB_IPNETTABLE pIPNetTable = NULL; PMIB_IPNETROW pIPNetRow; in_addr ia; char *szTypes[4] = {"Other", "Invalid", "Dynamic", "Static"}; GetIpNetTable((PMIB_IPNETTABLE) m_pBuffer, &m_ulSize, TRUE); m_pBuffer = new BYTE[m_ulSize]; if (NULL != m_pBuffer) { pList->AddString("Start get ip net table"); m_dwResult = GetIpNetTable((PMIB_IPNETTABLE) m_pBuffer, &m_ulSize, TRUE); if (m_dwResult == NO_ERROR) { pIPNetTable = (PMIB_IPNETTABLE) m_pBuffer; for ( unsigned int x = 0; x < pIPNetTable->dwNumEntries; x++) { pIPNetRow = &(pIPNetTable->table[x]); m_strText.Format(" Index: %lu\r\n", pIPNetRow->dwIndex); pList->AddString(m_strText); m_strText.Format("MAC address length: %lu\r\n", pIPNetRow->dwPhysAddrLen); pList->AddString(m_strText); m_strText.Format(" MAC address: %02x-%02x-%02x-%02x-%02x-%02x\r\n", pIPNetRow->bPhysAddr[0], pIPNetRow->bPhysAddr[1], pIPNetRow->bPhysAddr[2], pIPNetRow->bPhysAddr[3], pIPNetRow->bPhysAddr[4], pIPNetRow->bPhysAddr[5]); pList->AddString(m_strText); ia.S_un.S_addr = pIPNetRow->dwAddr; m_strText.Format(" IP address: %s\r\n", inet_ntoa(ia)); pList->AddString(m_strText); m_strText.Format(" Type: %s\r\n", szTypes[pIPNetRow->dwType - 1]); pList->AddString(m_strText); } } else { m_strText.Format("GetIpNetTable() failed.Result = %lu\r\n", m_dwResult); pList->AddString(m_strText); } delete [] m_pBuffer; } }

      L Offline
      L Offline
      Le Ridder Noir
      wrote on last edited by
      #2

      vc-programmer- wrote: GetIpNetTable((PMIB_IPNETTABLE) m_pBuffer, &m_ulSize, TRUE); m_pBuffer = new BYTE[m_ulSize]; This is strange typedef struct _MIB_IPNETTABLE { DWORD dwNumEntries; // number of entries in table MIB_IPNETROW table[ANY_SIZE]; // array of ARP entries } MIB_IPNETTABLE, *PMIB_IPNETTABLE; **Le Ridder Noir

      Considderd to be the worlds fastest knoppenbonker.
      three years of working experience with the worlds fastest copie paster(about 2000 lines a minute).
      And experience with the one and only NewEra Guru and Crystal Ace.**

      1 Reply Last reply
      0
      • V vc programmer 1

        when i run this code,message box appear "out of memory error". someone please explain me.Why? following my code--->> void CIPAddressDlg::OnButton1() { CListBox *pList = (CListBox*) GetDlgItem(IDC_LIST2); pList->ResetContent(); PMIB_IPNETTABLE pIPNetTable = NULL; PMIB_IPNETROW pIPNetRow; in_addr ia; char *szTypes[4] = {"Other", "Invalid", "Dynamic", "Static"}; GetIpNetTable((PMIB_IPNETTABLE) m_pBuffer, &m_ulSize, TRUE); m_pBuffer = new BYTE[m_ulSize]; if (NULL != m_pBuffer) { pList->AddString("Start get ip net table"); m_dwResult = GetIpNetTable((PMIB_IPNETTABLE) m_pBuffer, &m_ulSize, TRUE); if (m_dwResult == NO_ERROR) { pIPNetTable = (PMIB_IPNETTABLE) m_pBuffer; for ( unsigned int x = 0; x < pIPNetTable->dwNumEntries; x++) { pIPNetRow = &(pIPNetTable->table[x]); m_strText.Format(" Index: %lu\r\n", pIPNetRow->dwIndex); pList->AddString(m_strText); m_strText.Format("MAC address length: %lu\r\n", pIPNetRow->dwPhysAddrLen); pList->AddString(m_strText); m_strText.Format(" MAC address: %02x-%02x-%02x-%02x-%02x-%02x\r\n", pIPNetRow->bPhysAddr[0], pIPNetRow->bPhysAddr[1], pIPNetRow->bPhysAddr[2], pIPNetRow->bPhysAddr[3], pIPNetRow->bPhysAddr[4], pIPNetRow->bPhysAddr[5]); pList->AddString(m_strText); ia.S_un.S_addr = pIPNetRow->dwAddr; m_strText.Format(" IP address: %s\r\n", inet_ntoa(ia)); pList->AddString(m_strText); m_strText.Format(" Type: %s\r\n", szTypes[pIPNetRow->dwType - 1]); pList->AddString(m_strText); } } else { m_strText.Format("GetIpNetTable() failed.Result = %lu\r\n", m_dwResult); pList->AddString(m_strText); } delete [] m_pBuffer; } }

        M Offline
        M Offline
        Mike Dimmick
        wrote on last edited by
        #3

        The problem is that m_ulSize (assuming that's a class member) is default-initialised by the debug memory allocator to 0xcccccccc, and you've not set it the first time you use it. GetIpNetTable says that's big enough (and incidentally writes the table to the garbage pointer you probably provided as the first argument) and doesn't set m_ulSize to anything. You then try to get new to allocate a huge buffer, which fails and produces the 'out of memory' error. In C and C++, you must always initialise every variable you use. Failing to do this inevitably leads to some problem where you've used an uninitialised value. Since the lifetime of the buffer pointed to by m_pBuffer is the length of this method (you delete it at the end), I'd make m_pBuffer a local variable instead. I'd also make m_ulSize a local variable, along with m_strText. Something you've not considered is that the size of the table can change between calls to GetIpNetTable. If another ARP request occurs between the calls, the size will change. A common way of handling this problem is simply to use a while loop, quitting when the error is no longer ERROR_INSUFFICIENT_BUFFER. A fixed version follows:

        void CIpNetTableDlg::OnButton1()
        {
        CListBox *pList = (CListBox*) GetDlgItem(IDC_LIST2);
        pList->ResetContent();

        PMIB\_IPNETTABLE pIPNetTable = NULL;
        PMIB\_IPNETROW pIPNetRow;
        in\_addr ia;
        char \*szTypes\[4\] = {"Other", "Invalid", "Dynamic", "Static"};
        
        BYTE\* pBuffer = NULL;
        ULONG ulSize = 0;
        DWORD dwError = GetIpNetTable((PMIB\_IPNETTABLE) pBuffer, &ulSize, TRUE);
        CString strText;
        
        while ( ERROR\_INSUFFICIENT\_BUFFER == dwError )
        {
            delete\[\] pBuffer;
            pBuffer = new BYTE\[ulSize\];
        
            if ( pBuffer == NULL )
                break;
        
            dwError = GetIpNetTable((PMIB\_IPNETTABLE) pBuffer, &ulSize, TRUE);
        }
        
        if ( dwError == NO\_ERROR )
        {
            pIPNetTable = (PMIB\_IPNETTABLE) pBuffer;
        
            for ( unsigned int x = 0; x < pIPNetTable->dwNumEntries; x++)
            {
                pIPNetRow = &(pIPNetTable->table\[x\]);
        
                strText.Format(" Index: %lu", pIPNetRow->dwIndex); 
                pList->AddString(strText);
        
                strText.Format("MAC address length: %lu", pIPNetRow->dwPhysAddrLen); 
                pList->AddString(strText);
        
                strText.Format(" MAC address: %02x-%02x-%02x-%02x-%02x-%02x", 
                    pIPNetRow->bPhysAddr\[0\],
                    pIPNetRow->bPhysAddr\[1\],
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups