954,160 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

2-Dim Dynamic Array;

Please, I need an active solution for 2-Dim dynamic array, the code I have written is that:

int x , y; // size of the the array (Array)
char* array = new char [x][y]; // Decleration of the Dymamic array

There is syntax error in the decleration of the array, while I was declaring 1-Dim dynamic array there wasn't any syntax problem, and I have declared it as follow:

int x; // size of the the array
char* array = new char [x]; // Decleration of the Dymamic array

That code for the 1-Dim array have been compiled without any syntax or symantic error.

Mostafa Adel
Newbie Poster
5 posts since Feb 2009
Reputation Points: 7
Solved Threads: 0
 

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
Team Colleague
30,043 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

To be thorough, you should also wrap the allocation in a try-block and have a catch-block so that, if an exception occurs, it can be properly dealt with.

DavidB
Posting Whiz in Training
211 posts since Jul 2006
Reputation Points: 34
Solved Threads: 10
 

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
 

Thanks to you all.
Now Mr. Ancient Dragon i have a question...
How i can make "for loop " to reach any element in the sub array eg.(array[3][5] row 3 column 5) it is too important.
Thanks for everyone reply my question, It the best fourm over the all.

Mostafa Adel
Newbie Poster
5 posts since Feb 2009
Reputation Points: 7
Solved Threads: 0
 

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
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You