Malloc function returning NULL

Hi All,

I am using malloc function for allocating dynamic memory.

When I am using below code on Linux server its working fine, but When I am trying the same code on HP UNIX server its returning NULL.

below is a fragment of code in which it is giving problem.

    tmp = (format_tree *)malloc(sizeof(format_tree));
    if(tmp == NULL)
    {
       printf("Cannot create memory for tmp*2\n");
       exit(EXIT_STATUS_FAILURE);
    }

Any ideas abt this??

Please help me as it is urgent.

Can you post the entire code if it isnt too big...

I would look outside C for the answer. Your account on HPUX may have very stentorian resource limits. What does

ulimit -a

show on both boxes? Plus

errno == ENOMEM 

if there is nothing wrong other than memory -- my assumption since it works in Linux.

I think you are bumping heads with a memory limit on HPUX. If it is a soft limit try calling setrlimit() to raise it in your code.

Thnx for the replies guys

@Shamrock
Entire code is actually quite big so can't post it here, although below is the loop
from where I took the fragment earlier. What this loop is doing is, its parsing a CSV file into tree structure
based on some logic.

while(!(feof(fformat)))
 { 
  i1++;
  if (feof(fformat)) 
  {
     break;
  }
  preseq = curseq; 
  fgets(getline1,max_length,fformat);
  strcpy(chklist,getline1);
 
  curseq = numpts(getline1);
  if(curseq == preseq+1)/* this for child*/
  { //printf("\n chkpt1"); 
   tmp = (format_tree *)malloc(sizeof(format_tree));
      if(tmp == NULL)
      {
      printf("Cannot create memory for tmp*1\n");
      exit(EXIT_STATUS_FAILURE);
      }
   tmp->brother = NULL;    
   tmp->child = NULL;
 
   tmp->level = curseq;
   cur_add->child = tmp;
   tmp->parent = cur_add;
   loadstruct(tmp);  
   cur_add = tmp;
  }
  else
  { //printf("\n chkpt2");  
   if(preseq == curseq)/* this for the same level*/
   { //printf("\n chkpt2.1");
   tmp = (format_tree *)malloc(sizeof(format_tree));
    if(tmp == NULL)
    {
     //tmp = (format_tree *)my_malloc(sizeof(format_tree));
       printf("Cannot create memory for tmp*2\n");
       exit(EXIT_STATUS_FAILURE);
    }
    tmp->brother = NULL;
    tmp->child=NULL;
 
                tmp->level = curseq;
    cur_add->brother = tmp;
          tmp->parent = cur_add->parent;
    loadstruct(tmp);
 
       cur_add = tmp;   
 
   }
   else
   {//printf("\n chkpt3");
    tmp = (format_tree *)malloc(sizeof(format_tree));
    if(tmp == NULL)
    {
       printf("Cannot create memory for tmp*3\n");
       exit(EXIT_STATUS_FAILURE);
    }
    tmp->brother = NULL;
    tmp->child=NULL;
                tmp->level = curseq;
 
    for(i=0;i<(preseq-curseq);i++)
    {
     cur_add = cur_add->parent;
    }
 
    cur_add->brother=tmp;
    tmp->parent = cur_add->parent;  
    loadstruct(tmp);
    cur_add = tmp; 
   }
 
  }
 
 }

@Jim
I executed the command "ulimit -a" on both servers and below are the results:
HPUX:

uxndap02:/app/rafms>ulimit -a
time(seconds)        unlimited
file(blocks)         unlimited
data(kbytes)         3145728
stack(kbytes)        131072
memory(kbytes)       unlimited
coredump(blocks)     4194303
nofiles(descriptors) 4096

Linux:

[r@SOURCE]$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 257697
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 16384
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 2047
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

@OP:

I was only interested in the definition of the format_tree structure i.e. what its members are. Also are you compiling the source code for 32 or 64 bit mode and how much physical memory is installed on each of the *nixes...