初始化struc时的奇怪行为 [英] a strange behavior in initializing a struc
问题描述
在Visual C ++ 2012中,我在初始化struc时遇到了一个奇怪的行为。基本上,我有这个文件
can.h
#ifndef _CANDEMO_H_
#define _CANDEMO_H_
//定义
#define MAX_CHANNELS 63 //不能更多因为waitforsingle对象可以"仅"处理64个事件
//包括
#include< canlib.h>
//用于调试/错误目的
#if 1
#define PRINTF_ERR(x)printf x
#else
#define PRINTF_ERR(x)
#endif
// typedefs
typedef struct {
int channel;
char name [100];
DWORD hwType;
canHandle hnd;
int hwIndex;
int hwChannel;
int isOnBus;
int driverMode;
int txAck;
} ChannelDataStruct;
typedef struct {
unsigned int channelCount;
ChannelDataStruct频道[MAX_CHANNELS];
} driverData;
#endif
和CAN.cpp文件
#include" stdio.h中"
#include" CAN.h"
//模块全局变量
unsigned int m_usedBaudRate = 0;
canHandle m_usedChannel;
unsigned int m_usedId;
unsigned int m_usedFlags = 0;
unsigned int m_Verbose = 1;
driverData m_channelData;
driverData * m_DriverConfig =& m_channelData;
int main(int argc,char ** argv)
{
}
当我在主{}之前运行Can.cpp时,结果为
m_channelData.channelCount = 0
如果我在windows窗体应用程序创建的项目中运行此代码,如下所示
#include" stdafx.h"
#include" Form1.h"
#include" CAN.h"
unsigned int m_usedBaudRate = 0;
canHandle m_usedChannel;
unsigned int m_usedId;
unsigned int m_usedFlags = 0;
unsigned int m_Verbose = 1;
driverData m_channelData;
driverData * m_DriverConfig =& m_channelData;
使用命名空间CANproject;
[STAThreadAttribute]
int main(array< System :: String ^> ^ args)
{
control canGUI;
canGUI.InitDriver();
//在创建任何控件之前启用Windows XP视觉效果
Application :: EnableVisualStyles();
Application :: SetCompatibleTextRenderingDefault(false);
//创建主窗口并运行它
Application :: Run(gcnew Form1());
返回0;
}
然后,在int main {}之前,m_channelData.channelCount被初始化为一个非常高的值,看起来像一个随机数。
我不明白为什么???感谢您的帮助
我正在将此线程移至 Visual C ++ 论坛以获得更好的响应。
祝你好运,
In Visual C++ 2012, I got a strange behavior in initializing a struc. Basically, I have this file
can.h
#ifndef _CANDEMO_H_ #define _CANDEMO_H_ //defines #define MAX_CHANNELS 63 //cannot be more because waitforsingle object can "only" handle 64 events //includes #include <canlib.h> //for debug/error purposes #if 1 #define PRINTF_ERR(x) printf x #else #define PRINTF_ERR(x) #endif //typedefs typedef struct { int channel; char name[100]; DWORD hwType; canHandle hnd; int hwIndex; int hwChannel; int isOnBus; int driverMode; int txAck; } ChannelDataStruct; typedef struct { unsigned int channelCount; ChannelDataStruct channel[MAX_CHANNELS]; } driverData; #endif
and the CAN.cpp file
#include "stdio.h" #include "CAN.h" // module globals unsigned int m_usedBaudRate = 0; canHandle m_usedChannel; unsigned int m_usedId; unsigned int m_usedFlags = 0; unsigned int m_Verbose = 1; driverData m_channelData; driverData *m_DriverConfig = &m_channelData; int main(int argc, char **argv) { }
When I ran Can.cpp, before the main {}, the result is
m_channelData.channelCount=0
if I run this code inside a project created by windows form application, as shown below
#include "stdafx.h" #include "Form1.h" #include "CAN.h"
unsigned int m_usedBaudRate = 0; canHandle m_usedChannel; unsigned int m_usedId; unsigned int m_usedFlags = 0; unsigned int m_Verbose = 1; driverData m_channelData; driverData *m_DriverConfig = &m_channelData;
using namespace CANproject; [STAThreadAttribute] int main(array<System::String ^> ^args) { control canGUI; canGUI.InitDriver(); // Enabling Windows XP visual effects before any controls are created Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); // Create the main window and run it Application::Run(gcnew Form1()); return 0; }then, right before the int main {}, m_channelData.channelCount is initialized with a very high valued, looking like a random number.
I dont understand why ??? Thanks for helping
Hi,
I am moving this thread to Visual C++ forum for better response.
Best regards,
这篇关于初始化struc时的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!