Hi!
Can anyone, please, write me an example of initialization of double pointer with new?
char **tableOfStrings = new ...???
Thanks!
Printable View
Hi!
Can anyone, please, write me an example of initialization of double pointer with new?
char **tableOfStrings = new ...???
Thanks!
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;
}
delete p;
delete dp;
Except you trashed your allocated pointers with
p = &np;
dp = &p;
>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];
>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;
}
> 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;
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:Quote:
> 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;