# Allocate memory question

• 04-12-2007
h3ro
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:)
• 04-12-2007
karas
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.
• 04-12-2007
h3ro
Ah, thank you :)
• 04-12-2007
h3ro
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
• 04-12-2007
CornedBee
The return type of generateRayDirections indicates that the function returns a single Vector3D.
• 04-12-2007
h3ro
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:)
• 04-12-2007
laserlight
You should change the return type to Vector3D* instead, and remember to delete[] when you are done.