Page 1 of 1
Posted: 31 Aug 2010, 11:33
Huki
The AI part of the re-volt source code seems to be the most complex. But there is one part of AINode code in the source that really confuses me. Can someone with C knowledge clarify this?

Suppose there is a structure like this. As you can see, the 4 members of the structure are pointers to the same type as the struct itself.

Code: Select all

struct STRUCT {

	STRUCT *PrevStruct[2];
	STRUCT *NextStruct[2];
	
};
Suppose you create a variable like such:

Code: Select all

STRUCT *TestStruct;
TestStruct = (STRUCT *) malloc(sizeof(TestStruct) * int NumStructs);

Now if you want to access one of the Nextstruct members, you use

Code: Select all

TestStruct->NextStruct[0];
Now, if you want to access the PrevStruct member of the Nextstruct[0] struct, what would you use? This is how it is done in the re-volt sources.

Code: Select all

TestStruct->NextStruct[0]->PrevStruct[0];
This will compile without errors, but unfortunately the program will crash because of Access Violation. So here are my 2 questions:

- Can anyone think of a reason why this is done in the source?

- What is the alternative to this code which would work (and not cause Access Violation error + crash) ?

For additional information, such code is used in rv2.zip\Xbox\Src\ainode.cpp - SetupNewMethodLinkNodes() lines 532 to 545.

Posted: 31 Aug 2010, 18:42
jigebren
I don't know C or C++, so I could easily be mistaken, but from what I know about pointers:
Are you sure that TestStruct->NextStruct[0] actually point to something valid?

I mean, if you only created one "TestStruct" variable, you can access its "NextStruct" member. Ok. But if you have not filled this value with something consistent, the returned value (from TestStruct->NextStruct[0]) will be null. Then you can't access to the "PrevStruct[0]" member of a null value.

In that case, it could compile without error, because it is allowed to used it that way, but it will crash when running because you will try to access to memory at offset 0.