可以直接访问一个变量(而不是一个指针和dereferecer)给予不同的价值观? [英] Could accessing a variable directly (rather than with a pointer and dereferecer) give different values?
问题描述
我知道这是一个奇怪的问题,我冒险显示我的绿色,但我撕我的头发在这里。
我有一个C ++的例子,按预期工作。这里的麻烦位的一个片段:
BIRDFRAME框架;
birdGetMostRecentFrame(GROUP_ID,和放大器;帧);
BIRDREADING * bird_data;
bird_time = frame.dwTime;
为(FBBloop = FBBstart; FBBloop< FBBend; FBBloop ++)
{
bird_data =安培; frame.reading [FBBloop]
//做的东西
}
注意 bird_data
是一个指针,被分配 frame.reading [FBBloop]
的地址。我的应用程序是用C#编写,因此没有任何this指针说大话的。相应的位看起来是这样的:
FlockOfBirds.BIRDFRAME帧=新FlockOfBirds.BIRDFRAME();
FlockOfBirds.birdGetMostRecentFrame(1,参考帧);
Console.WriteLine(T:{0},frame.dwTime.ToString());
FlockOfBirds.BIRDREADING阅读;
为(INT K = 1; K< = sysconf.byNumDevices; k ++)
{
读数= frame.readings [K];
Console.WriteLine([{0}] [{1}] [{2}],reading.position.nX.ToString(),reading.position.nY.ToString(),reading.position。 nZ.ToString());
}
的问题是,在C#示例中,仅阅读[1]
的有意义的数据。在C ++例子中,两个 bird_data [1]
和 bird_data [2]
具备不错的数据。奇怪的是,阅读[2-N]
并不都具有零值,但似乎是随机的和不变的。下面是从C#应用程序的输出是什么样子:
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]
$ b $(b T) :17291363
[30072] [ - 2388] [ - 6600]
[-19660] [1048] [ - 31310]
注意,每个条目的顶部三重是从这些条目的预处理和随后的稍微不同。在C ++应用程序,底线的行为类似,但在这里它始终保持不变。
难道这是与缺乏指向和提领的呢?还是我吠叫完全错了?我做了别的东西这显然是愚蠢的? ?最重要的是:做我怎么做它的工作。
更新:结构体和功放;实习医生
C ++
//鸟阅读结构
typedef结构tagBIRDREADING
{
BIRDPOSITION位置;接收器
的位置//角度BIRDANGLES; //接收器的方向,角度为
BIRDMATRIX矩阵; //接收器的方向,作为基质
BIRDQUATERNION四元数; //接收器的方向,如四元数
WORD wButtons; //按钮状态
}
BIRDREADING;
//鸟框架结构
//
//注意:在单机模式下,读取鸟存储在阅读[0],和
/ /所有其他数组元素未使用。在主/从模式下,读书
//阵列由鸟号索引 - 例如,鸟#1在读[1],
//鸟#2是读[2 ]等,和读出[0]是不使用的。
typedef结构tagBIRDFRAME
{
DWORD dwTime; //在该读数拍摄,在毫秒
BIRDREADING阅读时间[BIRD_MAX_DEVICE_NUM + 1]; //从每只鸟
}
BIRDFRAME阅读;
BOOL DLLEXPORT birdGetMostRecentFrame(INT nGroupID,BIRDFRAME * PFRAME);
C#:
[StructLayout(LayoutKind.Sequential,包= 0)
公共结构BIRDREADING
{
公共BIRDPOSITION位置;
公共BIRDANGLES角度;
公共BIRDMATRIX矩阵;
公共BIRDQUATERNION四元数;
公共USHORT wButtons;
}
[StructLayout(LayoutKind.Sequential,包= 0)
公共结构BIRDFRAME
{
公共UINT dwTime;
[的MarshalAs(UnmanagedType.ByValArray,SizeConst = 127)]
公共BIRDREADING []读数;
}
函数[DllImport(@Bird.dll,CallingConvention = CallingConvention.Cdecl)]
公共静态外部布尔birdGetMostRecentFrame(INT nGroupID,楼盘BIRDFRAME帧);
**更新2:更多的结构:**
C ++
的#pragma包(1)//包装下面就一字节边界
//鸟持仓结构
typedef结构tagBIRDPOSITION
{
短nX的结构; // x坐标
短NY; // y坐标
短NZ; // z坐标
}
BIRDPOSITION;
//鸟角结构
typedef结构tagBIRDANGLES
{
短nAzimuth; //方位角
短nElevation; //仰角
短nRoll; //侧倾角
}
BIRDANGLES;
//伯德矩阵结构
typedef结构tagBIRDMATRIX
{
短N [3] [3]; //矩阵元素
}
BIRDMATRIX阵列;
//鸟四元结构
typedef结构tagBIRDQUATERNION
{
短nQ0; // Q0
短NQ1; // Q1
短NQ2; // Q2
短nQ3; // Q3
}
BIRDQUATERNION;
的#pragma pack()的//恢复的结构
C#正常包装
[StructLayout(LayoutKind.Sequential,包= 1)]
公共结构BIRDPOSITION
{
公共短nX的;
公共短NY;
公共短NZ;
}
[StructLayout(LayoutKind.Sequential,包= 1)]
公共结构BIRDANGLES
{
公共短nAzimuth; //方位角
公共短nElevation; //仰角
公共短nRoll; //侧倾角
}
[StructLayout(LayoutKind.Sequential,包= 1)]
公共不安全结构BIRDMATRIX
{
公共固定短ñ [9];
}
[StructLayout(LayoutKind.Sequential,包= 1)]
公共结构BIRDQUATERNION
{
公共短nQ0; // Q0
公共短NQ1; // Q1
公共短NQ2; // Q2
公共短nQ3; // Q3
}
我想你。结构中的两个项目(C ++ / C#),它似乎正常工作
我在C ++中使用这样的:
的#includestdafx.h中
#包括WINDOWS.H
的#pragma包(1)//包上以下结构一个字节边界
//鸟持仓结构
typedef结构tagBIRDPOSITION
{
短nX的; // x坐标
短NY; // y坐标
短NZ; // z坐标
}
BIRDPOSITION;
//鸟角结构
typedef结构tagBIRDANGLES
{
短nAzimuth; //方位角
短nElevation; //仰角
短nRoll; //侧倾角
}
BIRDANGLES;
//伯德矩阵结构
typedef结构tagBIRDMATRIX
{
短N [3] [3]; //矩阵元素
}
BIRDMATRIX阵列;
//鸟四元结构
typedef结构tagBIRDQUATERNION
{
短nQ0; // Q0
短NQ1; // Q1
短NQ2; // Q2
短nQ3; // Q3
}
BIRDQUATERNION;
的#pragma pack()的//恢复的结构
typedef结构tagBIRDREADING
{
BIRDPOSITION位置正常的包装;接收器
的位置//角度BIRDANGLES; //接收器的方向,角度为
BIRDMATRIX矩阵; //接收器的方向,作为基质
BIRDQUATERNION四元数; //接收器的方向,如四元数
WORD wButtons; //按钮状态
}
BIRDREADING;
的#define BIRD_MAX_DEVICE_NUM 126
//鸟框架结构
//
//注意:在单机模式下,读取鸟存储在阅读[0],和
//所有其它数组元素是未使用的。在主/从模式下,读书
//阵列由鸟号索引 - 例如,鸟#1在读[1],
//鸟#2是读[2 ]等,和读出[0]是不使用的。
typedef结构tagBIRDFRAME
{
DWORD dwTime; //在该读数拍摄,在毫秒
BIRDREADING阅读时间[BIRD_MAX_DEVICE_NUM + 1]; //从每只鸟
}
BIRDFRAME阅读;
的externC__declspec(dllexport)的BOOL birdGetMostRecentFrame(INT nGroupID,BIRDFRAME * PFRAME){
pframe-> dwTime =的GetTickCount();
的for(int i = 0; I<的sizeof(pframe->读)/ sizeof的(* pframe->读);我++){
pframe->阅读[I] = BIRDREADING (); [I] .position.nX =(短)我读书;
pframe->
pframe->阅读[I] .position.nY =(短)I + 1;
pframe->阅读[I] .position.nZ =(短)I + 2;
}
返回TRUE;
}
这在C#中:
[StructLayout(LayoutKind.Sequential,包= 1)]
公共结构BIRDPOSITION
{
公共短nX的;
公共短NY;
公共短NZ;
}
[StructLayout(LayoutKind.Sequential,包= 1)]
公共结构BIRDANGLES
{
公共短nAzimuth; //方位角
公共短nElevation; //仰角
公共短nRoll; //侧倾角
}
[StructLayout(LayoutKind.Sequential,包= 1)]
公共不安全结构BIRDMATRIX
{
公共固定短ñ [9];
}
[StructLayout(LayoutKind.Sequential,包= 1)]
公共结构BIRDQUATERNION
{
公共短nQ0; // Q0
公共短NQ1; // Q1
公共短NQ2; // Q2
公共短nQ3; // Q3
}
[StructLayout(LayoutKind.Sequential,包= 0)
公共结构BIRDREADING
{
公共BIRDPOSITION位置;
公共BIRDANGLES角度;
公共BIRDMATRIX矩阵;
公共BIRDQUATERNION四元数;
公共USHORT wButtons;
}
[StructLayout(LayoutKind.Sequential,包= 0)
公共结构BIRDFRAME
{
公共UINT dwTime;
[的MarshalAs(UnmanagedType.ByValArray,SizeConst = 127)]
公共BIRDREADING []读数;
}
公共部分Form1类:表格
{
函数[DllImport(@Bird.dll,CallingConvention = CallingConvention.Cdecl)]
公静态外部布尔birdGetMostRecentFrame(INT nGroupID,楼盘BIRDFRAME帧);
公共Form1中()
{
的InitializeComponent();
}
私人无效的button1_Click(对象发件人,EventArgs五)
{
BIRDFRAME BF =新BIRDFRAME();
如果(checkBox1.Checked)
{
bf.readings =新BIRDREADING [127];
}
如果(birdGetMostRecentFrame(0,参考BF)){
listBox1.Items.Add(bf.dwTime.ToString());
Console.WriteLine(bf.dwTime.ToString());
为(INT K = 1; K< = 3; k ++)
{
VAR读数= bf.readings [K];
Console.WriteLine(的String.Format([{0}] [{1}] [{2}],reading.position.nX.ToString(),reading.position.nY.ToString() reading.position.nZ.ToString()));
listBox1.Items.Add(的String.Format([{0}] [{1}] [{2}],reading.position.nX.ToString(),reading.position.nY.ToString( ),reading.position.nZ.ToString()));
}
}
}
}
在这两种情况下(在C#读数初始化为新的,或者不是还给有意义的数据:
40067905
[1] [2] [3]
[2] [3] [4]
[3] [4] [5]
40068653
[1] [2] [3]
[2] [3] [4]
[3] [4] [5]
40069418
[1] [2] [3]
[2 ] [3] [4]
[3] [4] [5]
40072585
[1] [2] [3]
[2] [3] [4 ]
[3] [4] [5]
做到了对Windows7的64位。
我附加了与两个项目VS2010一个ZIP
下面是链接:
http://hotfile.com/dl/115296617/9644496/testCS.zip。 HTML
看来你要设置的位置,结构内部错误的价值观:/
I know it's an odd question and I risk showing my greenness, but I'm tearing my hair out here.
I have a C++ example that works as expected. Here's a snippet of the bothersome bit:
BIRDFRAME frame;
birdGetMostRecentFrame(GROUP_ID,&frame);
BIRDREADING *bird_data;
bird_time=frame.dwTime;
for(FBBloop=FBBstart; FBBloop<FBBend; FBBloop++ )
{
bird_data = &frame.reading[FBBloop];
// Do stuff
}
Note that bird_data
is a pointer and is assigned the address of frame.reading[FBBloop]
. My app is written in C# and so doesn't have any of this pointer malarkey. The corresponding bit looks like this:
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 <= sysconf.byNumDevices; k++)
{
reading = frame.readings[k];
Console.WriteLine(" [{0}][{1}][{2}]", reading.position.nX.ToString(), reading.position.nY.ToString(), reading.position.nZ.ToString());
}
The problem is that in the C# example, only reading[1]
has meaningful data. In the C++ example both bird_data[1]
and bird_data[2]
have good data. Oddly, reading[2-n]
don't all have zeroed values, but appear to be random and constant. Here's what the output from the C# app looks like:
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]
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.
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?
Update: Structs & externs
C++
// 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);
C#:
[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);
** Update 2: More structs: **
C++
#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
C#
[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
}
I tried your struct in two projects (C++/C#) and it seems to work correctly.
I use this in C++:
#include "stdafx.h"
#include "windows.h"
#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
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;
#define BIRD_MAX_DEVICE_NUM 126
// 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;
extern "C" __declspec( dllexport ) BOOL birdGetMostRecentFrame(int nGroupID, BIRDFRAME *pframe) {
pframe->dwTime = GetTickCount();
for (int i = 0; i < sizeof(pframe->reading)/sizeof(*pframe->reading); i++) {
pframe->reading[i] = BIRDREADING();
pframe->reading[i].position.nX = (short)i;
pframe->reading[i].position.nY = (short)i + 1;
pframe->reading[i].position.nZ = (short)i + 2;
}
return TRUE;
}
And this in C#:
[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
}
[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;
}
public partial class Form1 : Form
{
[DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool birdGetMostRecentFrame(int nGroupID, ref BIRDFRAME frame);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
BIRDFRAME bf = new BIRDFRAME();
if (checkBox1.Checked)
{
bf.readings = new BIRDREADING[127];
}
if (birdGetMostRecentFrame(0, ref bf)) {
listBox1.Items.Add(bf.dwTime.ToString());
Console.WriteLine(bf.dwTime.ToString());
for (int k = 1; k <= 3; k++)
{
var reading = bf.readings[k];
Console.WriteLine(String.Format(" [{0}][{1}][{2}]", reading.position.nX.ToString(), reading.position.nY.ToString(), reading.position.nZ.ToString()));
listBox1.Items.Add(String.Format(" [{0}][{1}][{2}]", reading.position.nX.ToString(), reading.position.nY.ToString(), reading.position.nZ.ToString()));
}
}
}
}
In both cases (reading initialized as new in C#, or not it gives back meaningful data:
40067905
[1][2][3]
[2][3][4]
[3][4][5]
40068653
[1][2][3]
[2][3][4]
[3][4][5]
40069418
[1][2][3]
[2][3][4]
[3][4][5]
40072585
[1][2][3]
[2][3][4]
[3][4][5]
Did it on Windows7 64 bit. I'm attaching a ZIP with both projects for VS2010.
Here is the link:
http://hotfile.com/dl/115296617/9644496/testCS.zip.html
It seems that you are setting wrong values inside that position struct :/
这篇关于可以直接访问一个变量(而不是一个指针和dereferecer)给予不同的价值观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!