Using New

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 40
Reputation: midimatt is an unknown quantity at this point 
Solved Threads: 2
midimatt's Avatar
midimatt midimatt is offline Offline
Light Poster

Using New

 
0
  #1
May 15th, 2008
Just been looking over Dynamic Memory Allocation, using new.

Heres a snippet of the code i've got.

  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

  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Using New

 
0
  #2
May 15th, 2008
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++.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Using New

 
0
  #3
May 15th, 2008
Originally Posted by midimatt View Post
what i was thinking was could i just do

  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC