Hi all,I am having some doubts in C. 1>> My program needs to allocate memory of 256*256 array of integers ie array[256][256]. But it cannot be allocated statically.So pls suggest me how to allocate memory of 256*256 integers dynamically.My code below given is unable to allocate memory and is returning NULL to the array.

int far** array;array=(int far**) calloc(256,256*sizeof(int));

Is there any error in my code? If no, then why calloc() is returning NULL to the array.Expecting the answers Yours Raju

To allocate two dimensional arrays

#include <stdlib.h>
#include <stdio.h>
int main()
{
   int ** array, i;
   array = calloc(256, sizeof(int));
   if (array == NULL)
   {
      printf("No mem!\n");
      return 1;
   }
   for (i=0; i<256; i++)
   {
      array[i] = calloc(256, sizeof(int));
      if (array[i] == NULL)
      {
         printf("No mem!\n");
         return 1;
      }
   }

   return 0;
}

Don't forgett to call free (same as you called calloc)

commented: Good answer, apart from the nit - Salem +2

Thanx Andor,Thanx for ur solution to this problem

To allocate two dimensional arrays

#include <stdlib.h>
#include <stdio.h>
int main()
{
   int ** array, i;
   array = calloc(256, sizeof(int));
   if (array == NULL)
   {
      printf("No mem!\n");
      return 1;
   }
   for (i=0; i<256; i++)
   {
      array[i] = calloc(256, sizeof(int));
      if (array[i] == NULL)
      {
         printf("No mem!\n");
         return 1;
      }
   }
 
   return 0;
}

Don't forgett to call free (same as you called calloc)

Andor, could you please tell me is not necessary to cast while allocating using calloc/malloc? If it does internally, is it same on all compilers?

One commonly encountered difference is that C allows a void* pointer to be assigned to any pointer type without a cast, whereas C++ does not

Read this

Read this

Thanks Andor. Great reply.

> If no, then why calloc() is returning NULL to the array
Well your use of 'far' strongly suggests the fact that you're using an ancient 16-bit compiler rather than a more modern 32-bit compiler.

The maximum single allocation you can have is 64K (your array is 128K).

@ andor
> array = calloc(256, sizeof(int)); This is wrong - it assumes int's and pointers are the same size. It should be array = calloc(256, sizeof(int*)); To save confusion (and even thinking about it), p = calloc ( 256, sizeof *p ); will always be correct, no matter what the type of p is.

commented: good one, hats off to you sir[~s.o.s~] +2

> If no, then why calloc() is returning NULL to the array
Well your use of 'far' strongly suggests the fact that you're using an ancient 16-bit compiler rather than a more modern 32-bit compiler.

The maximum single allocation you can have is 64K (your array is 128K).

@ andor
> array = calloc(256, sizeof(int)); This is wrong - it assumes int's and pointers are the same size. It should be array = calloc(256, sizeof(int*)); To save confusion (and even thinking about it), p = calloc ( 256, sizeof *p ); will always be correct, no matter what the type of p is.

Yes your right. I forgott the sizeof(int) and sizeof(int*) is not same on all machines. You have perfect eyes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.