小编Iva*_*van的帖子

具有某些公共成员的强制转换结构

假设我有 2struct秒:

typedef struct
{
    uint8_t useThis;            
    uint8_t u8Byte2;             
    uint8_t u8Byte3; 
    uint8_t u8Byte4;  
} tstr1
Run Code Online (Sandbox Code Playgroud)

typedef struct
{
    uint8_t u8Byte1;            
    uint8_t u8Byte2;             
    uint8_t useThis;  
} tstr2
Run Code Online (Sandbox Code Playgroud)

需要useThis函数内的成员,但在某些情况下,我需要强制转换一个或另一个结构:

void someFunction()
{
    someStuff();
    SOMETHING MyInstance;
    if(someVariable)
    {
        MyInstance = reinterpret_cast<tstr1*>(INFO_FROM_HARDWARE); //This line of course doesn't work
    }
    else
    {
        MyInstance = reinterpret_cast<tstr2*>(INFO_FROM_HARDWARE); //This line of course doesn't work
    }
    MyInstance->useThis; //Calling this memeber with no problem

    moreStuff();
}
Run Code Online (Sandbox Code Playgroud)
  • 所以我想使用useThis无论做了什么演员。如何才能做到这一点?

  • 我想避免someFunction()成为模板(只是为了避免这种事情 …

c++ struct casting reinterpret-cast c++11

4
推荐指数
1
解决办法
152
查看次数

减去或添加常数到大型数组

我有一个大型uint8_t数组(大小= 1824 * 942)。我想对每个元素执行相同的操作。特别是我需要从每个元素中减去-15。

该阵列每秒刷新20次,因此时间是个问题,我避免在阵列上循环。

是否有捷径可寻?

c++ arrays math pointer-arithmetic integer-arithmetic

3
推荐指数
1
解决办法
387
查看次数

在文件段中写入零

我用来fwrite写一个文件(它是一个图像)。

首先,我正在编写一个具有固定大小的标题

int num_meta_write_bytes = fwrite(headerBuffer, 1, headerSize, file);
Run Code Online (Sandbox Code Playgroud)

这工作正常

然后我必须写像素:

num_write_bytes = fwrite(data_pointer, 1, image_size, file);
Run Code Online (Sandbox Code Playgroud)

这工作正常

对于我的情况,我必须在标题和像素之间写入可变数量的零,我发现的(丑陋的?)解决方案是:

for (size_t i = 0; i < padding_zeros; i++) //padding_zeros is calculated in runtime
{
    uint8_t zero = 0;
    num_meta_write_bytes += fwrite(&zero, 1, sizeof(uint8_t), fp);
}
Run Code Online (Sandbox Code Playgroud)

(此代码放置在写入像素之前)

我的问题是:有没有办法直接告诉fwrite连续写入相同的值padding_zeros次数?而不是使用这个for循环?

c fwrite zero-padding

2
推荐指数
1
解决办法
345
查看次数

强制 reinterpret_cast 返回 nullptr

我正在为一个接收内存地址的函数创建单元测试 unsigned long

在函数内部,这个地址被reinterpret_cast编入了一个你的类的指针。

void my function(Address inputAdress ) // Address comes from: typedef unsigned long Address;
{
  ClsMyClassThread* ptr = reinterpret_cast<ClsMyClassThread*>(inputAdress);

  if(ptr == nullptr)
  {
    // Do something 
  }
}
Run Code Online (Sandbox Code Playgroud)

该功能运作良好,但它创建单元测试时,我想不通我怎么能强迫ptrnullptr(我想测试全覆盖)。这什么时候可以发生?这甚至可能吗?

谢谢!

c++ unit-testing casting reinterpret-cast

1
推荐指数
2
解决办法
116
查看次数

指向结构成员?

我有以下typedef独立非执行董事struct

typedef struct
{
    uint8_t u8Byte1;             // This byte takes the needed values sometimes
    uint8_t u8Byte2;             // Not used
    uint8_t u8Byte3;             // This byte takes the needed values the other times
    uint8_t u8Byte4;             // Not used
} tstrMapMetadata;
Run Code Online (Sandbox Code Playgroud)

我有一个线程来填充(使用来自传感器的数据)这个结构并使用它的值之一:

while(true)
{
  tstrMapMetadata * pstrMapMetadata = something();
  
  if(pstrMapMetadata->u8Byte1 == SOMETHING) //<---- this line
  {
    //Do something special 
  }
}
Run Code Online (Sandbox Code Playgroud)

但是现在我有一个condition(线程中的常量),我想在其中比较标记的行u8Byte3而不是u8Byte1.

我当然可以

if(((condition) ? pstrMapMetadata->u8Byte1 : pstrMapMetadata->u8Byte3) == SOMETHING) //<---- this line …
Run Code Online (Sandbox Code Playgroud)

c++ struct pointers typedef

1
推荐指数
1
解决办法
69
查看次数