Thread: Allocate memory question

  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,895
    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
    28,413
    You should change the return type to Vector3D* instead, and remember to delete[] when you are done.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 11:40 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM