Dynamic Memory allocation

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Dynamic Memory allocation

 
0
  #1
Oct 2nd, 2009
01 Why i am not able to do this
  1. int* px;
  2. px = new int[2][2];
other the px = new int[2*2]; is there any other method

02 Why i am not able do access through pointer dx by dx[1][2] other than by using dx[1*7+2]
  1. static float mat[5][7] =
  2. {
  3. -5, -4, 0, 0, 0, 0, 0,
  4. 6, 4, 1, 0, 0, 0, 24,
  5. 1, 2, 0, 1, 0, 0, 6,
  6. -1, 1, 0, 0, 1, 0, 1,
  7. 0, 1, 0, 0, 0, 1, 2
  8. };
  9. float *dx=&sim_max[0][0];
  10. cout<<dx[1][0];

03 why i am not able to directly initialize array of new dynamically alloc. mem to
  1. float *dx= new float[5*7]
  2. (
  3. -5, -4, 0, 0, 0, 0, 0,
  4. 6, 4, 1, 0, 0, 0, 24,
  5. 1, 2, 0, 1, 0, 0, 6,
  6. -1, 1, 0, 0, 1, 0, 1,
  7. 0, 1, 0, 0, 0, 1, 2
  8. );
Last edited by Rhohitman; Oct 2nd, 2009 at 3:38 pm.
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Dynamic Memory allocation

 
0
  #2
Oct 2nd, 2009
Well Dynamic 2D arrays take a little more complexity.This is mainly because when you declare something like this

  1. int *p;
  2. p= new int [2][2];
What you are trying to do is assigning a pointer array to a pointer itself.
THe new int [2][2] converts itself into a pointer array in this case it is something like int * [2] So In order to Dynamically Create a 2D array you will need to do something like this.

  1. int *p,**q;
  2. q=new int* [2];
  3. q[0]=new int [2];
  4. q[1]=new int [2];
  5. delete []q[0];
  6. delete [] q[1];
  7. delete [] q;
So now you have a 2d array whose elements you can access.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,969
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is online now Online
Posting Virtuoso

Re: Dynamic Memory allocation

 
0
  #3
Oct 2nd, 2009
I've always found it simpler to allocate a one-dimensional array, and then calculating the row and column manually, it's an easy approach, and it works.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: Dynamic Memory allocation

 
0
  #4
Oct 2nd, 2009
Thanx Sky Diploma
here things got too much complicated
  1. Temp2=T1->dx[i*T1->n+T1->n-1] / T1->dx[i*T1->n+pivot_j];

i guess i have to use the loop for assigning the value inside it..
Last edited by Rhohitman; Oct 2nd, 2009 at 4:30 pm.
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Dynamic Memory allocation

 
0
  #5
Oct 2nd, 2009
Yes, Btw, Dont forget deleting each one of the dynamic pointers . Before you delete the pointer to pointer.
And yea, Loops could assign values.
Another solution would be to use vectors.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 291 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC