c++|GCC 4.6.3段错误(核心已转储)
//串口相关的头文件
#include/*标准输入输出定义*/
#include/*标准函数库定义*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#ifdef __cplusplus //跨平台定义方法
extern "C" {
#endif
#define FALSE-1
#define TRUE0
#define RS_OPEN_ERR 0
#define RX_UART_NO_ERR0
#define RX_UART_TIME_OUT 1
#define RX_UART_ERR2
#define RX_NO_ERR0
#define RX_PRA_ERR 1
#define RX_NO_data 2
#define RX_NO_MATH_ADDR 3
#define ENCODERSIGVAL 1048576//2^20
#define M 5
#define N 10
char Opera[5][10]=
{
{0x01,0x40,0x00,0x06,0x20,0x00,0x00,0x00,0x00,0x00},
{0x01,0x40,0x00,0x06,0x20,0x00,0x20,0x02,0x00,0x00},
{0x01,0x40,0x00,0x06,0x20,0x01,0x00,0x01,0x00,0x00},
{0x01,0x40,0x00,0x03,0xE6,0x00,0x00,0x03,0x00,0x00},
{0x01,0x40,0x00,0x03,0xE5,0x0A,0x00,0x01,0x00,0x00},
};
//绝对值编码器的圈量,连续读取3Byte(绝对值编码器1Byte,1圈内脉冲数2Byte)
void MEMODBUSCRC(char *DATA,int NUMCNT,char *CRCLOW,char *CRCHIGH) //计算CRC值
{
short int CRC;
short int i=0;
short int num=0;
CRC=0xFFFF;
for(num=0;
num
//CRC^=(DATA[num]&0x00FF);
CRC^=(*(reinterpret_cast
for(i=0;
i<8;
i++)
{
if(CRC&0x01) //如果移出位为1,则将CRC与A001相异或,A001为RTU模式校验多项式
{
CRC>>=1;
CRC&=0x7FFF;
CRC^=0xA001;
}
else
{
CRC>>=1;
CRC&=0x7FFF;
}
}
}
*CRCHIGH=CRC>>8&0xFF;
*CRCLOW=CRC&0xFF;
printf("%c\n",*CRCHIGH);
printf("%c\n",*CRCLOW);
}
intmain(int argc,char **argv)
{
unsigned int OperaOrder;
int i;
charCRCHIGH;
char CRCLOW;
char FrameCode[10];
MEMODBUSCRC(&Opera[OperaOrder][0],8,&CRCLOW,&CRCHIGH);
//计算CRC校验值
//printf("FrameCode[i]=%c",FrameCode[i]);
}
#ifdef __cplusplus
}
#endif
在ubuntu 12.0.4上用gcc4.6.3通过g++ a.c -o a -Wall -fexecptions -O2报错,估计是ubuntu加了越界检查,但是在gcc4.8以上的版本却没有问题,通过gdb调试发现原因是因为unsigned int OperaOrder未初始化,将其初始化为0则通过。
修改后的代码如下:
【c++|GCC 4.6.3段错误(核心已转储)】//串口相关的头文件
#include/*标准输入输出定义*/
#include/*标准函数库定义*/
#include pes.h>
#include
#include
#include
#include
//#include
#include
#include
#include
//#include lect.h>
#include
#ifdef __cplusplus //跨平台定义方法
extern "C" {
#endif
#define FALSE-1
#define TRUE0
#define RS_OPEN_ERR 0
#define RX_UART_NO_ERR0
#define RX_UART_TIME_OUT 1
#define RX_UART_ERR2
#define RX_NO_ERR0
#define RX_PRA_ERR 1
#define RX_NO_data 2
#define RX_NO_MATH_ADDR 3
#define ENCODERSIGVAL 1048576//2^20
#define M 5
#define N 10
/
文章图片
ar Opera[5][10]=
//{
//{0x01,0x40,0x00,0x06,0x20,0x00,0x00,0x00,0x00,0x00},
//{0x01,0x40,0x00,0x06,0x20,0x00,0x20,0x02,0x00,0x00},
//{0x01,0x40,0x00,0x06,0x20,0x01,0x00,0x01,0x00,0x00},
//{0x01,0x40,0x00,0x03,0xE6,0x00,0x00,0x03,0x00,0x00},
//{0x01,0x40,0x00,0x03,0xE5,0x0A,0x00,0x01,0x00,0x00},
//};
//绝对值编码器的圈量,连续读取3Byte(绝对值编码器1Byte,1圈内脉冲数2Byte)
char Opera[5][10]=
{
{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a},
{0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14},
{0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e},
{0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28},
{0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32},
};
//绝对值编码器的圈量,连续读取3Byte(绝对值编码器1Byte,1圈内脉冲数2Byte)
void MEMODBUSCRC(char *DATA,int NUMCNT,char *CRCLOW,char *CRCHIGH) //计算CRC值
{
short int CRC;
short int i=0;
short int num=0;
CRC=0xFFFF;
//*DATA = https://www.it610.com/article/*DATA;
for(num=0;
num
printf("%d, %x\n", num, DATA[num]);
CRC^=(DATA[num]&0x00FF);
//CRC^=(*(reinterpret_cast
for(i=0;
i<8;
i++)
{
if(CRC&0x01) //如果移出位为1,则将CRC与A001相异或,A001为RTU模式校验多项式
{
CRC>>=1;
CRC&=0x7FFF;
CRC^=0xA001;
}
else
{
CRC>>=1;
CRC&=0x7FFF;
}
}
}
*CRCHIGH=CRC>>8&0xFF;
*CRCLOW=CRC&0xFF;
printf("%d\n",*CRCHIGH);
printf("%d\n",*CRCLOW);
}
intmain(int argc,char **argv)
{
unsigned int OperaOrder = 100;
int i = 0;
charCRCHIGH = 0;
char CRCLOW = 0;
//
char FrameCode[10] = {0};
MEMODBUSCRC(&Opera[OperaOrder][0],8,&CRCLOW,&CRCHIGH);
//计算CRC校验值
//printf("FrameCode[i]=%c",FrameCode[i]);
}
#ifdef __cplusplus
}
#endif
1,这个是OperaOrder = 100 能输出 但是是全0;
2,这个是正确的
文章图片
3,这个是未初始化的输出,数据时乱的,指向未知地址
文章图片
4,这个是这个是release版本的
文章图片
5,这个是OperaOrder = 10000 数组越界,说明gcc做了优化,促使分配的
文章图片
所以,一定要养成声明变量后初始化的习惯,有些编译器会帮忙初始化,但是不能保证所有的编译器都帮你。
推荐阅读
- opencv|opencv C++模板匹配的简单实现
- C语言学习|第十一届蓝桥杯省赛 大学B组 C/C++ 第一场
- c++基础概念笔记
- 牛逼!C++开发的穿越丛林真人游戏,游戏未上线就有百万人气
- C++Primer之|C++Primer之 函数探幽
- c/c++|有感 Visual Studio 2015 RTM 简介 - 八年后回归 Dot Net,终于迎来了 Mvc 时代,盼走了 Web 窗体时代...
- QML基础信息
- C++-类型转换
- MongoDB|MongoDB - 简介
- C++面试题目总结