2-Dim Dynamic Array;

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 5
Reputation: Mostafa Adel is an unknown quantity at this point 
Solved Threads: 0
Mostafa Adel's Avatar
Mostafa Adel Mostafa Adel is offline Offline
Newbie Poster

2-Dim Dynamic Array;

 
0
  #1
Mar 27th, 2009
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.
With my best wishes=>mostafa_adel01@yahoo.com
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: 2-Dim Dynamic Array;

 
0
  #2
Mar 27th, 2009
you can't do 2d arrays like that
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 88
Reputation: DavidB is an unknown quantity at this point 
Solved Threads: 2
DavidB DavidB is offline Offline
Junior Poster in Training

Re: 2-Dim Dynamic Array;

 
0
  #3
Mar 27th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: 2-Dim Dynamic Array;

 
0
  #4
Mar 28th, 2009
I'm always using the following code if I want to use a 2D Dynamic char array:
  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 !
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: 2-Dim Dynamic Array;

 
1
  #5
Mar 28th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: 2-Dim Dynamic Array;

 
0
  #6
Mar 28th, 2009
Originally Posted by mitrmkar View Post
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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: 2-Dim Dynamic Array;

 
0
  #7
Mar 28th, 2009
Originally Posted by tux4life View Post
Do you mean the following?
-> Assign using strcopy();
-> Use a loop afterwards to cleanup ALL the memory ...
  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 5
Reputation: Mostafa Adel is an unknown quantity at this point 
Solved Threads: 0
Mostafa Adel's Avatar
Mostafa Adel Mostafa Adel is offline Offline
Newbie Poster

Re: 2-Dim Dynamic Array;

 
0
  #8
Mar 31st, 2009
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.
With my best wishes=>mostafa_adel01@yahoo.com
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 949
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: 2-Dim Dynamic Array;

 
0
  #9
Mar 31st, 2009
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.).
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC