Allocate memory question

This is a discussion on Allocate memory question within the C++ Programming forums, part of the General Programming Boards category; Hallo, Is there a difference between doing this: Code: // Allocate memory for the list of directions. CVector4 *direction = ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    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

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    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.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Ah, thank you

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    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

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The return type of generateRayDirections indicates that the function returns a single Vector3D.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    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

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    You should change the return type to Vector3D* instead, and remember to delete[] when you are done.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char ** and argv memory question
    By simo_mon in forum C Programming
    Replies: 3
    Last Post: 08-17-2008, 08:47 AM
  2. Memory Address of Array Question
    By Zeusbwr in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 09:58 AM
  3. can't allocate memory even though have plenty
    By venckman in forum C Programming
    Replies: 3
    Last Post: 04-25-2003, 10:43 AM
  4. when to allocate memory
    By SAMSAM in forum Windows Programming
    Replies: 3
    Last Post: 01-22-2003, 10:40 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21