943,751 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1221
  • C++ RSS
Mar 27th, 2009
0

2-Dim Dynamic Array;

Expand Post »
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.
Similar Threads
Reputation Points: 7
Solved Threads: 0
Newbie Poster
Mostafa Adel is offline Offline
5 posts
since Feb 2009
Mar 27th, 2009
0

Re: 2-Dim Dynamic Array;

you can't do 2d arrays like that
C++ Syntax (Toggle Plain Text)
  1. char **array = new char*[x];
  2. for(int i = 0; i < x; i++)
  3. 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
Last edited by Ancient Dragon; Mar 27th, 2009 at 6:09 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Mar 27th, 2009
0

Re: 2-Dim Dynamic Array;

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.
Reputation Points: 33
Solved Threads: 9
Junior Poster
DavidB is offline Offline
188 posts
since Jul 2006
Mar 28th, 2009
0

Re: 2-Dim Dynamic Array;

I'm always using the following code if I want to use a 2D Dynamic char array:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. /* Declare the '2D Array' */
  8. char ** ptr = new char * [5];
  9. ptr[0] = new char[20];
  10. ptr[1] = new char[20];
  11. ptr[2] = new char[20];
  12. ptr[3] = new char[20];
  13. ptr[4] = new char[20];
  14.  
  15. /* Put some data in the array */
  16. ptr[0] = "Hello ";
  17. ptr[1] = "wond";
  18. ptr[2] = "er";
  19. ptr[3] = "ful";
  20. ptr[4] = " world !!!";
  21.  
  22. /* Print the array on the screen */
  23. for(int i = 0; i < 5; i++)
  24. cout << ptr[i];
  25.  
  26. cout << endl;
  27.  
  28.  
  29. /* Cleanup */
  30. delete[] ptr;
  31.  
  32. /* Wait for the user to press ENTER */
  33. cin.get();
  34.  
  35. /* Tell the Operating System that everything went well */
  36. return 0;
  37. }
Hope this helps !
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Mar 28th, 2009
1

Re: 2-Dim Dynamic Array;

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;
}
Last edited by mitrmkar; Mar 28th, 2009 at 10:41 am.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 28th, 2009
0

Re: 2-Dim Dynamic Array;

Click to Expand / Collapse  Quote originally posted by mitrmkar ...
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 ...
Last edited by tux4life; Mar 28th, 2009 at 2:49 pm.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Mar 28th, 2009
0

Re: 2-Dim Dynamic Array;

Click to Expand / Collapse  Quote originally posted by tux4life ...
Do you mean the following?
-> Assign using strcopy();
-> Use a loop afterwards to cleanup ALL the memory ...
C++ Syntax (Toggle Plain Text)
  1. // in this case, there are 20 bytes available for use at ptr[0 .. 4],
  2. // one use might be by means of strcpy()
  3. strcpy(ptr[0], "Hello ");
  4. strcpy(ptr[1], "wond");
  5.  
  6. // cleanup ...
  7. for(int i = 0; i < 5; i++)
  8. delete [] ptr[i];
  9.  
  10. // and finally
  11. delete [] ptr;
Last edited by mitrmkar; Mar 28th, 2009 at 3:08 pm.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 31st, 2009
0

Re: 2-Dim Dynamic Array;

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.
Reputation Points: 7
Solved Threads: 0
Newbie Poster
Mostafa Adel is offline Offline
5 posts
since Feb 2009
Mar 31st, 2009
0

Re: 2-Dim Dynamic Array;

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.).
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Binary Tree - Works in C++ 6.0 but doesn't work in VS2008
Next Thread in C++ Forum Timeline: Interesting String Encryption





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC