Hi!
Can anyone, please, write me an example of initialization of double pointer with new?
char **tableOfStrings = new ...???
Thanks!
Hi!
Can anyone, please, write me an example of initialization of double pointer with new?
char **tableOfStrings = new ...???
Thanks!
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
You would do something like this:
Code:#include <iostream> int main(void) { int np = 10; int *p; int **dp; p = new int; dp = new int *; if(p == NULL || dp == NULL) { std::cout<<"Could Not Allocate Memory"<<std::endl; std::cin.get(); return 0; } p = &np; dp = &p; std::cout<<np<<std::endl; std::cout<<*p <<std::endl; std::cout<<**dp<<std::endl; **dp = 15; std::cout<<np<<std::endl; std::cout<<*p <<std::endl; std::cout<<**dp<<std::endl; delete p; delete dp; std::cin.get(); return 0; }
Last edited by prog-bman; 01-19-2005 at 04:29 AM.
Woop?
delete p;
delete dp;
Except you trashed your allocated pointers with
p = &np;
dp = &p;
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
>Can anyone, please, write me an example of initialization of double pointer with new?
Code:const int len = 80; char **tableOfStrings = new char[10]; for (int i=0; i<10; i++) tableOfStrings[i] = new char[len];
Swoopy: You mean,
>Can anyone, please, write me an example of initialization of double pointer with new?Code:char **tableOfStrings = new char*[10]; for (int i=0; i<10; i++) tableOfStrings[i] = new char[len]; //Work with your tableOfStrings for(int i = 0; i < 10; i++) delete[] tableOfStrings[i]; delete tableOfStrings;
Lightning fast method, but don't try this at home otherwise your instructor will shoot you!
Code:const int rows = 500; const int maxSizeOfEachRow = 200; char** tableOfStrings = (char**)(new char[rows * sizeOfRow]); //... do something with your table of strings delete[] tableOfStrings[0];
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
>Swoopy: You mean,
Yes, thanks Hunter, as one of our other members would say, I was on the phone at the time.
I get a debug assertion on delete when I try that code, Hunter (using VC6). After poking at it so that the delete worked, I got access violations when I tried to use tableOfStrings[x][y] to access an individual character.Am I doing something wrong, or should I not expect this sort of thing to allow individual character access?Code:int main() { const int rows = 500; const int maxSizeOfEachRow = 10; char** tableOfStrings = NULL; tableOfStrings = (char**)(new char[rows * maxSizeOfEachRow]); //... do something with your table of strings // tableOfStrings[0][0] = 'a'; //causes bad things to happen tableOfStrings[0] = "123456789"; tableOfStrings[1] = "abcdefghi"; cout << tableOfStrings[0] << endl; cout << tableOfStrings[1] << endl; delete tableOfStrings; // delete[] tableOfStrings[0]; //debug assertion return 0; }
Last edited by pianorain; 01-19-2005 at 10:50 AM. Reason: Forgot to ask my question
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
> Am I doing something wrong
Actually, you're doing everything wrong.
> tableOfStrings = (char**)(new char[rows * maxSizeOfEachRow]);
If you're casting new, think that you're doing something very wrong.
In this case, you should have written
Code:tableOfStrings = new char*[rows];
Now that's only part of the story, now allocate each row as well
Code:for ( int r = 0 ; r < rows ; r++ ) tableOfStrings[r] = new char[maxSizeOfEachRow];
Now you can do this
Code:tableOfStrings[0][0] = 'a';
> tableOfStrings[0] = "123456789";
Whilst this is legal, you've just leaked some memory, and set up a failure when you come to delete this memory.
Code:strcpy( tableOfStrings[0], "123456789" );
> delete tableOfStrings;
Nested structures are freed in reverse order.
Code:for ( int r = 0 ; r < rows ; r++ ) delete [] tableOfStrings[r]; delete [] tableOfStrings;
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Oops. I remember this method being posted some time ago as a faster method of allocating a 2D array, though apparently I remembered the method wrong. It should really be:> tableOfStrings = (char**)(new char[rows * maxSizeOfEachRow]);
If you're casting new, think that you're doing something very wrong.
My apologies for the confusion. And this still probably shouldn't be done unless what you're doing is time-critical anyway, which I highly doubt it is at this point. It's just something to keep at the back of your mind until the day comes when you'll actually need to use it.Code:char** tableOfStrings = new char*[rows]; char* bigMemoryChunk = new char[rows * rowLength]; for(int i = 0; i < rows; ++i) tableOfStrings[i] = bigMemoryChunk + (rowLength * i); ... delete[] bigMemoryChunk; delete[] tableOfStrings;
Last edited by Hunter2; 01-19-2005 at 11:11 AM.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.