memory allocation with new under linux

From: arju (arvydas.juskaitis_at_mogul.com)
Date: 08/11/05

  • Next message: Rich Gibbs: "Re: diff on 2 files wont work due to "^M" character"
    Date: 11 Aug 2005 08:32:31 -0700
    
    

    Hi,

    I'm developing a C++ program under Linux, which allocates lots of
    memory and fails sometimes - system sends KILL or TERM signal.
    I wrote a small program to test mem allocation and it fails in the same
    way.
    If I allocate bigger chunks of memory (250MB), operator new throws
    bad_alloc exception, as it is expected.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string>

    int main()
    {
       unsigned long size = 0x100000;
       unsigned long count = 0xffffffff / size;

       try {
          for(int i = 0; i < count; i++) {
             printf("#%d ", i);
             char* ptr = new char[size];
             printf("new ");
             memset(ptr, 0, size);
             printf("memset\n");
          }

       } catch(std::bad_alloc& e) {
          printf("catch\n");
       }

       printf("done\n");
       return 0;
    }

    So the problem is - it shouldn't crash, program should receive
    exception instead.
    I'm running Suse 9.2 as development platform.

    Thanks,
    Arvid


  • Next message: Rich Gibbs: "Re: diff on 2 files wont work due to "^M" character"

    Relevant Pages

    • Re: exception from C function?
      ... George, if a C function allocates any memory or resource, it can't survive an exception without a leak. ...
      (microsoft.public.vc.language)
    • Re: exception from C function?
      ... gracefully even if an exception happens. ... leaving qsort as a legacy C function?) ... George, if a C function allocates any memory or resource, it can't ...
      (microsoft.public.vc.language)
    • Re: Exception safe at what cost
      ... > with the design of his exception safe assignment operator. ... > memory allocation fails. ...
      (comp.lang.cpp)
    • Re: Garbage Collection bug?
      ... It seems like GC does not automatically detect the insufficent memory ... (The exception is acually thrown when running a query on SQL Server CE 2.0.) ... >> decided to write a simple memory testing app that allocates memory until ...
      (microsoft.public.dotnet.framework.compactframework)
    • Re: Challenge: why does new allocate more memory than there is??
      ... > exception if more memory was requested than was available. ... You didn't mention which platform you're using. ... particular allocates memory rather lazily, in the sense that the pages ...
      (comp.programming)