943,686 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 478
  • C++ RSS
May 15th, 2008
0

Using New

Expand Post »
Just been looking over Dynamic Memory Allocation, using new.

Heres a snippet of the code i've got.

c++ Syntax (Toggle Plain Text)
  1. vertices = new float *[v];
  2. for (i = 0; i < v; ++i)
  3. {
  4. vertices[i] = new float[3];
  5. }

what i was thinking was could i just do

c++ Syntax (Toggle Plain Text)
  1. vertices = new float[v][3];

instead of the for loop, are there any benefits for using either or are they exactly the same.

thanks in advance

finally one other minor question, currently all of my classes are in the same .cpp file as my main function, is there a way to put the classes into seperate .cpp files rather like java. i tried this initially but got errors about my code, and after moving them into the same file the program ran perfectly.

- Matt
Last edited by midimatt; May 15th, 2008 at 2:00 am.
Reputation Points: 34
Solved Threads: 4
Light Poster
midimatt is offline Offline
49 posts
since Mar 2008
May 15th, 2008
0

Re: Using New

Well if you declare the pointer the right way, then yes you can allocate the whole 2D array in one step.
float (*vertices)[3];
But this only works when all the minor dimensions are constants.

BTW - Use std::vector in C++.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 15th, 2008
0

Re: Using New

Click to Expand / Collapse  Quote originally posted by midimatt ...
what i was thinking was could i just do

c++ Syntax (Toggle Plain Text)
  1. vertices = new float[v][3];
this one only works if the "3" part is a constant, and always creates a "rectangular" array made of contiguous parts, no pointers involved
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006

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: Threads!
Next Thread in C++ Forum Timeline: stack of stack....





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


Follow us on Twitter


© 2011 DaniWeb® LLC