Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
c++ - problems with new
I have noticed a problem with new, whilst writing some test code. I was deliberately trying to allocate a large enough amount of memory to force a failure, so that i could test for it... I found that allocating ints did not report failure as expected, whilst allocating chars did report failure as expected. Note: My test for failure is to compare the ptr to 0. After a little testing I am assuming that new doesn't throw bad_alloc. I have tested the code with the another compiler & it reports failure in both cases. A simple example of the issue is below. The code was compiled within the idde. The contents of the output window are: ---- sc ..\test1.cpp -A -Ae -mn -C -WA -S -3 -a8 -c -gf -D_CONSOLE=1 -o..\test1.obj bad_alloc not defined by compiler, creating bad_alloc class link /CO /NOI /DE /NOPACKF /XN /NT /ENTRY:mainCRTStartup /BAS:4194304 /A:512 test1.LNK ren .\$SCW$.EXE test1.EXE .\test1.EXE built Lines Processed: 992 Errors: 0 Warnings: 0 Successful build --- source: --- #include <limits.h> // INT_MAX #include <iostream> // cout void alocateChar( int size_ ) { char* p = 0; p = new char[size_]; if ( p ) { cout << "\n char allocation succeeded"; //for ( int i=0; i < size_; ++i ) // p[i] = ' '; delete [] p; } else cout << "\n char allocation failed"; } void alocateInt( int size_ ) { int* p = 0; p = new int[size_]; if ( p ) { cout << "\n int allocation succeeded"; //for ( int i=0; i < size_; ++i ) // p[i] = 0; delete [] p; } else cout << "\n int allocation failed"; } int main() { int size = INT_MAX; alocateChar( INT_MAX ); cout << "\n----------------------\n"; alocateInt( INT_MAX ); cout << "\n----------------------\n"; return 0; } --- The output from a test run is: --- H:\Digital_Mars\test1\DmW32>test1 char allocation failed ---------------------- int allocation succeeded ---------------------- --- -- Regards Kon Tantos ksoft1 attglobal.net or kon.tantos tafe.nsw.edu.au Dec 11 2002
What's happening is you're getting an int overflow with INT_MAX*sizeof(int). "Kon Tantos" <ksoft1 attglobal.net> wrote in message news:3DF819A3.E874D975 attglobal.net...I have noticed a problem with new, whilst writing some test code. I was deliberately trying to allocate a large enough amount of memory to force a failure, so that i could test for it... I found that allocating ints did not report failure as expected, whilst allocating chars did report failure as expected. Note: My test for failure is to compare the ptr to 0. After a little testing I am assuming that new doesn't throw bad_alloc. I have tested the code with the another compiler & it reports failure in both cases. A simple example of the issue is below. The code was compiled within the idde. The contents of the output window are: ---- sc ..\test1.cpp -A -Ae -mn -C -WA -S -3 -a8 -c -gf -D_CONSOLE=1 -o..\test1.obj bad_alloc not defined by compiler, creating bad_alloc class link /CO /NOI /DE /NOPACKF /XN /NT /ENTRY:mainCRTStartup /BAS:4194304 /A:512 test1.LNK ren .\$SCW$.EXE test1.EXE .\test1.EXE built Lines Processed: 992 Errors: 0 Warnings: 0 Successful build --- source: --- #include <limits.h> // INT_MAX #include <iostream> // cout void alocateChar( int size_ ) { char* p = 0; p = new char[size_]; if ( p ) { cout << "\n char allocation succeeded"; //for ( int i=0; i < size_; ++i ) // p[i] = ' '; delete [] p; } else cout << "\n char allocation failed"; } void alocateInt( int size_ ) { int* p = 0; p = new int[size_]; if ( p ) { cout << "\n int allocation succeeded"; //for ( int i=0; i < size_; ++i ) // p[i] = 0; delete [] p; } else cout << "\n int allocation failed"; } int main() { int size = INT_MAX; alocateChar( INT_MAX ); cout << "\n----------------------\n"; alocateInt( INT_MAX ); cout << "\n----------------------\n"; return 0; } --- The output from a test run is: --- H:\Digital_Mars\test1\DmW32>test1 char allocation failed ---------------------- int allocation succeeded ---------------------- --- -- Regards Kon Tantos ksoft1 attglobal.net or kon.tantos tafe.nsw.edu.au Dec 11 2002
Walter wrote:What's happening is you're getting an int overflow with INT_MAX*sizeof(int). Dec 12 2002
(INT_MAX * sizeof(int)) == 0 It is not an error to allocate 0 bytes. "Kon Tantos" <ksoft1 attglobal.net> wrote in message news:3DF8FDE1.85B05AD5 attglobal.net...Walter wrote:What's happening is you're getting an int overflow with Dec 12 2002
Walter, thanks for your quick responses. Walter wrote:(INT_MAX * sizeof(int)) == 0 It is not an error to allocate 0 bytes. Dec 12 2002
Depends. It might add some overhead bytes in the alloc routine, causing another overflow. "Kon Tantos" <ksoft1 attglobal.net> wrote in message news:3DF9305E.A20D0FA6 attglobal.net...Walter, thanks for your quick responses. Walter wrote:(INT_MAX * sizeof(int)) == 0 It is not an error to allocate 0 bytes. Dec 12 2002
ok, so i assume that you are saying 'attempting to dynamically allocate more than INT_MAX bytes using new [] in dm can result in user code believing that memory was allocated, when in fact it may not be' At this stage I am just trying to familiarise myself with dm, my apologies for any inconvenience caused. Walter wrote:Depends. It might add some overhead bytes in the alloc routine, causing another overflow. "Kon Tantos" <ksoft1 attglobal.net> wrote in message news:3DF9305E.A20D0FA6 attglobal.net...Walter, thanks for your quick responses. Walter wrote:(INT_MAX * sizeof(int)) == 0 It is not an error to allocate 0 bytes. Dec 12 2002
In article <3DF93A23.44AC1132 attglobal.net>, Kon Tantos says...ok, so i assume that you are saying Dec 13 2002
|