Welcome, Guest. Please login or register.

Author Topic: double pointer indirection optimization dillema  (Read 2106 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Trev

  • Hero Member
  • *****
  • Join Date: May 2003
  • Posts: 1550
  • Country: 00
    • Show all replies
Re: double pointer indirection optimization dillema
« on: March 10, 2005, 09:47:11 PM »
I think we need a little more information. This:

struct MajorArrayElement
{
LONG 2ndArrayOffset;
LONG 2ndArrayNrElements;
LONG *2ndArrayPointer;
}*MajorArrayElementPtr;

creates an lvalue called MajorArrayElementPtr that is a pointer to a 'struct MajorArrayElement'. It's not initialized, so the value of MajorArrayElementPtr is undefined.

Ummm, you have to reference a structure's elements using the name of the structure, so you're stuck with MajorArrayElementPtr->element. (Forget about C++ and static members for a moment.) I'm assuming that MajorArrayElementPtr contains a valid pointer.

Compiler optimizations aren't really relevent. You're just dealing with standard C. Storing an element's value in a temporary variable may prevent the compiler from optimizing references to MajorArrayElementPtr and its elements (i.e. it now has to track three pointers instead of two). I suppose it depends on the compiler.

Trev
 

Offline Trev

  • Hero Member
  • *****
  • Join Date: May 2003
  • Posts: 1550
  • Country: 00
    • Show all replies
Re: double pointer indirection optimization dillema
« Reply #1 on: March 11, 2005, 07:02:32 PM »
Yeah, it's probably a good idea to leave some of the optimization up to the compiler. Of course, you'll want to write your code in a way that can be optimized, and that requires reading the compiler documentation. Both gcc and VBCC have pretty good documentation about how their optimizations work.

Really though, your code should be written for readability and debugging, e.g. use

(EDIT: removed dumb example, replaced with something more relevant, added "however")

MajorArrayElementPtr->2ndArrayPointer;

However, if you're working with 2ndArrayPointer in a loop, then using a temporary variable is probably more efficient:

/* initialize array that's already allocated */

{
LONG i;
LONG * p = MajorArrayElementPtr->2ndArrayPointer;

for (i = MajorArrayElementPtr->2ndArrayNrElements; i > 0; i--)
*(++p) = 0;
}

Trev