This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: setrlimit()/C++ new() seg fault


Hi,

On 10 Nov, Wolfram Gloger wrote:
 > Hello,
 > 
 > > I didn't know that RLIMIT_AS existed.  Is it portable?  It's not listed
 > > or mentioned in Richard Stevens' book "Advanced Programming in the UNIX
 > > Environment" so I assume that it is non-portable.  Correct?
 > 
 > I don't know.  On some systems it's called RLIMIT_VMEM or `virtual
 > memory limit'.  Anyway, it's available on all my Unix systems.

I just tried RLIMIT_AS and got the same seg fault.  I then bumped up
the limit and got rid of the seg fault.  Unfortunately, I was able to
allocate more than the RLIMIT_AS resource limit I set.  I think I'm
really misunderstanding how resource limits are supposed to work, 
so I'll move this discussion to the glibc mailing list.  Thanks
again for your help!

Incidentally, here's the code that demonstrates the problem:

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>

#include <cstdio>
#include <cerrno>

#include <iostream>
#include <stdexcept>

int main (int, char **)
{
  struct rlimit r;

  const long BLK_SIZE = 1024 * 1024 * 1024;

  r.rlim_cur = BLK_SIZE;
  r.rlim_max = RLIM_INFINITY;

  if (setrlimit (RLIMIT_AS, &r) != 0)
    ::perror ("setrlimit");

  try
    {
      int *a = new int[BLK_SIZE * 2];
      delete [] a;
    }
  catch (std::bad_alloc)
    {
      std::cout << "Successfully caught exception." << endl;
      return 0;
    }
  catch (...)
    {
      std::cerr << "Caught unexpected exception." << endl;
      return -1;
    }

  cerr << "Successfully allocated block larger than resource limit!" << endl;

  return 1;
}


-Ossama
-- 
Ossama Othman <othman@cs.wustl.edu>
Center for Distributed Object Computing, Washington University, St. Louis
58 60 1A E8 7A 66 F4 44  74 9F 3C D4 EF BF 35 88  1024/8A04D15D 1998/08/26


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]