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.

9
  • 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. Commented Jun 2, 2014 at 6:33
  • 2
    "And i dont want to use pointers in my application." Well, tough. That array is way too big. Commented Jun 2, 2014 at 6:35
  • 4
    If you don't want to use pointers in C, you're going to have a bad time. Commented Jun 2, 2014 at 6: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. Commented Jun 2, 2014 at 6:42
  • 2
    as @JonathanLeffler said, your problem is not with initializing the array, but with the size of the array Commented Jun 2, 2014 at 6:43

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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!

3 Comments

@user2894607's method of allocating an array and initialising it is correct. The issue is with the size of the memory being allocated.
What are you talking about? He has double myArray[50][785190] clearly the size of the array is known ahead of time.
I see, I was mistaken about the issue!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.