-1
#include <stdio.h>  
#include <string.h>  
#include <stdlib.h>  

void main() {
   int i,j; 
   int *u = malloc(10000 * 10000 * sizeof(int));   
 for (i=0; i<10000; i++)
   {
       for(j=0;j<10000;j++)
       {
            u[i][j]=i+j;   
       }
   }
    free(u); 
}

I edited my program. when compiling this program, I get an error "subscripted value is neither array nor pointer nor vector". how can i allocate memory?

Armaa
  • 625
  • 1
  • 5
  • 13
  • 3
    Possible duplicate of [Segmentation fault on large array sizes](http://stackoverflow.com/questions/1847789/segmentation-fault-on-large-array-sizes) – Shridhar R Kulkarni May 07 '17 at 17:50
  • I think the standard stack size on windows is about 8MB, assuming you're on a 32bit system, `double u[10000][10000];` attempts to allocate around 48MB. – George May 07 '17 at 17:55
  • i know how to use malloc for allocate memory for array, but i don't know how can i allocate memory for matrix? – Armaa May 07 '17 at 17:56
  • Please don't edit the question so that it suddenly asks a new question entirely. This creates a mess out of the posted answers. After your edit, neither the question nor any answers make any sense. What you should have done here was to create a new question. – Lundin May 08 '17 at 14:23

2 Answers2

1

You have allocated memory for a single dimensional array and you are trying to use it as a two-dimensional array. There is a slight alteration which you need to do to your code:

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

int main() {
   int i,j; 
   int *u = malloc(10 * 10 * sizeof(int));   
   for (i=0; i<10; i++)
   {
       for(j=0;j<10;j++)
       {
            u[10*i +j]=i+j; // this is how you can use it   
       }
   }
   for (i=0; i<10; i++)
   {
       for(j=0;j<10;j++)
       {
            printf("%d ",u[10 *i +j]);   
       }
       printf("\n");
   }
   free(u); 
   return 0;
}

Note that I have used size 10*10, you can do the same for whatever size you need.

Check-here

Rishi
  • 1,387
  • 10
  • 14
-1

You can't allocate large arrays on heap directly in the declaration. You can allocate large arrays using malloc as follows

#include <stdlib.h> int *matrix = malloc(ROW * COLUMNs * sizeof(int));

Always use column major order to search for elements. Explanation for column major order can be found here Accessing elements in a matrix

Here size = 10000 Always after you complete your task,free the memory

free(matrix);

bigbounty
  • 16,526
  • 5
  • 37
  • 65