-
Allocate memory question
Hallo,
Is there a difference between doing this:
Code:
// Allocate memory for the list of directions.
CVector4 *direction = new CVector4[width * height];
And:
Code:
// Allocate memory for the list of directions.
CVector4 direction[width * height];
Thanks:)
-
Yes. The first one is dynamic allocating (so called heap memory) and requires deleting after use. Otherwise it will produce a memory leek. The second one is freed automatically and the reserved memory is on the stack.
-
-
Ok, I have a problem here, relating to the above question
Code:
// Load the direction list
Vector3D myList = generateRayDirections(400,400,0);
// A function for generating a ray
// Note: this is not a part of Engine::
Vector3D generateRayDirections(int width, int hight, int depth)
{
// An array for holding all the directions for all the rays which
// later will be created
Vector3D *directionTable = new Vector3D[width * hight];
// Variables for holding half the width and hight.
// Is used to set the camera at the center of the screen
float halfWidth = (float)(width/2);
float halfHight = (float)(hight/2);
// We loop through all the pixels starting from left uper
// corner and move to the right. Then a direction is created
// for each of them, making it easy to acces them later
for (int y = 0; y < hight; y++)
{
for (int x = 0; x < width; x++)
{
// Assign direction
// reference which number of the array
// Create a vector with the direction
// Normalize the vector
// Add a vector with the direction to the array
}
}
return directionTable;
}
Here is the errors:
RayTracer.cpp In function `Vector3D generateRayDirections(int, int, int)':
RayTracer.cpp conversion from `Vector3D*' to non-scalar type `Vector3D' requested
-
The return type of generateRayDirections indicates that the function returns a single Vector3D.
-
I have been trying to fix this for a while now, but no sucsess so far. So could anyone please give me a helping hand?
Thanks:)
-
You should change the return type to Vector3D* instead, and remember to delete[] when you are done.