Thread: very strange output with allocation

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    very strange output with allocation

    hey all, someone just asked me why their program continues to allocate when the exception has been caught. the exception being a failed allocation. here is the code:

    Code:
    #include <iostream>
      using std::cin; 
      using std::cout;
      using std::endl;
      using std::cerr;
    #include <iomanip>
       using std::setbase;
    #include <new> using std::bad_alloc;
    
    int main() 
    { 
    double *ptr[1000];
    try
    {
        for (int x = 0; x<1000; x++)
       { 
         ptr[x] = new double [5000000];
         cout << "allocated 5000000 doubles in " << x << endl;
        }
      }
    catch (bad_alloc & memoryAllocationException )
    {
    cerr <<"Exception occured: "<< memoryAllocationException.what() << endl;
    }
    return 0;
    }
    now I'm not a C++ programmer, but I just can't explain this output. here is the example output from this program:

    allocated 5000000 doubles in 37
    allocated 5000000 doubles in 38
    allocated 5000000 doubles in 39
    allocated 5000000 doubles in 40
    allocated 5000000 doubles in 41
    Exception occured: bad alloc exception thrown
    allocated 5000000 doubles in 42
    allocated 5000000 doubles in 43
    allocated 5000000 doubles in 44
    allocated 5000000 doubles in 45
    allocated 5000000 doubles in 46
    allocated 5000000 doubles in 47
    allocated 5000000 doubles in 48
    allocated 5000000 doubles in 49
    allocated 5000000 doubles in 50
    why does it continue to allocate memory which technically shouldn't be available? could this be a buffering problem, because of the fact that the exception message is passing through stderr and not stdout (it is getting printed before the messages in stdout are processed/outputted)?

    could anyone here please offer me some insight as to why such output might be occuring?


    thank you in advance.
    Last edited by Bleech; 11-09-2006 at 01:00 AM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Might be. Try printing the error through std::cout instead to see what happens.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Hmm ... the endl ought to flush cout ...

    Still, see what happens.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Out of curiosity, can't this happen if a faulty set_new_handler is defined without a throw?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, then it wouldn't ever throw.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Hmm... have to go back to my books then.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search - Strange Output
    By mike_g in forum C Programming
    Replies: 13
    Last Post: 06-16-2007, 02:55 PM
  2. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  3. Array output strange
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2006, 06:58 AM
  4. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  5. Really strange, unexpected values from allocated variables
    By Jaken Veina in forum Windows Programming
    Replies: 6
    Last Post: 04-16-2005, 05:40 PM