Thread: malloc error can't allocate region on mac os X

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    61

    malloc error can't allocate region on mac os X

    Hi everyone;

    Unexpectedly I am receiving a dynamic memory allocation error in my code when running on a Mac OS X (10.5), while that does not happen when I compile and execute the same code on a linux x86.

    the error is the following (one for each memory allocation call):
    ...
    testing.exe(13762) malloc: *** mmap(size=2132258816) failed (error code=12)
    *** error: can't allocate region
    ...

    and the lines of the code are wrapped below.

    Where could the problem be? I report my memory allocation lines here below

    thanks in advance
    S.M.

    The problem is the same either using malloc or calloc (with their respectively syntax)
    1) option 1 with calloc:
    Code:
    _esup1 = (int*) calloc(1,sizeof(int));
    	_esup2 = (int*) calloc(nnodes+1,sizeof(int));
    	_nesup = (int*) calloc(nnodes+1,sizeof(int));
    	_lpoin = (int*) calloc(nnodes+1,sizeof(int));
    	_psup2 = (int*) calloc(nnodes+1,sizeof(int));
    	_npsup = (int*) calloc(nnodes+1,sizeof(int));
    2) option with malloc:
    Code:
    	_esup2 = (int*) malloc((nnodes+1)*sizeof(int));
    	_nesup = (int*) malloc((nnodes+1)*sizeof(int));
    	_lpoin = (int*) malloc((nnodes+1)*sizeof(int));
    	_psup2 = (int*) malloc((nnodes+1)*sizeof(int));
    	_npsup = (int*) malloc((nnodes+1)*sizeof(int));

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Are you sure nnodes is initialised?

    If you do this (and only this) in a new program, does it work?

    The problem is almost certainly down to some mis-use of some memory you've already allocated.
    Typically
    - accessing past the end of the memory you allocated
    - trying to free it twice
    - trying to free something never allocated.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Quote Originally Posted by Salem View Post
    Are you sure nnodes is initialised?

    If you do this (and only this) in a new program, does it work?

    The problem is almost certainly down to some mis-use of some memory you've already allocated.
    Typically
    - accessing past the end of the memory you allocated
    - trying to free it twice
    - trying to free something never allocated.
    Hello Salem, thanks for replying; yes, actually everything was allocated and it works fine on another machine. Anyhow, I think I can solve it by recoding it in a different way.

    Thank you very much
    S.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You mis-understand.
    That it "works" on a single machine is basically down to luck as much as judgement.

    When you add new code, you have a chance of exposing previously hidden bugs.
    When you change your compiler flags (say a release or debug build), you have a chance of exposing previously hidden bugs.
    When you change compiler, you have a chance of exposing previously hidden bugs.
    When you change OS, you have a chance of exposing previously hidden bugs.

    And so it goes on.

    Debugging stops when the program works in a given environment, not when it's bug-free.

    > Anyhow, I think I can solve it by recoding it in a different way.
    I'm sure you can. But unless you understand why you're at the bottom of this particular hole, how will you know what to do to avoid finding yourself back in the same hole at some future time?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Quote Originally Posted by Salem View Post
    You mis-understand.
    That it "works" on a single machine is basically down to luck as much as judgement.

    When you add new code, you have a chance of exposing previously hidden bugs.
    When you change your compiler flags (say a release or debug build), you have a chance of exposing previously hidden bugs.
    When you change compiler, you have a chance of exposing previously hidden bugs.
    When you change OS, you have a chance of exposing previously hidden bugs.

    And so it goes on.

    Debugging stops when the program works in a given environment, not when it's bug-free.

    > Anyhow, I think I can solve it by recoding it in a different way.
    I'm sure you can. But unless you understand why you're at the bottom of this particular hole, how will you know what to do to avoid finding yourself back in the same hole at some future time?
    Hi Salem, yes, it is indeed so; I actually found a bug on a previous code I wrote when I ported it from Linux to Mac. It was actually a bug that for some reason wouldn't raise problems under linux. That helped learning by mistake!

    I actually figured now that the problem originally was not in the function I sent, but in another one where the same malloc were being called. In there there there was a variable that was not being allocated beforehand ("sz"), and that is what caused it to crash.

    Now with sz properly allocated, all those malloc calls stop giving problems!

    thanks for helping and for the hint on good programming
    S.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Excellent job
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Thanks!

    have a nice weekend
    s.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Playing mp3 files in Mac OS X
    By tek in forum C++ Programming
    Replies: 23
    Last Post: 08-30-2003, 01:42 PM
  2. Assembly & Mac OS X
    By kristy in forum Tech Board
    Replies: 2
    Last Post: 07-29-2003, 04:29 PM
  3. Mac Os X Vs Windows
    By hermit in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 09-07-2002, 02:29 PM
  4. download compilers for mac os?
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2002, 08:44 AM
  5. compiled under windows, work for mac os?
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2002, 09:55 AM

Tags for this Thread