Is there any limitation on the maximum size of array in c?

11,800

Solution 1

I am guessing that idata is a local variable. The problem is that local variables are stored on the stack (technically "automatic storage"), and the stack is much smaller than the 6400 megabytes you're trying to allocate on it. Allocating that much storage on it causes a stack overflow.

Try

unsigned char** idata = new unsigned char*[DIM1];

for (int i = 0; i < DIM1; ++i)
    idata[i] = new unsigned char[DIM2];

// or

unsigned char (*idata)[DIM2] = new char[DIM1][DIM2];

To allocate it in the free store and you shouldn't have a problem.

EDIT:

I just looked at the tags and didn't see you were only talking about C. If so, you can do the same thing but use malloc instead of new:

unsigned char** idata = malloc(sizeof(unsigned char*) * DIM1);

for (i = 0; i < DIM1; ++i)
    idata[i] = malloc(DIM2);

// or

unsigned char (*idata)[DIM2] = malloc(DIM1 * DIM2);

And don't forget to free (or delete[] for C++) the memory you allocate to avoid memory leaks.

Solution 2

Here is how you allocate and free a 2D array on the free store in C:

unsigned char (*idata)[1400] = malloc(1600 * 1400);
// ...
free(idata);

Solution 3

If you declare this on stack (e.g. in some function) then yes, it will provide stack overflow.

You can declare it as static ('global variable') or allocate memory dynamically.

The Using malloc for allocation of multi-dimensional arrays with different row lengths question is pretty much about it.

Share:
11,800
Meluha
Author by

Meluha

Updated on June 04, 2022

Comments

  • Meluha
    Meluha almost 2 years

    Possible Duplicate:
    C programming, why does this large array declaration produce a segmentation fault?

    I am reading an image in c language but i am unable to do so as my program is stopping in between... after debugging i found that it is due to array size... is there any restriction on maximum size of array? if i declare array of size 1400X1400 everything works fine but if i define array of size 1600X1400 my program stops working... why it is so... is there any limit imposed by compiler or OS on array size? and if so what is solution for this in c.

        unsigned char idata[1400][1400]; //working fine
        unsigned char idata[1600][1400]; //not working
    
  • Seth Carnegie
    Seth Carnegie over 11 years
    @Flexo usually people tag the question with the language they want the solution in. It was tagged C++, so I answered in C++, and it's not that big a stretch to convert it to C anyway.
  • shirley
    shirley over 11 years
    Sorry, I didn't realize that he was talking about C exactly. If it's c, you can use functions like malloc. While if it's c++, you can use new.
  • Luchian Grigore
    Luchian Grigore over 11 years
    Removed my downvote. The question was tagged both C and C++, but I edited because in the question text it was clearly stated that the language was C. And, frankly, I'm tired of seeing mixed C/C++ tags when they shouldn't :)
  • Seth Carnegie
    Seth Carnegie over 11 years
    @LuchianGrigore yes, it's been so long since I used new (especially for multidimensional arrays) that I completely and utterly forgot the syntax for it. Thank you for your patience.