Frustrating new operator problem
-
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-
完成の円 -
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-
完成の円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/ -
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-
完成の円