C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-28-2009, 04:45 AM   #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));
simone.marras is offline   Reply With Quote
Old 02-28-2009, 05:04 AM   #2
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 02-28-2009, 05:12 AM   #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.
simone.marras is offline   Reply With Quote
Old 02-28-2009, 05:23 AM   #4
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 02-28-2009, 05:51 AM   #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.
simone.marras is offline   Reply With Quote
Old 02-28-2009, 05:56 AM   #6
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
Excellent job
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 02-28-2009, 05:59 AM   #7
Registered User
 
Join Date: Aug 2006
Posts: 61
Thanks!

have a nice weekend
s.
simone.marras is offline   Reply With Quote
Reply

Tags
calloc, error, malloc, region

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22