c++ - problems with new
- Kon Tantos (80/80) Dec 11 2002 I have noticed a problem with new, whilst writing some test code. I was
- Walter (3/83) Dec 11 2002 What's happening is you're getting an int overflow with INT_MAX*sizeof(i...
- Kon Tantos (13/121) Dec 12 2002 Ok. If I change the int allocation to:
- Walter (10/131) Dec 12 2002 (INT_MAX * sizeof(int)) == 0
- Kon Tantos (14/154) Dec 12 2002 Walter,
- Walter (9/163) Dec 12 2002 Depends. It might add some overhead bytes in the alloc routine, causing
- Kon Tantos (11/186) Dec 12 2002 ok, so i assume that you are saying
- Richard (12/13) Dec 13 2002 not that it matters much to this discussion, but I did the following to ...
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).Ok. If I change the int allocation to: size = INT_MAX / sizeof(int); p = new int[size]; new correctly reports failure. However when I was trying for INT_MAX ints, new reported success when it actually failed. When I uncommented the code which accesses the memory supposedly returned by new Windows terminated the program with an application error."Kon Tantos" <ksoft1 attglobal.net> wrote in message news:3DF819A3.E874D975 attglobal.net...-- Regards Kon Tantos ksoft1 attglobal.net or kon.tantos tafe.nsw.edu.auI 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 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:INT_MAX*sizeof(int).What's happening is you're getting an int overflow withOk. If I change the int allocation to: size = INT_MAX / sizeof(int); p = new int[size]; new correctly reports failure. However when I was trying for INT_MAX ints, new reported success when it actually failed. When I uncommented the code which accesses the memory supposedly returned by new Windows terminated the program with an application error.was"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. Iforcedeliberately trying to allocate a large enough amount of memory towhilsta failure, so that i could test for it... I found that allocating ints did not report failure as expected,inallocating 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 failurewindowboth cases. A simple example of the issue is below. The code was compiled within the idde. The contents of the output-- Regards Kon Tantos ksoft1 attglobal.net or kon.tantos tafe.nsw.edu.auare: ---- 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 12 2002
Walter, thanks for your quick responses. Walter wrote:(INT_MAX * sizeof(int)) == 0 It is not an error to allocate 0 bytes.that is very true, & that is exactly what I was originally doing. However if I use the following code int size = INT_MAX - 5; int* p = new int[size]; I am effectively making a call like int* p = new int[-24]; shouldn't this fail & either set p to 0, or throw bad_alloc?"Kon Tantos" <ksoft1 attglobal.net> wrote in message news:3DF8FDE1.85B05AD5 attglobal.net...-- Regards Kon Tantos ksoft1 attglobal.net or kon.tantos tafe.nsw.edu.auWalter wrote:INT_MAX*sizeof(int).What's happening is you're getting an int overflow withOk. If I change the int allocation to: size = INT_MAX / sizeof(int); p = new int[size]; new correctly reports failure. However when I was trying for INT_MAX ints, new reported success when it actually failed. When I uncommented the code which accesses the memory supposedly returned by new Windows terminated the program with an application error.was"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. Iforcedeliberately trying to allocate a large enough amount of memory towhilsta failure, so that i could test for it... I found that allocating ints did not report failure as expected,inallocating 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 failurewindowboth cases. A simple example of the issue is below. The code was compiled within the idde. The contents of the output-- Regards Kon Tantos ksoft1 attglobal.net or kon.tantos tafe.nsw.edu.auare: ---- 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 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:it(INT_MAX * sizeof(int)) == 0 It is not an error to allocate 0 bytes.that is very true, & that is exactly what I was originally doing. However if I use the following code int size = INT_MAX - 5; int* p = new int[size]; I am effectively making a call like int* p = new int[-24]; shouldn't this fail & either set p to 0, or throw bad_alloc?"Kon Tantos" <ksoft1 attglobal.net> wrote in message news:3DF8FDE1.85B05AD5 attglobal.net...Walter wrote:INT_MAX*sizeof(int).What's happening is you're getting an int overflow withOk. If I change the int allocation to: size = INT_MAX / sizeof(int); p = new int[size]; new correctly reports failure. However when I was trying for INT_MAX ints, new reported success whenIactually failed. When I uncommented the code which accesses the memory supposedly returned by new Windows terminated the program with an application error."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.littlewasforcedeliberately trying to allocate a large enough amount of memory towhilsta failure, so that i could test for it... I found that allocating ints did not report failure as expected,allocating chars did report failure as expected. Note: My test for failure is to compare the ptr to 0. After afailuretesting I am assuming that new doesn't throw bad_alloc. I have tested the code with the another compiler & it reports/BAS:4194304inwindowboth cases. A simple example of the issue is below. The code was compiled within the idde. The contents of the outputare: ---- 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-- Regards Kon Tantos ksoft1 attglobal.net or kon.tantos tafe.nsw.edu.au-- Regards Kon Tantos ksoft1 attglobal.net or kon.tantos tafe.nsw.edu.au/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 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...-- Regards Kon Tantos ksoft1 attglobal.net or kon.tantos tafe.nsw.edu.auWalter, thanks for your quick responses. Walter wrote:it(INT_MAX * sizeof(int)) == 0 It is not an error to allocate 0 bytes.that is very true, & that is exactly what I was originally doing. However if I use the following code int size = INT_MAX - 5; int* p = new int[size]; I am effectively making a call like int* p = new int[-24]; shouldn't this fail & either set p to 0, or throw bad_alloc?"Kon Tantos" <ksoft1 attglobal.net> wrote in message news:3DF8FDE1.85B05AD5 attglobal.net...Walter wrote:INT_MAX*sizeof(int).What's happening is you're getting an int overflow withOk. If I change the int allocation to: size = INT_MAX / sizeof(int); p = new int[size]; new correctly reports failure. However when I was trying for INT_MAX ints, new reported success whenIactually failed. When I uncommented the code which accesses the memory supposedly returned by new Windows terminated the program with an application error."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.littlewasforcedeliberately trying to allocate a large enough amount of memory towhilsta failure, so that i could test for it... I found that allocating ints did not report failure as expected,allocating chars did report failure as expected. Note: My test for failure is to compare the ptr to 0. After afailuretesting I am assuming that new doesn't throw bad_alloc. I have tested the code with the another compiler & it reports/BAS:4194304inwindowboth cases. A simple example of the issue is below. The code was compiled within the idde. The contents of the outputare: ---- 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-- Regards Kon Tantos ksoft1 attglobal.net or kon.tantos tafe.nsw.edu.au-- Regards Kon Tantos ksoft1 attglobal.net or kon.tantos tafe.nsw.edu.au/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 12 2002
In article <3DF93A23.44AC1132 attglobal.net>, Kon Tantos says...ok, so i assume that you are sayingnot that it matters much to this discussion, but I did the following to your code and got an exception in your alocateInt definition.. // stlport include #include <new> .. try { p = (int*)std::__stl_new(size_); } catch (bad_alloc& e) { cout << e.what() << '\n'; } Richard
Dec 13 2002