you can't do 2d arrays like that
char **array = new char*[x];
for(int i = 0; i < x; i++)
array[i] = new char[y];
Then you destroy it like above, but in reverse order.
I have seen another way to do it, but I never bothered to get the hang of it :)
Ancient Dragon
Retired & Loving It
30,043 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
I'm always using the following code if I want to use a 2D Dynamic char array:
#include <iostream>
using namespace std;
int main(void)
{
/* Declare the '2D Array' */
char ** ptr = new char * [5];
ptr[0] = new char[20];
ptr[1] = new char[20];
ptr[2] = new char[20];
ptr[3] = new char[20];
ptr[4] = new char[20];
/* Put some data in the array */
ptr[0] = "Hello ";
ptr[1] = "wond";
ptr[2] = "er";
ptr[3] = "ful";
ptr[4] = " world !!!";
/* Print the array on the screen */
for(int i = 0; i < 5; i++)
cout << ptr[i];
cout << endl;
/* Cleanup */
delete[] ptr;
/* Wait for the user to press ENTER */
cin.get();
/* Tell the Operating System that everything went well */
return 0;
}
Hope this helps !
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
That is not quite correct, see comments
int main(void)
{
/* Declare the '2D Array' */
char ** ptr = new char * [5];
ptr[0] = new char[20];
ptr[1] = new char[20];
ptr[2] = new char[20];
ptr[3] = new char[20];
ptr[4] = new char[20];
/* Put some data in the array */
// below you lose all the above allocated 20 byte chunks because you
// re-assign the pointers to strings, meaning that you cannot anymore
// delete the allocated memory, which you should do
ptr[0] = "Hello ";
ptr[1] = "wond";
ptr[2] = "er";
ptr[3] = "ful";
ptr[4] = " world !!!";
// instead you should e.g.: strcpy(ptr[0], "Hello ") and so on ..
/* Print the array on the screen */
for(int i = 0; i < 5; i++)
cout << ptr[i];
cout << endl;
// you are not deleting the 20 byte chunks here
/* Cleanup */
delete[] ptr;
/* Wait for the user to press ENTER */
cin.get();
/* Tell the Operating System that everything went well */
return 0;
}
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
That is not quite correct, see comments
int main(void)
{
/* Declare the '2D Array' */
char ** ptr = new char * [5];
ptr[0] = new char[20];
ptr[1] = new char[20];
ptr[2] = new char[20];
ptr[3] = new char[20];
ptr[4] = new char[20];
/* Put some data in the array */
// below you lose all the above allocated 20 byte chunks because you
// re-assign the pointers to strings, meaning that you cannot anymore
// delete the allocated memory, which you should do
ptr[0] = "Hello ";
ptr[1] = "wond";
ptr[2] = "er";
ptr[3] = "ful";
ptr[4] = " world !!!";
// instead you should e.g.: strcpy(ptr[0], "Hello ") and so on ..
/* Print the array on the screen */
for(int i = 0; i < 5; i++)
cout << ptr[i];
cout << endl;
// you are not deleting the 20 byte chunks here
/* Cleanup */
delete[] ptr;
/* Wait for the user to press ENTER */
cin.get();
/* Tell the Operating System that everything went well */
return 0;
}
Thank you very much for letting me know ...
Do you mean the following?
-> Assign using strcopy();
-> Use a loop afterwards to cleanup ALL the memory ...
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
Do you mean the following?
-> Assign using strcopy();
-> Use a loop afterwards to cleanup ALL the memory ...
// in this case, there are 20 bytes available for use at ptr[0 .. 4],
// one use might be by means of strcpy()
strcpy(ptr[0], "Hello ");
strcpy(ptr[1], "wond");
// cleanup ...
for(int i = 0; i < 5; i++)
delete [] ptr[i];
// and finally
delete [] ptr;
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
Why not just allocate a single array and treat it as a x*y space.
for every y spaces is a new x row
Less initializing time spent doing separate allocations, for every new row. Particularly if this code is only for small 2D arrays; for larger amounts of data and more involved code, you'll need to write a container or look into the ones C++ provides(ie. vector, map, stack, string(for all char types), et al.).
MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116