954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using New

Just been looking over Dynamic Memory Allocation, using new.

Heres a snippet of the code i've got.

vertices = new float *[v];
	for (i = 0; i < v; ++i)
	{
		vertices[i] = new float[3];
	}


what i was thinking was could i just do

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

midimatt
Light Poster
49 posts since Mar 2008
Reputation Points: 34
Solved Threads: 4
 

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++.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

what i was thinking was could i just do

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

bugmenot
Posting Whiz in Training
225 posts since Nov 2006
Reputation Points: 53
Solved Threads: 34
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You