Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C++ 2D arrays

  • 14-03-2006 3:31pm
    #1
    Registered Users, Registered Users 2 Posts: 1,997 ✭✭✭


    I'm new to c++ and I'm trying to make 3 2D arrays.

    When I define the 2d arrays statically I can make the 3 arrarys contain about 150000 elements each before I start getting memory problems. I did the same program in java and could have over 6 million elements for each array.

    when I define the 2d arrays dynamically I can only have about 100 elements.

    Why is there such a difference between 1) c++ and java, and 2) between the static and dynamically assigned arrarys in c++.

    Is my code to blame?

    here's my c++ static array
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	unsigned short width = 640, height = 240;
    	if( width > 0 && height > 0 )
    	{
    		unsigned short valR[640][240];
    		unsigned short valG[640][240];
    		unsigned short valB[640][240];
    		for( unsigned short j = 0; j < height; j++ )
    		{
    			for( unsigned short i = 0; i < width; i++ )
    			{
    				valR[i][j] = 255;
    				valG[i][j] = 255;
    				valB[i][j] = 255;
    			}
    		}
    		for( unsigned short j = 0; j < height; j++ )
    		{
    			for( unsigned short i = 0; i < width; i++ )
    			{
    				cout << "valR: " << valR[i][j] << ", valG: " << valG[i][j] << ", valB: " << valB[i][j] 
    							<< ", i: " << i << ", j: " << j << endl;
    			}
    		}
    	}
    	else
    	{
    		cout << "error12" << endl;
    	}
    	
    	cin.get();
    	return 0;
    }
    

    and the dynamic array
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int i, j;
    	
    	short width = 10, height = 10;
        
        short ** valR;
    	short ** valG;
    	short ** valB;
        
        valR= new short* [height]; 
    	valG = new short* [height];
    	valB = new short* [height];
        
        for (i=0; i<width; i++)
    	{
                     valR[i]= new short [width]; 
    		valG[i] = new short [width];
    		valB[i] = new short [height];
    	}
    
        for (i=0; i<width; i++) 
    	{
    		for (j=0; j<height; j++)
    		{
    			valR[i][j] = 255;
    			valG[i][j] = 255;
    			valB[i][j] = 255;
    		}
    	}
    
        for (i=0; i<width; i++)
    	{
            for (j=0; j<height; j++)
    		{
    			cout << "valR: " << valR[i][j] << ", valG: " << valG[i][j] << ", valB: " << valB[i][j] 
    							<< ", i: " << i << ", j: " << j << endl;
    		}
        }
    
        for (i=0; i<width; i++)
    	{
            delete [] valR[i];
    		delete [] valG[i];
    		delete [] valB[i];
    	}
    
        delete [] valR;
    	delete [] valG;
    	delete [] valB;
    	
    	cin.get();
        return(0);
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    What do you mean by memory problems?

    As far as I know the default windows stack size is 1MB, so allocating them on the stack (ie statically) you would hit the limit at around 3 150k arrays of shorts (2 bytes) alright - after this you would probably get a stack overflow error of some sort. You could test this by increasing the stack size of the program in your linker and see if you can make bigger arrays.

    As for the dynamic one, you seem to be getting you widths and your heights mixed up during memory allocation - with the wrong values you could end up writing past the end of your array to unallocated memory, which can make your program crash in all sorts of weird and wonderful ways.


  • Registered Users, Registered Users 2 Posts: 1,997 ✭✭✭The_Bullman


    satchmo wrote:
    As for the dynamic one, you seem to be getting you widths and your heights mixed up during memory allocation - with the wrong values you could end up writing past the end of your array to unallocated memory, which can make your program crash in all sorts of weird and wonderful ways.

    I looked over that again and you were right. I was all over the place with the widths and heights. When I posted earlier today I was after tearing my hair(what's left of it) out and couldn't find where I was screwing up. And I was getting some bizarre error messages too :)

    Thanks for the help


  • Registered Users, Registered Users 2 Posts: 1,275 ✭✭✭bpmurray


    And, of course, you could create your array much easier without any loops, etc.
    short *valR = new short[width*height];
    short *valG = new short[width*height];
    short *valB = new short[width*height];
    

    The fact that you address them as valR[j] is irrelevant - it could just as easily be something like *(valR+x*width+y). The point is that it's just a chunk of memory allocated for your array, so it's more efficient to allocate it all at once, rather than allocating it one row at a time.


Advertisement