10

i ve created two dimensional array inside a function, i want to return that array, and pass it somewhere to other function..

char *createBoard( ){  
  char board[16][10];
  int j =0;int i = 0;
  for(i=0; i<16;i++){
        for( j=0;j<10;j++){   
                board[i][j]=(char)201;
        }   
  }
  return board;
}

but this keeps giving me error

3
  • Can you be more specific about the error? Commented Apr 6, 2009 at 9:07
  • 1
    You should use unsigned char instead, because 201 is not within the range of char. Commented Apr 6, 2009 at 9:13
  • 2
    Also, you are returning the address of the local variable. If you try to access it, your program will crash. Commented Apr 6, 2009 at 9:16

8 Answers 8

11

Yeah see what you are doing there is returning a pointer to a object (the array called board) which was created on the stack. The array is destroyed when it goes out of scope so the pointer is no longer pointing to any valid object (a dangling pointer).

You need to make sure that the array is allocated on the heap instead, using new. The sanctified method to create a dynamically allocated array in modern C++ is to use something like the std::vector class, although that's more complicated here since you are trying to create a 2D array.

char **createBoard()
{
    char **board=new char*[16];
    for (int i=0; i<16; i++)
    {
       board[i] = new char[10];
       for (int j=0; j<10; j++)
         board[i][j]=(char)201;
    }

    return board;
}

void freeBoard(char **board)
{
    for (int i=0; i<16; i++)
      delete [] board[i];
    delete [] board;
}
Sign up to request clarification or add additional context in comments.

8 Comments

it gives me error on the second line, it says " syntax error : missing ';' before '[' "
removes the bracket around the second line, and it works. char **board = new char*[maxX]; at least it doesnt gives me compiler error and outbound error
Yeah sorry about that, I wasn't sitting at a compiler and I had to guess at the syntax for that line.
it might be a good idea to post some code illustrating how to free the board too.
I've added some detail on the free function and fixed the syntax error. I'd like to mention that this style is not really classic C++, it is more C style - the right way to do this in C++ would involve an array class such as vector - there is a reason why the answer by Neil was voted up highly
|
11

The best approach is create a board class and make the ctreateBoard function its constructor:

class Board {
  private:
   char mSquares[16][10];

   public:
    Board() {
        for(int i=0; i<16;i++){
        for( int j=0;j<10;j++){   
                mSquares[i][j]=201;
        }       
    }

   // suitable member functions here
 };

For information on how to use such a class, there is no substitute for reading a good book. I strongly recommend Accelerated C++ by Andrew Koenig and Barbra Moo.

3 Comments

how do i pass it to other function on other class?
A simple way would be to mark mSquares as public. And pass references to Board object to functions.
Why is this a bad way? Please elaborate more.
2

This approach will not work. If you return a pointer to a local variable you'll run into undefined behaviour. Instead allocate an array on heap with new and copy data into it manually indexing it.

Comments

2

I would really recommend using STL vector<> or boost/multi_array containers for this.

If you must use arrays, then I would recommend using a typedef to define the array.

typedef char[16][10] TBoard;

You could also return

 char**

...but then you would need to typecast it to the correct size in order to index it correctly. C++ does not support dynamic multiple dimension arrays.

Also as others have suggested you can't return an object on the stack (i.e., local variable)

2 Comments

cant use boost framework, and they havent teach me about vector.. so i cant use it
Then go with the typedef (if you have a static board size).
1

Don't return pointer to a local variable, as other mentioned. If I were forced to do what you want to achieve, first I'd go for std::vector. Since you haven't learnt std::vector, here is another way:

void createBoard(char board[16][10])
{  
  int j =0;int i = 0;
  for(i=0; i<16;i++){
        for( j=0;j<10;j++){   
                board[i][j]=(char)201;
        }       
  }
}

3 Comments

should be "void createBoard(char (&board)[16][10]);" (reference to array)
qwerty's right: you define your argument as pass-by-value. Pass it by reference and all works fine.
Both of them works in Visual Studio 2008. I'm confused. What is wrong with my implementation? How the array is passed by value?
0

You should return char** instead of char*

1 Comment

trying to do this, changed my grid definition to char *board[16][10]; and the assigning to *board[i][j]=(char)201; still generate compiler error
0

The simple answer to your question is char**.

Having said that, DON'T DO IT ! Your "board" variable won't last outside createBoard().

Use boost::multi_array and pass it as a reference to createBoard() or return it directly (but if you do that, it will be copied).

Comments

0

You must not return a pointer to a functions local variables because this space gets overwritten as soon as the function returns.

The storage associated with board is on the function's stack.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.