Thread: memory allocation for array

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    memory allocation for array

    Hi,
    I was wondering:
    if the memory allocation in the following codes is static or dynamic,
    why the upper limits on their sizes are different,
    why some of them would cause segment fault?
    what you would suggest to allocate array according to different need?

    1.
    Code:
       double data[1048000];
       for(int i=0; i<1048000; i++){
          data[i] = i;
       }
    this gives segfault error at the first iteration ie i=0. If I change size to be 1047000, it will be fine. Is the size too big? What is the maximum size for it?

    2.
    Code:
       int size=1048000;
       double data[size];
       for(int i=0; i<size; i++){
          data[i] = i;
       }
    Same error as the code before. Is the allocation for the array also static?

    3.
    Code:
       int size=atoi(argv[1]);  
       double data [size];
       for(int i=0; i<size; i++){
          data[i] = i;
       }
    Same error as the code before. Is this one static allocation for the array? The size is dynamically determine by command line argument. This one is actually a simplified case of what I met in real today where the double array is of size 3241728 and causing segfault somewhere down the running . I changed the code to the one below and all is fine now.

    4.
    Code:
       int size=atoi(argv[1]);
       double *data = new double[size];
       for(int i=0; i<size; i++){
          data[i] = i;
       }
      delete [] data;
    This one I know is dynamical allocation. It seems that there is virtually no limit on the size of the array.

    Thanks in advance for looking at these somehow tedious things!
    Last edited by lehe; 03-31-2009 at 06:28 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The difference is memory allocated on the stack vs the heap. In the first 3 cases, you are allocating stack memory. The amount of space you have for each stack varies depending on your OS and perhaps what your threading library allows you to do. This value is usually 1-8MB on modern operating systems.

    In the 4th case, you are allocating memory off the heap. In this case you are only limited by the amount of RAM you have up to a certain point (You may even go above the amount of RAM you have depending on virtual memory settings). Depending on your OS, there may also be a per process limitation, but this value is usually very high (on my 32 bit linux OS, I can allocate up to 3.2GB).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM