dynamic multidimensional arrays

Feb 16, 2009 at 9:44pm
I tried to make a multidimensional array on the free store.

1
2
3
4
5
6
7
8
cout << "rows:";cin>>rows;
cout << "columns";cin>>columns;
string **a=0;
a=new string*[rows];
for(int i=0;i<rows;i++){a[i]=new string[columns];}
string **b=0;
b=new string*[rows];
for(int i=0;i<rows;i++){b[i]=new string[columns];}


The compiler doesn't complain, but the program crashed!
Can any one help?
Feb 16, 2009 at 9:59pm
I tried your code (using std::string) and got no problem...

BTW, where do you delete the dynamically allocated memory?

Last edited on Feb 16, 2009 at 10:02pm
Feb 16, 2009 at 10:01pm
The problem could be with the sizes ( rows and columns )
Feb 16, 2009 at 10:25pm
OOps, now I feel chicky...

I figured out that the code works fine when I remove these lines(fills the corner with zero's):
1
2
3
4
for(int i=0;i<rows;i++){b[i][0]="0";}
 for(int i=0;i<rows;i++){b[i][columns-1]="0";}
 for(int i=0;i<columns;i++){b[0][i]="0";}
  for(int i=0;i<columns;i++){b[rows][i]="0";}


I forgot to change "rows" to "rows -1", and so I got outside the array-boundaries.

Thanks anyway for your help
Last edited on Feb 16, 2009 at 10:28pm
Topic archived. No new replies allowed.