Note that there are some explanatory texts on larger screens.

plurals
  1. POCould accessing a variable directly (rather than with a pointer and dereferecer) give different values?
    primarykey
    data
    text
    <p>I know it's an odd question and I risk showing my greenness, but I'm tearing my hair out here.</p> <p>I have a C++ example that works as expected. Here's a snippet of the bothersome bit:</p> <pre><code>BIRDFRAME frame; birdGetMostRecentFrame(GROUP_ID,&amp;frame); BIRDREADING *bird_data; bird_time=frame.dwTime; for(FBBloop=FBBstart; FBBloop&lt;FBBend; FBBloop++ ) { bird_data = &amp;frame.reading[FBBloop]; // Do stuff } </code></pre> <p>Note that <code>bird_data</code> is a pointer and is assigned the address of <code>frame.reading[FBBloop]</code>. My app is written in C# and so doesn't have any of this pointer malarkey. The corresponding bit looks like this:</p> <pre><code>FlockOfBirds.BIRDFRAME frame = new FlockOfBirds.BIRDFRAME(); FlockOfBirds.birdGetMostRecentFrame(1, ref frame); Console.WriteLine("T:{0}",frame.dwTime.ToString()); FlockOfBirds.BIRDREADING reading; for (int k = 1; k &lt;= sysconf.byNumDevices; k++) { reading = frame.readings[k]; Console.WriteLine(" [{0}][{1}][{2}]", reading.position.nX.ToString(), reading.position.nY.ToString(), reading.position.nZ.ToString()); } </code></pre> <p>The problem is that in the C# example, only <code>reading[1]</code> has meaningful data. In the C++ example both <code>bird_data[1]</code> and <code>bird_data[2]</code> have good data. Oddly, <code>reading[2-n]</code> don't all have zeroed values, but appear to be random and constant. Here's what the output from the C# app looks like:</p> <pre><code>T:17291325 [30708][-2584][-5220] [-19660][1048][-31310] T:17291334 [30464][-2588][-5600] [-19660][1048][-31310] T:17291346 [30228][-2600][-5952] [-19660][1048][-31310] T:17291354 [30120][-2520][-6264] [-19660][1048][-31310] T:17291363 [30072][-2388][-6600] [-19660][1048][-31310] </code></pre> <p>Notice that the top triplet of each entry is slightly different from those of the entries pre- and succeeding it. In the C++ app, the bottom line behaves similarly, but here it stays the same throughout.</p> <p>Could this be to do with the lack of pointing and dereferencing? Or am I barking up entirely the wrong tree? Have I done something else that's obviously stupid? Most importantly: how do I make it work?</p> <hr> <p><strong>Update: Structs &amp; externs</strong></p> <p>C++</p> <pre><code>// Bird reading structure typedef struct tagBIRDREADING { BIRDPOSITION position; // position of receiver BIRDANGLES angles; // orientation of receiver, as angles BIRDMATRIX matrix; // orientation of receiver, as matrix BIRDQUATERNION quaternion; // orientation of receiver, as quaternion WORD wButtons; // button states } BIRDREADING; // Bird frame structure // // NOTE: In stand-alone mode, the bird reading is stored in reading[0], and // all other array elements are unused. In master/slave mode, the "reading" // array is indexed by bird number - for example, bird #1 is at reading[1], // bird #2 is at reading[2], etc., and reading[0] is unused. typedef struct tagBIRDFRAME { DWORD dwTime; // time at which readings were taken, in msecs BIRDREADING reading[BIRD_MAX_DEVICE_NUM + 1]; // reading from each bird } BIRDFRAME; BOOL DLLEXPORT birdGetMostRecentFrame(int nGroupID, BIRDFRAME *pframe); </code></pre> <p>C#:</p> <pre><code>[StructLayout(LayoutKind.Sequential, Pack = 0)] public struct BIRDREADING { public BIRDPOSITION position; public BIRDANGLES angles; public BIRDMATRIX matrix; public BIRDQUATERNION quaternion; public ushort wButtons; } [StructLayout(LayoutKind.Sequential, Pack = 0)] public struct BIRDFRAME { public uint dwTime; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 127)] public BIRDREADING[] readings; } [DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)] public static extern bool birdGetMostRecentFrame(int nGroupID, ref BIRDFRAME frame); </code></pre> <hr> <p>** Update 2: More structs: **</p> <p>C++</p> <pre><code>#pragma pack(1) // pack the following structures on one-byte boundaries // Bird position structure typedef struct tagBIRDPOSITION { short nX; // x-coordinate short nY; // y-coordinate short nZ; // z-coordinate } BIRDPOSITION; // Bird angles structure typedef struct tagBIRDANGLES { short nAzimuth; // azimuth angle short nElevation; // elevation angle short nRoll; // roll angle } BIRDANGLES; // Bird matrix structure typedef struct tagBIRDMATRIX { short n[3][3]; // array of matrix elements } BIRDMATRIX; // Bird quaternion structure typedef struct tagBIRDQUATERNION { short nQ0; // q0 short nQ1; // q1 short nQ2; // q2 short nQ3; // q3 } BIRDQUATERNION; #pragma pack() // resume normal packing of structures </code></pre> <p>C#</p> <pre><code>[StructLayout(LayoutKind.Sequential, Pack = 1)] public struct BIRDPOSITION { public short nX; public short nY; public short nZ; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct BIRDANGLES { public short nAzimuth; // azimuth angle public short nElevation; // elevation angle public short nRoll; // roll angle } [StructLayout(LayoutKind.Sequential, Pack = 1)] public unsafe struct BIRDMATRIX { public fixed short n[9]; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct BIRDQUATERNION { public short nQ0; // q0 public short nQ1; // q1 public short nQ2; // q2 public short nQ3; // q3 } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload