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. Frustrating new operator problem

Frustrating new operator problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structureshelpperformancequestion
3 Posts 3 Posters 0 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.
  • S Offline
    S Offline
    Sparticus
    wrote on last edited by
    #1

    Hi all. I'm trying to create a simple stack template class. Yes, I can hear your remarks about just using the STL, but I have my reasons. Anyway, I'm simply trying to allocate memory to grow the stack array, but keep getting the same runtime error:

    Unhandled exception at 0x0046a470 in StackTest.exe: 0xC0000005: Access violation reading location 0x00000000.

    The call stack currently looks like this:

    StackTest.exe!strlen() Line 78 Asm StackTest.exe!exception::exception(const exception & that={...}) + 0x31 C++ StackTest.exe!std::bad_alloc::bad_alloc(const std::bad_alloc & __that={...}) + 0x13 C++ StackTest.exe!std::_Nomemory() Line 8 + 0xd C++ StackTest.exe!operator new(unsigned int size=240) Line 15 C++ StackTest.exe!operator new[](unsigned int count=240) Line 7 + 0x9 C++ StackTest.exe!CStack::resize(unsigned int nNewSize=60) Line 75 + 0xc C++ StackTest.exe!CStack::push(int & tData=8) Line 26 C++ StackTest.exe!main() Line 33 C++ StackTest.exe!mainCRTStartup() Line 259 + 0x19 C kernel32.dll!GetCurrentDirectoryW() + 0x44

    I assume from this information that there isn't enough memory available to make the allocation, but this is only the second new call that I make from the start of the program. Also, I'm only trying to allocate 240 bytes of data. Here's the code for the resize() function: template < typename T, int INCREMENT > void CStack< T, INCREMENT >::resize( size_t nNewSize ) { > // Make sure the new size is valid if ( nNewSize == m_nCapacity ) return; else if ( nNewSize < INCREMENT ) nNewSize = INCREMENT; T *pNewStack; // Pointer to the new stack size_t nMinSize; // Minimum copy size nMinSize = nNewSize < m_nCapacity ? nNewSize : m_nCapacity; // Allocate the new stack pNewStack = new T[ nNewSize ]; // Move the old data into the new stack memmove( pNewStack, m_pStack, nMinSize*sizeof(T) ); // Deallocate the old stack delete [] m_pStack; // Point to the new stack m_pStack = pNewStack; } Class Variables: m_nCapacity = 30 m_nSize = 30 INCREMENT = 30 I appreciate any insite that anyone can provide. Thanks!

    -Michael Anderson-
    完成の円

    I T 2 Replies Last reply
    0
    • S Sparticus

      Hi all. I'm trying to create a simple stack template class. Yes, I can hear your remarks about just using the STL, but I have my reasons. Anyway, I'm simply trying to allocate memory to grow the stack array, but keep getting the same runtime error:

      Unhandled exception at 0x0046a470 in StackTest.exe: 0xC0000005: Access violation reading location 0x00000000.

      The call stack currently looks like this:

      StackTest.exe!strlen() Line 78 Asm StackTest.exe!exception::exception(const exception & that={...}) + 0x31 C++ StackTest.exe!std::bad_alloc::bad_alloc(const std::bad_alloc & __that={...}) + 0x13 C++ StackTest.exe!std::_Nomemory() Line 8 + 0xd C++ StackTest.exe!operator new(unsigned int size=240) Line 15 C++ StackTest.exe!operator new[](unsigned int count=240) Line 7 + 0x9 C++ StackTest.exe!CStack::resize(unsigned int nNewSize=60) Line 75 + 0xc C++ StackTest.exe!CStack::push(int & tData=8) Line 26 C++ StackTest.exe!main() Line 33 C++ StackTest.exe!mainCRTStartup() Line 259 + 0x19 C kernel32.dll!GetCurrentDirectoryW() + 0x44

      I assume from this information that there isn't enough memory available to make the allocation, but this is only the second new call that I make from the start of the program. Also, I'm only trying to allocate 240 bytes of data. Here's the code for the resize() function: template < typename T, int INCREMENT > void CStack< T, INCREMENT >::resize( size_t nNewSize ) { > // Make sure the new size is valid if ( nNewSize == m_nCapacity ) return; else if ( nNewSize < INCREMENT ) nNewSize = INCREMENT; T *pNewStack; // Pointer to the new stack size_t nMinSize; // Minimum copy size nMinSize = nNewSize < m_nCapacity ? nNewSize : m_nCapacity; // Allocate the new stack pNewStack = new T[ nNewSize ]; // Move the old data into the new stack memmove( pNewStack, m_pStack, nMinSize*sizeof(T) ); // Deallocate the old stack delete [] m_pStack; // Point to the new stack m_pStack = pNewStack; } Class Variables: m_nCapacity = 30 m_nSize = 30 INCREMENT = 30 I appreciate any insite that anyone can provide. Thanks!

      -Michael Anderson-
      完成の円

      I Offline
      I Offline
      ian mariano
      wrote on last edited by
      #2

      You can enable the debug CRT checks for memory leaks[^], and some of the other Memory Dumping/checking stuff from there. Maybe that will help on why you're unable to allocate using new.


      "The greatest danger to humanity is humanity without an open mind."
      - Ian Mariano
      http://www.ian-space.com/

      1 Reply Last reply
      0
      • S Sparticus

        Hi all. I'm trying to create a simple stack template class. Yes, I can hear your remarks about just using the STL, but I have my reasons. Anyway, I'm simply trying to allocate memory to grow the stack array, but keep getting the same runtime error:

        Unhandled exception at 0x0046a470 in StackTest.exe: 0xC0000005: Access violation reading location 0x00000000.

        The call stack currently looks like this:

        StackTest.exe!strlen() Line 78 Asm StackTest.exe!exception::exception(const exception & that={...}) + 0x31 C++ StackTest.exe!std::bad_alloc::bad_alloc(const std::bad_alloc & __that={...}) + 0x13 C++ StackTest.exe!std::_Nomemory() Line 8 + 0xd C++ StackTest.exe!operator new(unsigned int size=240) Line 15 C++ StackTest.exe!operator new[](unsigned int count=240) Line 7 + 0x9 C++ StackTest.exe!CStack::resize(unsigned int nNewSize=60) Line 75 + 0xc C++ StackTest.exe!CStack::push(int & tData=8) Line 26 C++ StackTest.exe!main() Line 33 C++ StackTest.exe!mainCRTStartup() Line 259 + 0x19 C kernel32.dll!GetCurrentDirectoryW() + 0x44

        I assume from this information that there isn't enough memory available to make the allocation, but this is only the second new call that I make from the start of the program. Also, I'm only trying to allocate 240 bytes of data. Here's the code for the resize() function: template < typename T, int INCREMENT > void CStack< T, INCREMENT >::resize( size_t nNewSize ) { > // Make sure the new size is valid if ( nNewSize == m_nCapacity ) return; else if ( nNewSize < INCREMENT ) nNewSize = INCREMENT; T *pNewStack; // Pointer to the new stack size_t nMinSize; // Minimum copy size nMinSize = nNewSize < m_nCapacity ? nNewSize : m_nCapacity; // Allocate the new stack pNewStack = new T[ nNewSize ]; // Move the old data into the new stack memmove( pNewStack, m_pStack, nMinSize*sizeof(T) ); // Deallocate the old stack delete [] m_pStack; // Point to the new stack m_pStack = pNewStack; } Class Variables: m_nCapacity = 30 m_nSize = 30 INCREMENT = 30 I appreciate any insite that anyone can provide. Thanks!

        -Michael Anderson-
        完成の円

        T Offline
        T Offline
        Tim Smith
        wrote on last edited by
        #3

        I would doubt that you are out of memory unless you have a leak. You might be over running an allocated memory region and thus trashing the memory manager. Tim Smith I'm going to patent thought. I have yet to see any prior art.

        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