www.digitalmars.com         C & C++   DMDScript  

c++ - problems with new

reply Kon Tantos <ksoft1 attglobal.net> writes:
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
parent reply "Walter" <walter digitalmars.com> writes:
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
parent reply Kon Tantos <ksoft1 attglobal.net> writes:
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...
 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


-- Regards Kon Tantos ksoft1 attglobal.net or kon.tantos tafe.nsw.edu.au
Dec 12 2002
parent reply "Walter" <walter digitalmars.com> writes:
(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


 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...
 I have noticed a problem with new, whilst writing some test code. I



 deliberately trying to allocate a large enough amount of memory to



 a 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 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



 both cases.

 A simple example of the issue is below.
 The code was compiled within the idde. The contents of the output



 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


-- Regards Kon Tantos ksoft1 attglobal.net or kon.tantos tafe.nsw.edu.au

Dec 12 2002
parent reply Kon Tantos <ksoft1 attglobal.net> writes:
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...
 Walter wrote:
 What's happening is you're getting an int overflow with


 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...
 I have noticed a problem with new, whilst writing some test code. I



 deliberately trying to allocate a large enough amount of memory to



 a 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 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



 both cases.

 A simple example of the issue is below.
 The code was compiled within the idde. The contents of the output



 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


-- 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
Dec 12 2002
parent reply "Walter" <walter digitalmars.com> writes:
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.

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:
 What's happening is you're getting an int overflow with


 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



 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...
 I have noticed a problem with new, whilst writing some test code.





 was
 deliberately trying to allocate a large enough amount of memory to



 a 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 a





 testing I am assuming that new doesn't throw bad_alloc.


 I have tested the code with the another compiler & it reports





 in
 both cases.

 A simple example of the issue is below.
 The code was compiled within the idde. The contents of the output



 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





 /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


-- 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

Dec 12 2002
parent reply Kon Tantos <ksoft1 attglobal.net> writes:
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.

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:
 What's happening is you're getting an int overflow with


 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



 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...
 I have noticed a problem with new, whilst writing some test code.





 was
 deliberately trying to allocate a large enough amount of memory to



 a 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 a





 testing I am assuming that new doesn't throw bad_alloc.


 I have tested the code with the another compiler & it reports





 in
 both cases.

 A simple example of the issue is below.
 The code was compiled within the idde. The contents of the output



 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





 /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


-- 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


-- Regards Kon Tantos ksoft1 attglobal.net or kon.tantos tafe.nsw.edu.au
Dec 12 2002
parent Richard <fractal clark.net> writes:
In article <3DF93A23.44AC1132 attglobal.net>, Kon Tantos says...
ok, so i assume that you are saying

not 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