gcc malloc problem with Os X

Has anybody else out there had problem with malloc when porting codes from other unix variants to Os X?

My code has a loop in which several MB are allocated at a time, for a total of roughly 500 MB. This is well within the capacity of my Mac Pro which has 16 GB of RAM (dual quad-core architecture), but at some point in the loop the malloc returns a NULL instead of a memory address. I don't understand why malloc isn't giving me access to the memory I need. The code works fine on ubuntu linux machines with far less RAM.

Any insights into this issue would be appreciated.

Thanks

Mac Pro Dual Quad Core, Mac OS X (10.4)

Posted on Jul 8, 2007 2:36 PM

Reply
8 replies

Jul 8, 2007 8:44 PM in response to wavemaster911

I am just guessing here, but perhaps you are having trouble because of differences between 64-bit and 32-bit code? Regular 'ole 32 code on a 32 bit OS might just work fine. But you are probably running 32-bit code on a 64 bit OS. You may (keep in mind, this is a guess) be running into limitations of running 32-bit code on a 64-bit machine. Try specifically compiling as 64 bit and also running it on an older Mac.

Jul 9, 2007 12:57 PM in response to wavemaster911

May be it is a coding error that only shows up on the macintosh. Try this simple test program. In 32 bit mode it should die at about 4GB. In 64 bit mode it should run until it is out of virtual memory. -

#include <stdio.h>
#include <stdlib.h>
main()
{
long n,k,all;
char *m;
long long sum;

sum=0;
all=100000000;
for(n=0;n<100000;++n){
m=(char *)malloc(all);
if(m == NULL){
printf("memory gone\n");
break;
}
for(k=0;k<all;++k)m[k]=k;
sum += all;
fprintf(stderr,"Sum = %lld\n",sum);
}
}

This dum thing puts in a "br" that does not belong and I can not kill.

Jul 9, 2007 12:52 PM in response to etresoft

I tested this 32-bit vs. 64-bit theory on two ubuntu linux machines. The binary code was compiled on the 32-bit machine and then that same binary executable was run on BOTH the 32-bit and 64-bit machines. It ran without error in both cases. However, it still fails in the manner I described in my previous post on Os X. There it is not a simple 32-bit vs. 64-bit issue, but something more specific to Os X and/or the computer architecture.

Jul 9, 2007 2:12 PM in response to wavemaster911

OK. Here is another guess (inspired by IBM.

What does your VM look like on the Mac compared to the Linux machine? I'm guessing that in order to use that 16 GB, you need to make sure you have that much space free.

If not that, there could be an issue with size of int vs long, etc. int may or may not be 64 bits, depending on your settings. For that, you might have to post some code.

Jul 9, 2007 3:06 PM in response to Dale Ranta

Thanks Dale. The code you posted breaks at about 3.5 GB.

In my problematic code returns a NULL pointer at roughly 400 MB... The does image processing. When it has to process 12 image simultaneously it works fine. When it has to process 13 or more images malloc returns a NULL pointer. On Ubuntu linux machines it can process 13 or more images without problems (32 or 64 bit machine w/ 32 bit binary).

Jul 10, 2007 5:38 AM in response to wavemaster911

Actually, your virtual mem is how much disk space you have free. If that isn't the cause, you are going to have to post some code. Just saying "it runs fine on Ubuntu" isn't enough. Have you compared the gcc command line settings between the Mac and Linux? Are they identical? Are the libraries you are using identical? Superficially, MacOS X and Linux look alike. But underneath they are quite different. There may be a setting that works on both. I don't know - I'm out of guesses.

Jul 10, 2007 7:03 AM in response to wavemaster911

One thing that my be your problem is that with most versions of LINUX you can allocate more memory than you actually have and as long as you do not try and access it there is no problem. If you remove the writes to the allocated memory in that test program, it gives inconsistant results on many LINUX systems. So - it is possible that you are allocated extra memory, but not using it and only the Macintosh is having the problem.

You can down load "valgrind" and use it on LINUX to find those kind of problems. It works nicely.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

gcc malloc problem with Os X

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.