Results 1 to 9 of 9

Thread: arrays

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2002
    Location
    england
    Posts
    4

    arrays

    Hi guys, more of a c++ question. created some maths functions in a .h file to give me an array[10[2] that i want to pass back to a .cpp file to use to create a graph using a Bezier curve, however having problems passing or returning the array. any ideas???
    thank you!

  2. #2
    Junior Member Newbie
    Join Date
    Apr 2002
    Location
    Linz, Austria
    Posts
    9

    Re: arrays

    Originally posted by andy-m:
    Hi guys, more of a c++ question. created some maths functions in a .h file to give me an array[10[2] that i want to pass back to a .cpp file to use to create a graph using a Bezier curve, however having problems passing or returning the array. any ideas???
    what exactly is your problem?
    could you show the problematic piece of code?

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: arrays

    More detail would be nice. Without more information about the only thing I could possibly see wrong is that if you are implementing your functions in a .h, you will have problems in the linking stage if you #include that .h in multiple files. (Those functions would attempt to get linked in twice, and you'd get a duplicate symbol error, or something to that effect.)

    So far as passing arrays. Arrays are always passed by pointer. Which makes me think of another potential problem...

    Example
    Code :
    float* myFunction()
    {
         float blah[1024]; // this is created on the stack
     
         // do stuff with it
         return blah;
    } // function exits, memory for blah is cleaned off the stack... 
    // but you have a pointer to it... 
    // you try and use that pointer, but it points to memory that is now unallocated
    // crash!!
    A better way to handle something like that would be like so...

    Code :
    void myFunction(float* blah)
    {
        // manipluate blah when the function exits
        // it will still be there because the memory
        // is allocated outside of this function.
    }
    EDIT: Added code tags to 2nd block of code.

    [This message has been edited by Deiussum (edited 04-09-2002).]
    Deiussum
    Software Engineer and OpenGL enthusiast

  4. #4
    Junior Member Newbie
    Join Date
    Apr 2002
    Location
    england
    Posts
    4

    Re: arrays

    Hi guys, thanks, with the code below i can pass numbers in the 2d arry stored in the .h file to a 1d array in the .cpp (just 1 main file) but i cannot figure out how to get two accross yet??

    got following variables and code in .h

    double Pos[10][2];
    const int q = 10;
    int w = 0;

    double passmeanarray(double X[q])

    {
    X[0] = Pos[w][0];
    w++;
    return X[0];
    }

    /// and following in .cpp

    const int NumPar = 10;
    void displayX2(double[NumPar]);
    double cord[10];

    void displayX2(double* arr)
    {
    for(int col = 0; col < NumPar; col++)
    {
    cout << ", " << passmeanarray(arr);
    //cord[1] = passmeanarray(arr);
    cout << " ! " << cord[0] << endl;
    //cout << " n" << cord[0][1] << endl;
    }
    }
    thank you!

  5. #5
    Senior Member Frequent Contributor
    Join Date
    Aug 2000
    Location
    Cardiff University
    Posts
    653

    Re: arrays

    You may want to look at this code.....
    http://www.cs.cf.ac.uk/User/G.R.Powe...E/dyn_matrix.c

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: arrays

    Andy, I'm not quite sure what you mean by getting "two across." Do you mean you want to return two values?

    Also if you just want to access the array you are putting in the .h file, you can do so directly if you want since it is just a global variable. The way #include works is basically the same as if you took all that code from the #include and pasted it into the .cpp file. The compiler doesn't really see them as 2 separate files.
    Deiussum
    Software Engineer and OpenGL enthusiast

  7. #7
    Junior Member Newbie
    Join Date
    Apr 2002
    Location
    england
    Posts
    4

    Re: arrays

    Thanks guys for all your help, done it now had to turn the 2d array[10][2] into a 1d array[20] , pass it to the cpp file and then convert it back into a 2d array which i can now use to make my curves on my graph. There must be away of passing 2d arrays but i can't find it???? here the code if anyones intrested : -

    in .h

    double Pos[10][2]; //2D Array Used To Store Graph Plots
    const int q = 1; //Int Used In passingmearray Function
    int w = 0; // ""
    int p = 0; // ""

    double passmeanarray(double X[q])

    {
    if ( p == 0 )
    {
    X[0] = Pos[w][0];
    p++;

    return X[0];
    }

    if (p == 1)
    {
    X[0] = Pos[w][1];
    p = 0;
    w++;

    return X[0];
    }

    else
    {
    cout << "INTERNAL MEMORY ERROR" << endl;

    return 0;
    }
    }


    in .cpp

    //Variables Used In Passing Array Over In mathsys
    const int NumPar = 20; // Used In displayX2

    void displayX2(double[NumPar]); //Function Declaration

    double cord[20]; //Double Array Used For Converting 1D Array To 2D Array
    double Finalcord[10][2]; //2D Double Array Used For Final Graph Plots
    int r = 0; //Int Used In displayX2
    int t = 0; // "" "" "" ""


    main()
    {
    displayX2(cord);
    }

    void displayX2(double* arr) //Reads 1D Array From maths.h And Saves Into 2D Array
    {

    for(int col = 0; col < NumPar; col++) //Loop Though 1D Array
    {
    passmeanarray(arr); //Pass Values Over

    if ( r == 0 )
    {
    Finalcord[t][0] = cord[0]; //Save Into 2D Array
    r++;
    }

    else if (r == 1)
    {

    Finalcord[t][1] = cord[0];
    r = 0;
    t++;
    }

    else
    {
    cout << "INTERNAL MEMORY ERROR" << endl; // Error Handler
    }


    }

    /*for (int i=0; i<10; i++)
    {

    cout << " " << Finalcord[i][0];
    cout << ", " << Finalcord[i][1] << endl;
    }
    */
    }
    thank you!

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: arrays

    Ahhh... I think I understand your problem, maybe. This might help. A 2d array is basically the same as a pointer to a pointer.

    So in your case you could do something like so.

    double passArray(double** array)
    {
    }

    BTW, arrays are ALWAYS passed by pointer even if you try and do this.

    double passArray(double array[10])
    {
    }

    In the above example, the 10 is mostly meaningless. You could pass in an array that is size 2 or a size of 100. The compiler won't care. If you try and go past the bounds of the actual size of the array passed in, you'll be flirting with a crash. (i.e. if you pass in an array of size 2 and try and access element 3 there will be a problem even though you specified 10)

    Hope that helps
    Deiussum
    Software Engineer and OpenGL enthusiast

  9. #9
    Senior Member Frequent Contributor
    Join Date
    Aug 2000
    Location
    Cardiff University
    Posts
    653

    Re: arrays

    Have a look at the link which I posted above!!

Similar Threads

  1. arrays of arrays as vertex output/fragment input
    By tsojtsoj in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 05-17-2018, 08:02 PM
  2. How does Uniform block indexing work for arrays of arrays?
    By timothyja1 in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 03-21-2015, 03:51 PM
  3. Vertex Arrays, interleaved arrays, mouseMove func
    By Nibirue in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 02-13-2012, 11:17 AM
  4. Big problem show arrays with vertex arrays
    By Rondey in forum OpenGL: Basic Coding
    Replies: 13
    Last Post: 03-15-2009, 10:09 AM
  5. Vertex Arrays: Interleaved or seperated arrays?
    By Jan in forum OpenGL: Advanced Coding
    Replies: 16
    Last Post: 06-21-2005, 05:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean