creating an array in a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rolan
    New Member
    • Jan 2008
    • 13

    creating an array in a function

    I'm trying to create a function which will return a pointer to an array, however this causes a massive number of errors, so I'm wondering if it is possible to do this in c++. This is the code I'm trying to use:

    [PHP]int *getMatrix(int column, int row){
    int sparseMatrix[column][row];
    ...
    return &sparseMatri x[0];
    }[/PHP]


    I want to be able to use this function to create multiple arrays, however when I try to build the project, the declaration of sparseMatrix[][] gives me an error saying I'm allocating a constant size of 0 to it.

    So, is it possible to create a function which can return a pointer to an array, where the array itself was created inside the function?
    Or am I trying to make c++ do something that it isn't intended to do?

    Thanks for any help!
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    You need to use new to create the pointer. As it stands, you have a local pointer to memory allocated on the stack. That memory goes out of scope when the function returns. Using new allocates the memory on the heap and returning the pointer ensures you have access to the memory outside the function. Just make sure you carefully manage your memory to avoid memory leaks.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      It may not be germaine but i`m not sure u can cross pollinate rows and columns like this.e.g. rows come first.....but since all multi dimensional arrays are really single dimensional it may not matter.

      Comment

      • mmk
        New Member
        • Oct 2006
        • 19

        #4
        Originally posted by rolan
        I'm trying to create a function which will return a pointer to an array, however this causes a massive number of errors, so I'm wondering if it is possible to do this in c++. This is the code I'm trying to use:

        [PHP]int *getMatrix(int column, int row){
        int sparseMatrix[column][row];
        ...
        return &sparseMatri x[0];
        }[/PHP]


        I want to be able to use this function to create multiple arrays, however when I try to build the project, the declaration of sparseMatrix[][] gives me an error saying I'm allocating a constant size of 0 to it.

        So, is it possible to create a function which can return a pointer to an array, where the array itself was created inside the function?
        Or am I trying to make c++ do something that it isn't intended to do?

        Thanks for any help!
        Hi rolan,

        I suggest you following code. Anyone please correct me if I am wrong.

        Code:
        int *getMatrix(int column, int row){
            int sparseMatrix =new int [column][row]; 
                ...
               return &sparseMatrix[0];
        }
        Thank you,
        Mmk

        Comment

        • rolan
          New Member
          • Jan 2008
          • 13

          #5
          Thanks so much, using the new operator worked wonderfully.

          Comment

          Working...