944,052 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 565
  • C++ RSS
Oct 2nd, 2009
0

Dynamic Memory allocation

Expand Post »
01 Why i am not able to do this
C++ Syntax (Toggle Plain Text)
  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]
cpp Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Oct 2nd, 2009
0

Re: Dynamic Memory allocation

Well Dynamic 2D arrays take a little more complexity.This is mainly because when you declare something like this

C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 2nd, 2009
0

Re: Dynamic Memory allocation

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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Oct 2nd, 2009
0

Re: Dynamic Memory allocation

Thanx Sky Diploma
here things got too much complicated
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Oct 2nd, 2009
0

Re: Dynamic Memory allocation

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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: deallocate allocated memory
Next Thread in C++ Forum Timeline: Search position for date in file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC