arrays !!

Discussion in 'C' started by Jatsui, Oct 20, 2008.

  1. Jatsui

    Jatsui New Member

    Joined:
    Feb 1, 2008
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    London, UK
    Hello ppl,

    Does anyone know the largest 2D array possible in `C`.
    So if there is arr[a], what are the limitations on the values of a and b ??

    Cheers guys, appreciate it as always !!
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Depends on the platform. a and b are integers, so presumably the maximum allowed by the language is 2^32-1. But (2^32-1)^2 is a LOT of memory, 16 exabytes to be precise, so the chances are you're limited by RAM first, then secondly stack or heap space depending how you declare it.
     
  3. Jatsui

    Jatsui New Member

    Joined:
    Feb 1, 2008
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    London, UK
    So I take it that I need to check the following for array size restrictions..
    1) RAM
    2) Stack/heap space depending on declaration


    Can you explain a bit about point 2) - What do you exactly mean by how I declare it ??


    Thanks again.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Code:
    
    int main()
    {
      int *arr1=malloc(100*sizeof(int));
      int arr2[10][10];
      return 0;
    }
    
    arr1 will be on the heap, arr2 on the stack. How you declare variables can determine where in memory they are kept. There are different types of memory; local variables go on the stack and are overwritten as functions call/return, globals go in a different type of memory (BSS I think), and malloc/new'd memory is taken off the heap.

    So if you have a small stack and a large heap you will need to malloc large arrays. Conversely if you have a large stack and a small heap then you can plonk the array on the stack, provided that you don't want to keep it after the function has returned.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice