-4

Is there a way to allocate an array with this size:

unsigned long M[2000][900000] ;

This is what I get when I run the program (no errors during compilation).

enter image description here

Processus arrêté (Process stopped)  
alk
  • 69,737
  • 10
  • 105
  • 255

1 Answers1

6
unsigned long (*pM)[2000][900000] = malloc(sizeof *pM);

does the job.

Use it like this

#define ROWS_MAX (2000)
#define COLUMNS_MAX (900000)

...

unsigned long (*pM)[ROWS_MAX][COLUMNS_MAX] = malloc(sizeof *pM);

/* 1st test whether the allocation succeeded! */
if (NULL == pM)
{
  perror("malloc() failed");
  exit(EXIT_FAILURE);
}

/* Then initialise the array. */
for (size_t row = 0; row < ROWS_MAX; ++row)
{
  for (size_t column = 0; column < COLUMNS_MAX; ++column)
  {
    (*pM)[row][column] = 42;
  }
}

/* Do something ... */
...

/* Deallocate, free the memory. */
free(pM);

An alternative approach using more than one block or memory would be using a scattered/sparse array:

unsigned long ** ppM = malloc(ROWS_MAX * sizeof *ppM);
if (NULL == ppM)
{
  perror("malloc() for row pointers failed");
  exit(EXIT_FAILURE);
}

for (size_t row = 0; row < ROWS_MAX; ++row)
{
  ppM[row] = malloc(COLUMNS_MAX * sizeof *ppM[row]);
  if (NULL == ppM[row])
  {
    perror("malloc() for a column failed");
    exit(EXIT_FAILURE);
    /* If not exiting the process here (but probably return from the function
       we are in), we need to perform a clean-up on what had been allocated 
       so far. See below code for free()ing it as a hint how to approach this. */
  }
}

/* Then initialise the array. */
for (size_t row = 0; row < ROWS_MAX; ++row)
{
  for (size_t column = 0; column < COLUMNS_MAX; ++column)
  {
    ppM[row][column] = 42; /* Note the difference how to access the array. */
  }
}

/* Do something ... */
...

/* Deallocate, free the memory. */
/* Free columns. */
for (size_t row = 0; row < ROWS_MAX; ++row)
{
  free(ppM[row]);
}

/* Free row pointers. */
free(ppM);
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
alk
  • 69,737
  • 10
  • 105
  • 255
  • 2
    Also with such a `malloc()` checking result is not null seems to me mandatory. – jdarthenay Apr 23 '16 at 10:54
  • it does not work : malloc() failed: Cannot allocate memory – Abderrahmane Mechri Apr 23 '16 at 11:01
  • .. or go for a scattered array. Updating my answer. – alk Apr 23 '16 at 11:05
  • @AbderrahmaneMechri: So you want to increase the OS's swap space. Or have a look a what `ulimit` tells you on the currently configured maximum allowed memory per process. – alk Apr 23 '16 at 11:16
  • I think that you meant "Sparse Array", no? – David Hoelzer Apr 23 '16 at 11:22
  • I am no native English speaker ... would "*Sparse*" be the official jargon, yes?-S @DavidHoelzer – alk Apr 23 '16 at 11:27
  • @alk : max memory size (kbytes, -m) unlimited – Abderrahmane Mechri Apr 23 '16 at 11:35
  • @AbderrahmaneMechri: Does the 2nd approach as per my answer work? How much swap space does the OS has configured? – alk Apr 23 '16 at 11:36
  • @alk it work but it's block now in the initialisation i have the same message error : process stopped is there any timeout or something like this ? – Abderrahmane Mechri Apr 23 '16 at 12:16
  • @AbderrahmaneMechri: It looks as if the blocks to be alocated are still to large. You either want to get more RAM, or make up different data structure to hold those 2000 * 900000 * 8 byte (per `unsigned long`) = 14GBytes! – alk Apr 23 '16 at 12:19
  • @AbderrahmaneMechri: For the reason why it errors during initialisation and not during allocation you might like to read here: http://www.win.tue.nl/~aeb/linux/lk/lk-9.html#ss9.6 (found the link here: http://stackoverflow.com/questions/19148296/linux-memory-overcommit-details) How to steer over-commiting behaviour on Linux you want to have look here: https://www.kernel.org/doc/Documentation/vm/overcommit-accounting – alk Apr 23 '16 at 12:23
  • And again: How much swap space does the OS has configured? – alk Apr 23 '16 at 12:27