The memory allocation and deletion using new and deletion for 2d array

64 views
Skip to first unread message

M Arvind

unread,
Jun 1, 2006, 4:00:17 AM6/1/06
to ImageApprentice
New to c++ and OOP. I need help
I'm trying to code using arrays that will do the following:
-memory allocation for a 2d array using new.
-Removal of memory allocated for a 2d array.
-List books on a given subject or author in alphabetical order
I'm totally buffed. I don't even know where to start.
All help will be appreciated.

Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

divyar...@gmail.com

unread,
Jul 22, 2006, 2:16:17 PM7/22/06
to Image-Apprentice
//## Beginning of code
int** array_2d;
int height = 6; // user's choice
int width = 7; // user's coice
array_2d = new int*[height]; //allocate height
// now allocate width
for(int j=0; j<height;j++) array_2D[j] = new int[width];

//sample usage
for(j=0; j<height; j++)
{
for(int i=0; i<width; i++)
{
//use as per needed
array_2d[j][i] = 0; //set all to 0
}

}

// In C/C++ deleting memory
// is user's responsibility (unlike Java)
for(j=height-1; j>= 0; j--)
delete[] array_2d[j]; //delete rows

delete[] array_2d;//delete the poiter to pointer

//## End of code


*******************************************************
*******************************************************

> -List books on a given subject or author in alphabetical order--
C - Kernighan and Richie
C++ - Stroustrup
(both are the creators of the respective languages so the books are a
bit advanced)

> I'm totally buffed. I don't even know where to start.--
You have reached the right place :)

> All help will be appreciated.--
Hope that helped..!


--
warm regards,
Divya Rathore
(remove underscores for email ID)

Reply all
Reply to author
Forward
0 new messages