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

RE: c++: 2D array -> maybe OT



hi Vikram and Ajay !

	Although the program (Vikram's) looks functionally correct , there
are certain observations in order.

	a) Memory leaks are a major problem with this kind of programming.
{An example of a Memoryleak is when you allocate memory from the heap but do
not release it. This is a serious bug in real-life critical applications and
it is considered to be good practise to avoid such leaks)

	b) Mmory leak in your program will occur since even if I 'delete' or
'delete[]' the pointer (to a pointer to int) i, even then it does not
deallocate the next-level of memory allocated by ctr. So you have to be very
careful deallocating memory. Run any tools such as Purify or BoundChecker or
Electric Fence to detect emmory leaks.

	c) I wonder why this is touted as a C++ program, because there is
noithing specific to C++ over here apart from the new operator used to
allcoated memory. Even that can be substituited by malloc in this case with
appropriate casts and sizeof.
and ok, if you say so, <iostream.h>. The subject line needs a relook.

	d) IF you want to do things the C++ way, things are tough as per the
original specs. Because C++ will not let you overload the [][] operator
directly :) So a choice would be to write an IntMatrix class (preferable a
template, so as not to hardcode 'int') and overload the (which?) operator
and fool the C++ compiler into believing that you are actually overloading
the [][] operator. Finding out (which) is left to you as an exercise. To be
candid, with your competency, I do not think that should be a problem.

		The reason why I sugegst using the framework of the class if
that the destruction and hence the memory deallocation is guaranteed
(no-forgetting) and safe, that is all allocated memory if freed (provided
you write a proper dtor that is :)

	e) OT observation : LIP listmails go to mygarbage2000@xxxxxxxxx !!
Aha !

Warmest regards
Shourya
Shourya.Sarcar@xxxxxxxxxxxx

Global Software Platforms
GE Medical Systems
A1 Golden Enclave, Airport Road, Bangalore, India 560 017 
Tel : +91-80-526 3121|3496 Ext 362 
Surf : www.ge.com| www.gemedicalsystems.com | www.wiproge.com |
www.shourya.com <http://www.shourya.com> 


	

#-----Original Message-----
#From: Vikram Dhaliwal [mailto:mygarbage2000@xxxxxxxxx]
#Sent: Wednesday, September 20, 2000 11:47 PM
#To: linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx
#Subject: Re: [LIP] c++: 2D array
#
#
#You could try something like
#
#int **i;
#int ctr;
#i=new int*[5];
#for(ctr=0;ctr<4;ctr++)
#     {
#          i[ctr]=new int[4];
#     }
#
#Then you can refer to members as i[3][2].
#
#Complete program which I made is
#
#
##include <iostream.h>
#
#void main()
# {
#    int **i;
#    int ctr,count1;
#    i=new int*[5];
#    for(ctr=0;ctr<4;ctr++)
#     {
# i[ctr]=new int[4];
#     }
#    for(ctr=0;ctr<4;ctr++)
#     {
# for(count1=0;count1<3;count1++)
#    cin>>i[ctr][count1];
#     }
#    for(ctr=0;ctr<4;ctr++)
#     {
# for(count1=0;count1<3;count1++)
#    cout<<i[ctr][count1]<<"\t";
#     }
# }
#
#
#It works.
#
#
#
#----- Original Message ----- 
#From: "Dwivedi Ajay kumar" <ajayd@xxxxxxxxxx>
#To: <linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx>
#Sent: Wednesday, September 20, 2000 10:37 PM
#Subject: [LIP] c++: 2D array
#
#
#> hi all,
#> how can we declare a 2 dimensional array in c++ on heap. I am
#> trying to make a Matrix class and need to allocate the array 
#using new in
#> the constructor. 
#> Just wanted to ask if I could do something like:
#> 
#> int ** a;
#> a = new [4][5];
#> 
#> Or some other way to accomplish the above so that I can call a[i][j]
#> later.
#> 
#> -- 
#> 
#> #!!! If anything can go wrong, _FIX_ it. (To hell with MURPHY)
#> 
#> Ajay kumar Dwivedi
#> ajayd@xxxxxxxxxx
#> 
#> 
#> ---------------------------------------------
#> Find out more about this and other Linux India
#> mailing lists at http://lists.linux-india.org/
#
#
#__________________________________________________
#Do You Yahoo!?
#Talk to your friends online with Yahoo! Messenger.
#http://im.yahoo.com
#
#---------------------------------------------
#Find out more about this and other Linux India
#mailing lists at http://lists.linux-india.org/
#