Problem pointer to 2d array

SGDK only sub forum

Moderator: Stef

Post Reply
diskontrol
Newbie
Posts: 3
Joined: Mon Mar 24, 2014 9:41 am

Problem pointer to 2d array

Post 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);
}
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post 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).
diskontrol
Newbie
Posts: 3
Joined: Mon Mar 24, 2014 9:41 am

Post by diskontrol »

Thank you! It worked.
I was confused because this in .net doesn't happen. :P
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post 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 ;)
diskontrol
Newbie
Posts: 3
Joined: Mon Mar 24, 2014 9:41 am

Post by diskontrol »

Yes, you are right. A long time I don't write code in C/C++. Hehe.
Thanks.
Chilly Willy
Very interested
Posts: 2994
Joined: Fri Aug 17, 2007 9:33 pm

Post 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
Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post 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 ;)
Chilly Willy
Very interested
Posts: 2994
Joined: Fri Aug 17, 2007 9:33 pm

Post 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.
Post Reply