[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

RE: c++: 2D array



the memcpy feature is cool but beware, if you delete ptr, you have the
problem of dangling pointers with ptr[1,2,3,4]
but anyway, i like the recursive structure :)

shourya

#-----Original Message-----
#From: Sojan James [mailto:sojan@xxxxxxxxxxxxxxxx]
#Sent: Thursday, September 21, 2000 11:06 AM
#To: linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx
#Subject: Re: [LIP] c++: 2D array
#
#
#HI,
#	Here is another way I figured out. The advantage of 
#using this is
#that the entire array can be copied with a single memcpy()
#
#here is the code which explains all
#
#<snip>
##include<iostream>
#
#int main()
#{
#        // create the pointers to rows
#        int ** ptr = new int*[4];
#
#        // let the first row point to the entire data
#        ptr[0] = new int[5 * 4];
#
#        // let each subsequent row index into the
#        // allocated data
#        for(int i = 1; i < 4; i++)
#        {
#                ptr[i] = ptr[i-1] + 5 ;
#        }
#
#        ptr[0][1] = 10;
#        ptr[3][4] = 10;
#
#        cout << ptr[0][0] << endl ;
#        cout << ptr[0][1] << endl ;
#        cout << ptr[3][4] << endl ;
#}
#
#
#even though you can index into the array using ptr[i][j], the 
#entire data
#is pointed to by ptr[0], so copying the array will be much easier.
#
#Cheers,
#
#Soj.
#
#
#-- 
#Sojan James
#National Semi. India Designs Ltd.
#7/6 Brunton Road, Bangalore 560025. India.
#Ph: 5587918/919/920 ext. 308
#
#
#
#
#
#
#---------------------------------------------
#The mailing list archives are available at
#http://lists.linux-india.org/cgi-bin/wilma/LIP
#