我知道这个算法是如何工作的,但是不能决定何时使用哪种算法?
是否有一些指导方针,哪一个比其他或任何考虑更好?
非常感谢.
我正在寻找一种简单的方法来使用流从文本文件中读取十六进制值.我使用"C++十六进制读取流0x"搜索了Stack Overflow,大多数响应是关于将十六进制写为文本或以十六进制值读取而没有"0x"前缀.这个问题是关于读取十六进制数,在一次操作中使用"0x"前缀作为数字.
我的方法:
unsigned char byte;
std::istringstream sample("0xce");
sample >> std::hex >> byte;
Run Code Online (Sandbox Code Playgroud)
结束时byte从第一个字符开始包含'0'(0x30).
'strtol`函数处理转换,但需要读取数据,转换为C风格的字符串.
我operator>>在类中重载以读取逗号分隔值(CSV)文本文件.以下是数据文件的示例:
1,-4.93994892,0xa5,8,115.313e+3,
2,-4.93986238,0xc0,8,114.711e+3,
3,-4.93977554,0xc2,8,114.677e+3,
Run Code Online (Sandbox Code Playgroud)
我的提取方法:
class Csv_Entry
{
public:
friend std::istream& operator >>(std::istream& inp, Csv_Entry& ce);
unsigned int m_index;
double m_time;
unsigned char m_byte;
unsigned int m_data_length;
double m_bit_rate;
};
std::istream&
operator >> (std::istream& inp, Csv_Entry& ce)
{
char separator;
inp >> ce.m_index;
inp >> separator;
inp >> ce.m_time;
inp >> separator;
inp >> std::hex >> ce.m_byte;
inp >> separator;
inp …Run Code Online (Sandbox Code Playgroud) 根据这篇SO帖子:
C中枚举的大小是多少?
枚举类型有signed int类型.
我想将枚举定义从转换signed int为unsigned int.
例如,在我的平台上,a unsigned int是32位宽.我想创建一个枚举:
typedef enum hardware_register_e
{
REGISTER_STATUS_BIT = (1U << 31U)
} My_Register_Bits_t;
Run Code Online (Sandbox Code Playgroud)
我的编译器抱怨上面的定义超出范围(它用于a signed int).
我如何申报unsigned int enum价值?
当我使用"%f"说明符snprintf和类型参数时,我得到MISRA类型错误float.
根据我的研究,MISRA是正确的,因为"%f"是一种类型的double.
是否有一个浮点说明符或修饰符将使用float类型参数而不是double?
我正在研究一个嵌入式系统,不想将32位浮点数转换为64位double只是为了取悦该snprintf功能.代码打印到调试/控制台端口,这是转换发生的唯一位置.
对于那些需要代码示例的人:
// This section is for those C++ purists and it fulfills the C++ tag.
#if __cplusplus
#include <cstdio>
#else
#include <stdio.h>
#endif
#define BUFFER_SIZE (128U)
int main(void)
{
char buffer[BUFFER_SIZE];
float my_float = 1.234F;
// The compiler will promote the single precision "my_float"
// to double precision before passing to snprintf.
(void)snprintf(buffer, BUFFER_SIZE, "%10.4f", my_float);
puts(buffer);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我对SO和Web的所有研究都是关于打印浮点值,而不是关于哪些说明符需要float …
我们正在使用Parasoft Static Analysis并启用了MISRA C 2004检查程序.
该软件是一个嵌入式系统.我们喜欢描述常量如下:
[1] #define MOTOR_ON (1 << 9)
Run Code Online (Sandbox Code Playgroud)
这将显示寄存器中的第9位应为1以打开电机.
表达式是MISRA失败,所以我们改变了它:
[2] #define MOTOR_ON (1U << 9U)
Run Code Online (Sandbox Code Playgroud)
更改转换为无符号整数常量,因为最好使用无符号整数进行移位.
语句2中的表达式仍然失败,因为右侧操作符(9U)需要检查.根据MISRA,如果右手操作符大于左手操作符的底层类型的位宽,则存在问题.
该问题的基础是1U具有基础类型unsigned char或8位.
我们写的寄存器是16位,所以理论上没有问题.
如何更改[2]中的表达式以使其通过MISRA C 2004,更喜欢不使用演员表?
我正在使用IAR Embedded Workbench和8/32位模式的ARM7TDMI处理器.
编辑1:示例代码.
void turn_on_motor(void);
#define MOTOR_ON (1U << 9U)
void turn_on_motor(void)
{
uint16_t * const p_motor_control = (uint16_t *)(0x01234567U);
*p_motor_control = MOTOR_ON;
}
Run Code Online (Sandbox Code Playgroud)
错误文本:应限制用作移位运算符的右侧操作数的常量.
来自Parasoft提供的MISRA规则文档:
Rule reports a violation if:
- the right-hand operand is a constant with negative value or with value that
exceeds the length …Run Code Online (Sandbox Code Playgroud) 可能重复:
C的智能指针/安全内存管理?
我有一个嵌入式应用程序,我在动态内存中分配一个对象并将其传递给其他模块.
我想创建一个指向这个对象的智能指针.C++中有许多用于使用和实现智能指针的示例.
我正在寻找一个只有C语言的智能指针实现.
谢谢.
我有一个C语言源代码,用于嵌入式系统,包含每像素8位灰度图像的数据数组.我负责记录软件,我想将此源代码转换为JPEG(图像)文件.
这是一个代码示例:
const unsigned char grayscale_image[] = {
0, 0, 0, 0, 0, 0, 0, 74, 106, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159,
159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 146, 93, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
//...
};
const unsigned int height = 41;
const unsigned int width = 20;
Run Code Online (Sandbox Code Playgroud)
以下是我的问题:(是的,复数)
我拥有以下资源:MS Visio 2010,Gimp,Paint,Java,Eclipse,MS Visual Studio 2010 Professional,wxWidgets,wxFrameBuilder,Cygwin. …
将NULL作为参数传递时的行为是strstr什么?
鉴于:
char * p = NULL;
char * s = NULL;
Run Code Online (Sandbox Code Playgroud)
情况1: strstr(p, "Hello");
案例2: strstr("With my dog", p);
案例3: strstr(p, s);
我的理解是行为未定义,并留给所有3个案例的实施.
根据Microsoft Visual Studio文档,他们执行参数验证并在那里处理它. 请参阅备注部分.
我们在IAR Workbench上使用C99.
背景:一些测试人员正在编写单元测试并为字符串变量分配NULL.
我正在尝试使用on_errorBoost :: Spirit :: qi 的机制来找出解析失败的原因.
我在on_error函数中设置了一个断点,并且正在调用函数,但没有输出(nada,nothing,void,...).
简单on_error:
on_error<fail>(level1,
boost::phoenix::ref(std::cout) << "I've failed.\n"
);
Run Code Online (Sandbox Code Playgroud)
复杂on_error(来自不同的网站):
on_error<fail>
(
start,
boost::phoenix::ref(std::cout)
<< val("Error! Expecting ")
<< _4
<< val(" here: \"")
<< construct<std::string>(qi::_3, qi::_2)
<< val("\"")
<< std::endl
);
Run Code Online (Sandbox Code Playgroud)
这是我的类包含简单on_error:
template <typename Iterator, typename Skipper>
struct Event_Compound
: qi::grammar<Iterator, Skipper>
{
Event_Compound ()
: Event_Compound::base_type(start, "Compound-Event")
{
using qi::lexeme;
using qi::lit;
using namespace qi;
using boost::spirit::ascii::char_;
relational_operator =
lit("&&")[Matched_Relational_AND]
|| lit("||")[Matched_Relational_OR]
; …Run Code Online (Sandbox Code Playgroud) 如果我想使用 <limits> 标头,则很容易编写最小整数:
std::int32_t small = std::numeric_limits<std::int32_t>::min();
Run Code Online (Sandbox Code Playgroud)
这small等于-2147483648。
我在一家图书馆工作,里面有这样的数字。因此最好写成
std::int32_t small = -2147483648;
Run Code Online (Sandbox Code Playgroud)
我在 Visual Studio 上遇到了问题,但是当我阅读 C++ 规范时,我发现它表明我无法对任何编译器执行此操作。我认为我不能这样做的原因是规范表明整数文字不包含负号。负号实际上后来变成了一元减号。因此,C++ 处理整数文字 2147483648。这个数字太大,无法放入 int 中(假设 int 是 32 位,这是我正在考虑的平台上的),因此它必须是一个 long int (64 位) 。
(顺便说一句,这是我在 Visual Studio 上出错的地方。它似乎将 2147483648 视为无符号长整型,而不是有符号长整型,但这个问题与规范行为有关)
据我所知,无法将 32 位数字 -2147483648 写为 32 位文字。您必须首先将其设为 64 位文字 2147483648UL,然后将其取反。这似乎是一个疏忽,但它似乎也可以在任何地方编译(除了 Visual Studio),那么我错过了什么?