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.

Recommended Answers

All 8 Replies

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 :)

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.

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 !

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;
}
commented: Thank you very much for correcting me ! +1

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 ...

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;

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.

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.).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.