1

I am trying to initialize all columns and rows of array of large size to 0, without using pointers in C. But sometimes it is getting a crash and sometimes it shows an error message about 'not enough memory'. And I don't want to use pointers in my application.

I have a local variable (inside a function) defined like this:

 double myArray[50][785190]={0}; 

I tried the code above but it's not working.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user2894607
  • 165
  • 4
  • 14
  • 7
    In what way is `double myArray[50][785190] = { 0 };` not working. Is this at file scope or a local variable? You're allocating around 320 MiB; that's too big to fit on most stacks. – Jonathan Leffler Jun 02 '14 at 06:33
  • 2
    "And i dont want to use pointers in my application." Well, tough. That array is way too big. – BonzaiThePenguin Jun 02 '14 at 06:35
  • 4
    If you don't want to use pointers in C, you're going to have a bad time. – user2357112 Jun 02 '14 at 06:37
  • 4
    Most machines have a limit of 8 MiB or less for the stack size. You cannot reliably create a 320 MiB (give or take) array in an 8 MiB stack. If you don't want pointers (a forlorn hope in C), you will have to either make it a file scope or global scope variable — define it outside any function, and make it `static` for file scope or not for global scope. With pointers, you could use dynamic allocation with `malloc()` et al. There are a number of duplicates for this problem — creating a big array that crashes your program. – Jonathan Leffler Jun 02 '14 at 06:42
  • 1
    I REALLY don't suggest allocating 150MB on heap. You should be dynamically allocating that on the stack... – Lord Zsolt Jun 02 '14 at 06:42
  • Stasel- but i need for above mentioned size – user2894607 Jun 02 '14 at 06:43
  • 2
    as @JonathanLeffler said, your problem is not with initializing the array, but with the size of the array – bolov Jun 02 '14 at 06:43
  • 2
    If you need the above mentioned size, you clearly need dynamic allocation. End of story. – Lord Zsolt Jun 02 '14 at 06:43
  • Whoops…@LordZsolt — I've just rediscovered the comment I critiqued. You said _"I really don't suggest allocating 150 MB on heap. You should be dynamically allocating that on the stack"_ That is incorrect; please delete the comment. The problem is that local variables **are** allocated on the stack and 150 (300, 320) MiB is too big for the stack on most machines. You could dynamically allocate it on the heap with `malloc()` et al; you could place it as a global or file scope variable in the 'data' or 'bss' segment. Neither of those is the stack. – Jonathan Leffler Jun 02 '14 at 07:07

2 Answers2

2

I thought you could do this, at least in C++, not sure if/when that was added to C.

double myArray[50][785190]={{0}}; 

But, as you said you're getting out of memory errors. Declaring 300meg on the stack is generally not a good idea. Move that to a global variable (outside of any functions) and it will no longer be on the stack. Or declare it static. In either case your code will not be reentrant with a global or static array but I'm assuming you don't need it to be for such a large array.

Otherwise you can do this to clear it as well

double myArray[50][785190]
memset(myArray, 0, sizeof(myArray));

Though any good compiler will likely generate the same code under the hood for either style.

gman
  • 100,619
  • 31
  • 269
  • 393
0

If you don't want to use pointers in your 2-D array, you will need to know what size your array is ahead of time. If you declare your array as something like this:

double array[x][y];

Then to initialize it you should use a nested for loop like this (with i and j declared elsewhere):

for(i = 0; i<x; i++)  
    for(j = 0; j<y; j++)  
        array[i][j] = 0;  

I hope this helps!

EDIT- As others have pointed out, the issue is with the size, listen to them!

Jack42494
  • 21
  • 1
  • @user2894607's method of allocating an array and initialising it is correct. The issue is with the size of the memory being allocated. – Will Jun 02 '14 at 06:38
  • What are you talking about? He has `double myArray[50][785190]` clearly the size of the array is known ahead of time. – bolov Jun 02 '14 at 06:41
  • I see, I was mistaken about the issue! – Jack42494 Jun 02 '14 at 06:42