Page 1 of 1

Problem pointer to 2d array

Posted: Mon Mar 24, 2014 9:45 am
by diskontrol
Hello. I am trying to make a sudoku with SGDK but I am having a problem. I am sure that it's a silly error but I don't know how to solve it.

The problem is that when I try to print the matrix that contains the data of the sudoku, it prints correcty only the half of the matrix.

I would appreciate any help. Thank you.

Code: Select all

#include <genesis.h>

typedef struct
{
	short *board;
} sudoku;

void PaintMatrix(sudoku mySudoku)
{
  short i;
  short j;
  short *p;
  p=mySudoku.board;

  for(i=0;i<9;i++)
  {
	for(j=0;j<9;j++)
	{
        char a = *p+'0';
        VDP_drawText(&a, j*2+2, i*2+2);
        p++;
	}
  }
}

sudoku LoadSudoku()
{
	sudoku mySudoku;

	short board1[9][9]= 	{
		{1, 2, 3, 4, 5, 6, 7, 8, 9} ,
		{2, 2, 3, 4, 5, 6, 7, 8, 8} ,
		{3, 2, 3, 4, 5, 6, 7, 8, 7} ,
		{4, 2, 3, 4, 5, 6, 7, 8, 6} ,
		{5, 2, 3, 4, 5, 6, 7, 8, 5} ,
		{6, 2, 3, 4, 5, 6, 7, 8, 4} ,
		{7, 2, 3, 4, 5, 6, 7, 8, 3} ,
		{8, 2, 3, 4, 5, 6, 7, 8, 2} ,
		{9, 2, 3, 4, 5, 6, 7, 8, 1}
	};

	mySudoku.board = &board1[0][0];

	return mySudoku;
}

int main()
{
	sudoku mySudoku = LoadSudoku();
    PaintMatrix(mySudoku);

	while(1)
	{	   
		//wait for screen refresh
		VDP_waitVSync();
	}

	return (0);
}

Posted: Mon Mar 24, 2014 10:22 am
by Stef
You should not return array from your function.
When you do that :

Code: Select all

sudoku LoadSudoku()
{
   sudoku mySudoku;
   short board1[9][9]=    { ... }
   ...
   return mySudoku;
}
The board1 array is allocated on stack and when you leave the LoadSudoku method the array is lost (actually it is still somewhere in the stack but will be overwritten).
You should create the array as global var or use dynamic memory allocation with MEM_Alloc (but that would be pretty wasteful to use dynamic allocation for this particular case).

Posted: Mon Mar 24, 2014 12:43 pm
by diskontrol
Thank you! It worked.
I was confused because this in .net doesn't happen. :P

Posted: Mon Mar 24, 2014 12:57 pm
by Stef
.net as java are managed languages with garbage collection and i guess you use a "new" keyword somewhere at least to create the object.
Anyway if you come from .net i totally understand you made this error, just think you cannot return "structure" or "object" allocated on stack in C ;)

Posted: Mon Mar 24, 2014 2:37 pm
by diskontrol
Yes, you are right. A long time I don't write code in C/C++. Hehe.
Thanks.

Posted: Mon Mar 24, 2014 6:08 pm
by Chilly Willy
Stef wrote:.net as java are managed languages with garbage collection and i guess you use a "new" keyword somewhere at least to create the object.
Anyway if you come from .net i totally understand you made this error, just think you cannot return "structure" or "object" allocated on stack in C ;)
Unless it's static. It's a common tactic for C++ folks to do something like that in C.

Code: Select all

sudoku LoadSudoku()
{
   sudoku mySudoku;
   static short board1[9][9]=    { ... }
   ...
   return mySudoku;
}
It provides a level of scope they feel lost without. :D

Posted: Mon Mar 24, 2014 7:25 pm
by Stef
The static keyword actually make it "global" (not anymore allocated on stack) so in this case it works but it is always returning the same object ;)

Posted: Tue Mar 25, 2014 8:22 pm
by Chilly Willy
Stef wrote:The static keyword actually make it "global" (not anymore allocated on stack) so in this case it works but it is always returning the same object ;)
Yep! The only difference to a global structure is you can't access it directly globally. You need to pass back the pointer to it like in the code.