Thread: Dynamic array Allocation

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    5

    Dynamic array Allocation

    Hey guys,
    Is there any limitation in the maximum memory allocated for arrays. Cos When i execute this program it is breaking in the allocation.
    Here the row size is 735000, if the row size is small say 5000 it is working fine. It is having trouble for bigger size.

    Thanks


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
            int row,column;
            int **p; 
            int i, j;
    		row=735000;
    		column = 5500;
    	
    		p = malloc( row * sizeof(int*) ); 
            if(p != NULL)
            {
                for(i = 0; i < row; i++) 
                {
                 
            	    p[i] = malloc(column * sizeof (int)); 
                    if(p[i] == NULL)
                    {
                        printf("Memory allocation failed in Column: %d",i);
                        printf(" \nExiting....");
                        return 1;
                    }
                }
    
            }
            else
            {
                printf("Memory allocation failed in Row. Exiting....");
                return 1;
            }
    
            for(i=0;i<row;i++)
            {
               for(j=0;j<column;j++)
               {
                   p[i][j]=-1; 
                   printf("%d", p[i][j]);
               }
               printf("\n");
            }
    
            return 0;
     }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are asking for (at least) 16G of memory (735000x5500=4 042 500 000 times at least four bytes per int). Do you have 16G of memory (and an address space that can handle 16G of memory)?

  3. #3
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    Quote Originally Posted by tabstop View Post
    You are asking for (at least) 16G of memory (735000x5500=4 042 500 000 times at least four bytes per int). Do you have 16G of memory (and an address space that can handle 16G of memory)?

    even if he did have big memory for it...... no single human being on earth would use his software

    in the programming world you have to give up one of two if you need a good software: memory or performance
    unfortunately you gave up the two
    Last edited by Hussain Hani; 08-16-2009 at 10:48 PM.
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    Thanks for the reply guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic memory allocation
    By Luciferek in forum C++ Programming
    Replies: 118
    Last Post: 10-02-2008, 11:34 AM
  2. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2005, 12:13 AM
  3. dynamic array
    By linuxdude in forum C Programming
    Replies: 5
    Last Post: 07-13-2004, 02:33 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. dynamic allocation question
    By vale in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 04:23 PM