1
 int main()
  {

    double *array;
    long int n;
    n=10000000;//10^7
    array = (double *) malloc(n*sizeof(double));
    return 0;
  }

basically, I want to use this code for a really big aray into a 2 dimensional array, which will have dimensions [very large][4].

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • I am not sure on your requirements but I suggest looking at GMP https://gmplib.org/ ( considering your manipulations are mostly in doubles and long ints ) – asio_guy Sep 13 '15 at 13:24
  • 2
    [don't cast the result of malloc in C](http://stackoverflow.com/q/605845/995714) – phuclv Sep 13 '15 at 14:15

3 Answers3

2

If you want a 2D array, then allocate a 2D array. It's that simple.

double (*pArr)[4] = malloc(10000000 * sizeof pArr[0]);

Notes:

0

This seems working on Wandbox.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    double (* array)[4];
    long int n;
    int i, j;
    n=10000000;//10^7
    array =  (double (*)[4]) malloc(n*sizeof(double[4]));

    printf("%u\n",(unsigned int)sizeof(array[0]));
    printf("%u\n",(unsigned int)sizeof(double[4]));
    for (i = 0; i <n; i++) {
        for (j = 0; j < 4; j++) array[i][j] = (double)i * j;
    }
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 4; j++) printf("%f ", array[i][j]);
        putchar('\n');
    }
    for (i = n - 10; i < n; i++) {
        for (j = 0; j < 4; j++) printf("%f ", array[i][j]);
        putchar('\n');
    }
    free(array);
    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • [do not cast the return value of `malloc()`, it is harmful](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). Use `sizeof ptr[0]` instead of `sizeof(ElementType)` for safety reasons. – The Paramagnetic Croissant Sep 23 '15 at 20:45
-1
int n = 100000;
double** array = malloc(sizeof(double*)*n);
for (int i = 0; i < n; ++i)
{
    array[i] = malloc(4*sizeof(double));
}

Also note that we don't cast the malloc's result(Do I cast the result of malloc?).

Community
  • 1
  • 1
Alex Díaz
  • 2,303
  • 15
  • 24