By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,327 Members | 1,781 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,327 IT Pros & Developers. It's quick & easy.

need for simple program of arrays

P: 23
declare two dimensional array of type double using dynamic memory allocation then write code to delete this two dimensional array???
Oct 1 '06 #1
Share this Question
Share on Google+
4 Replies


P: 23
declare two dimensional array of type double using dynamic memory allocation then write code to delete this two dimensional array???
double *dPtr[5] = new double[5];

delete[ ] dPtr;
Oct 1 '06 #2

100+
P: 293
D_C
Expand|Select|Wrap|Line Numbers
  1.   int WIDTH;
  2.   int HEIGHT;
  3.  
  4.   cout << "Enter array width: ";
  5.   cin >> WIDTH;
  6.   cout << "Enter array height: ";
  7.   cin >> HEIGHT;
  8.  
  9.   char* array2D = (char*)malloc(WIDTH*HEIGHT*sizeof(char));
  10.   free(array2D);
Oct 2 '06 #3

P: 23
i asked for a program which declares a two dimensional array of type double using dynamic memory allocation and then delete this array but im a begginer in c++ and i dont know how to completly write it ,i tried and thats it
#include<iostream>

using namespace std;

int main()
{


double *dPtr[5]= new double[5];


delete[] dPtr;
cout<<&dPtr[]<<" "<<endl;
return 0;
}
Oct 3 '06 #4

Banfa
Expert Mod 5K+
P: 8,916
Please don't double post

Unfortunately you have chosen to use the non-working code from this thread instead of the working code. There is no such thing as an allocated 2D array, it's in your head.

However you can simulate this imaginary concept like this

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     double *dPtr[5];
  8.     double *data = new double[25];
  9.  
  10.     dPtr[0] = data;
  11.     dPtr[1] = dPtr[0]+5;
  12.     dPtr[2] = dPtr[1]+5;
  13.     dPtr[3] = dPtr[2]+5;
  14.     dPtr[4] = dPtr[3]+5;
  15.  
  16.     delete data;
  17.  
  18. //    cout<<&dPtr[]<<" "<<endl;
  19.  
  20.     return 0;
  21. }
  22.  
I have commented out the cout line because it is not at all clear what you where trying to achieve with it (especially after the data has been deleted).
Oct 3 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.