www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Issue De-referencing a Pointer to a Struct in an Array

reply Pen Hall <aidencbernstein gmail.com> writes:
Hello!
In my program, I have defined a struct called `Tile` in the 
global scope. In my `main()` function I create a 3d array of 
`Tile`s called `board`. I then have a function called 
`loopBoard()` that loops through certain parts of `board`. I then 
create a reference to the `Tile` struct at the current position 
of `board`. This all works until I try to de-reference that 
pointer. The result compiles, but I get the exit code 
`-1073741819` whenever I run it. Below is the function in 
question. Do any of you have literally any clue about where I'm 
screwing up?

```d
void loopBoard(Tile[8][8][8]* board, void function(int, int, int, 
Tile*) func){
	for(int number = 7; number >= 0; number--){
		writeln(number);
		for(int symbol = 7 - number; symbol < 8; symbol++) {
			writeln("  " ~ to!string(symbol));
			//((x % y) + y) % y to make modulo work as expected
			int letter = (((symbol - 7) % 7) + 7) % 7;
			if(letter == 0 && symbol != 0)
				letter += 7;

			int[2] lArr;
			lArr[0] = letter;
			lArr[1] = (letter != 7) ? letter + 1 : -1;
			writeln("    [" ~ to!string(lArr[0]) ~", " ~ 
to!string(lArr[1]) ~ "]");

			for(int i = 0; i < lArr.length; i++){
				
				Tile * tile = (board[number][symbol][i]).ptr;

				//program runs well until I reach this point, then it crashes
				writeln("\t", *tile);
				func(number,symbol,lArr[i], tile);
			}

			
		}
	}
}
```
Jul 14 2023
parent reply Pen Hall <aidencbernstein gmail.com> writes:
On Friday, 14 July 2023 at 16:57:33 UTC, Pen Hall wrote:
 Hello!
 In my program, I have defined a struct called `Tile` in the 
 global scope. In my `main()` function I create a 3d array of 
 `Tile`s called `board`. I then have a function called 
 `loopBoard()` that loops through certain parts of `board`. I 
 then create a reference to the `Tile` struct at the current 
 position of `board`. This all works until I try to de-reference 
 that pointer. The result compiles, but I get the exit code 
 `-1073741819` whenever I run it. Below is the function in 
 question. Do any of you have literally any clue about where I'm 
 screwing up?

 [...]
I realized a slight issue with this code is that `board[number][symbol][i]` refers to an array... somehow. I genuinely have no idea as to why this is.
Jul 14 2023
parent reply Pen Hall <aidencbernstein gmail.com> writes:
On Friday, 14 July 2023 at 17:14:01 UTC, Pen Hall wrote:
 On Friday, 14 July 2023 at 16:57:33 UTC, Pen Hall wrote:
 Hello!
 In my program, I have defined a struct called `Tile` in the 
 global scope. In my `main()` function I create a 3d array of 
 `Tile`s called `board`. I then have a function called 
 `loopBoard()` that loops through certain parts of `board`. I 
 then create a reference to the `Tile` struct at the current 
 position of `board`. This all works until I try to 
 de-reference that pointer. The result compiles, but I get the 
 exit code `-1073741819` whenever I run it. Below is the 
 function in question. Do any of you have literally any clue 
 about where I'm screwing up?

 [...]
I realized a slight issue with this code is that `board[number][symbol][i]` refers to an array... somehow. I genuinely have no idea as to why this is.
I think i figured out my issue... The issue was that 'board' is a pointer, and all of the ways I tried to de-reference it, failed. I just tried the form `(*board)[number][symbol][letter]` which worked to de-reference it. `&(*board)[number][symbol][letter]`, of course, works to get a pointer to that spot in memory.
Jul 14 2023
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/14/23 1:23 PM, Pen Hall wrote:

 I think i figured out my issue...
 The issue was that 'board' is a pointer, and all of the ways I tried to 
 de-reference it, failed. I just tried the form 
 `(*board)[number][symbol][letter]` which worked to de-reference it. 
 `&(*board)[number][symbol][letter]`, of course, works to get a pointer 
 to that spot in memory.
A nice abstraction available in D is a nested function. e.g.: ```d ref boardRef => *board; // or with traditional syntax: ref Tile[8][8][8] boardRef() { return *board; } ``` This should allow you to use `boardRef[number][symbol][i]` -Steve
Jul 14 2023