| | |
2-Dim Dynamic Array;
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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
you can't do 2d arrays like that
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
C++ Syntax (Toggle Plain Text)
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
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.
•
•
Join Date: Jul 2006
Posts: 88
Reputation:
Solved Threads: 2
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:
Hope this helps !
C++ Syntax (Toggle Plain Text)
#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; }
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
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.
•
•
•
•
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; }
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."
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
•
•
•
•
Do you mean the following?
-> Assign using strcopy();
-> Use a loop afterwards to cleanup ALL the memory ...
C++ Syntax (Toggle Plain Text)
// 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;
Last edited by mitrmkar; Mar 28th, 2009 at 3:08 pm.
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.
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
Why not just allocate a single array and treat it as a x*y space.
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.).
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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
![]() |
Similar Threads
- Safe Array (C++)
- Programming in VBA reading file into dynamic array (VB.NET)
- array declaration problem (C++)
- Declaring multidimensional array in VBA (Visual Basic 4 / 5 / 6)
- Programming VBA: Reading excel into an Array (Visual Basic 4 / 5 / 6)
- How to read excel cells into Array (Visual Basic 4 / 5 / 6)
- Programming VBA Dynamic Arry of Custom Class Problem (Visual Basic 4 / 5 / 6)
- Sort a dynamic array of structs (Visual Basic 4 / 5 / 6)
- Huge Dynamic arrays in QB45, BC7 (Legacy and Other Languages)
Other Threads in the C++ Forum
- Previous Thread: Binary Tree - Works in C++ 6.0 but doesn't work in VS2008
- Next Thread: Interesting String Encryption
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






