小编Gui*_*e D的帖子

抽象函数和虚函数有什么区别?

抽象函数和虚函数有什么区别?在哪些情况下建议使用虚拟或抽象?哪一个是最好的方法?

oop programming-languages virtual-functions abstract

1526
推荐指数
11
解决办法
58万
查看次数

为什么不建议在空间/辐射环境中使用C ++模板?

通过阅读此问题,例如,我理解了为什么在空间或核电站等辐射较高的环境中不建议动态分配或例外的原因。关于模板,我不知道为什么。你能给我解释一下吗?

考虑到这个答案,它说使用起来很安全。

注意:我不是在谈论复杂的标准库内容,而是针对性的自定义模板。

c++ embedded templates fault-tolerance

64
推荐指数
2
解决办法
7950
查看次数

如何适配ios13状态栏?

当我在 ios13 xcode11 beta 上运行我的项目时。

[UIApplication sharedApplication].statusBarFrame.size.height

代码返回 0。

我应该怎么做才能使其适应 ios13?

objective-c ios ios13

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

数组和结构体可以以不同的方式初始化吗?

我的问题可能看起来很奇怪,事实上,这是上下文:

我目前面临一个奇怪的问题,同时切换 -在我正在从事的项目上- 从pullinino到CV32的核心(也发生了一些其他变化,例如关于crt0,如一些数据RAM重置)。

这是一个(真实的)示例,说明了一个相当简单的 main 所发生的情况(我无法对startup/crt0 文件进行编辑:我在帖子后面部分给出了它)。

#include <string.h>
#include <inttypes.h>
#include <stdio.h>

typedef struct
{
    uintptr_t addr;
    uint32_t foo;  
} some_struct_t;

static uint32_t text_in_data[8] = {0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555, 0x66666666, 0x77777777, 0x88888888};
uint32_t text_in_data2[8] = {0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555, 0x66666666, 0x77777777, 0x88888888};

some_struct_t text_in = {(uintptr_t)text_in_data, 8};
static some_struct_t text_in2 = {(uintptr_t)text_in_data, 8};

int main(void)
{
    some_struct_t text_in3 = {(uintptr_t)text_in_data, 8};
    static some_struct_t text_in4 = {(uintptr_t)text_in_data, 8};
    static some_struct_t text_in5 = {(uintptr_t)text_in_data2, …
Run Code Online (Sandbox Code Playgroud)

c embedded storage linker-scripts riscv

5
推荐指数
0
解决办法
323
查看次数

如何删除或替换两个模式之间的多行文本

我想在我的一些脚本中添加一些客户标志,以便在 shell 脚本打包之前对其进行解析。

比方说,删除之间的所有多行文本

^([#]|[//]){0,1}[_]+NOT_FOR_CUSTOMER_BEGIN[_]+\n

和之间

^([#]|[//]){0,1}[_]+NOT_FOR_CUSTOMER_END[_]+\n

我希望它具有容错性(关于“_”的数量),这就是我使用正则表达式的原因。

例如:

之前.foo

i want this
#____NOT_FOR_CUSTOMER_BEGIN________
not this
nor this
#________NOT_FOR_CUSTOMER_END____
and this
//____NOT_FOR_CUSTOMER_BEGIN__
not this again
nor this again
//__________NOT_FOR_CUSTOMER_END____
and this again
Run Code Online (Sandbox Code Playgroud)

会变成:

后.foo

i want this
and this
and this again
Run Code Online (Sandbox Code Playgroud)

我宁愿使用 sed,但欢迎任何聪明的解决方案:)

像这样的东西:

cat before.foo |  tr '\n' '\a' | sed -r 's/([#]|[//]){0,1}[_]+NOT_FOR_CUSTOMER_BEGIN[_]+\a.*\a([#]|[//]){0,1}[_]+NOT_FOR_CUSTOMER_END[_]+\a/\a/g' | tr '\a' '\n' > after.foo
Run Code Online (Sandbox Code Playgroud)

python shell awk sed substitution

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

两次释放 glib 缓冲区是否安全?

释放由glib g_malloc函数分配的缓冲区两次是安全的还是禁止的?

char *buffer = g_malloc(10);
g_free(buffer);
g_free(buffer);
Run Code Online (Sandbox Code Playgroud)

c memory-management glib double-free

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

变量零初始化会降低性能吗?

我正在纠正静态分析 (MISRA-C-2012) 违规行为,其中一项规则(规则 9.3)规定变量应在使用前初始化。

例如:

void bar_read(int * array)
{
    printf("array[1]: %u\n",array[1]);    
}

void bar_write(int * array)
{
    array[1]=1;    
}

int main(void)
{
    #define FOO_SIZE 12
 #ifdef MISRA_VIOLATION_DISABLED
    int foo[FOO_SIZE]  = {0}; //ok
 #else
    int foo[FOO_SIZE]; //violation
 #endif
    bar_read(foo);
    bar_write(foo);
    bar_read(foo); 

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的一些同事声称他们正在删除变量初始化(对于大数组),foo[FOO_SIZE] = {0};因为它会降低性能,这让我感到困惑。

在我的理解中,零初始化变量是在编译时放在 bss 部分中的,并且没有运行时性能影响。

我会错吗?这可能取决于编译器吗?是否有任何优化使其成为现实?

c arrays performance static-analysis initialization

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

不清楚最令人讨厌的解析

让我们假设以下类Foo。

struct Foo
{
  int i;
};
Run Code Online (Sandbox Code Playgroud)

如果要创建此类的实例并对其进行初始化,则应该执行以下操作:Foo foo1 = Foo();调用构造函数。

int main(void)
{
    foo1 = Foo(); 
    cout << foo1.i << " : " << foo1.j << endl; // " 0 " 
}
Run Code Online (Sandbox Code Playgroud)

然后我以为我会用以下代码行调用默认构造函数,但事实并非如此:

int main(void)
{
    Foo foo2(); // most vexing parse
    cout << foo2  << endl; // " 1 " ??? why?
    foo2.i++; // error: request for member ‘i’ in ‘foo2’, which is of non-class type ‘Foo()’ 
}

Run Code Online (Sandbox Code Playgroud)

为什么foo2初始化为int(1)而不是Foo()?

我知道最烦人的解析,它告诉我,当我将一行解释为一个函数时,它就被解释为一个函数。

但是foo1()被解释为一个int,而不是一个函数,也不是一个Foo类。

之所以编译Foo foo2()行是因为它将其解释为函数原型。好。

为什么要编译此行? cout << …

c++ constructor struct default-constructor most-vexing-parse

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

在任何情况下,删除ctor的类都可能有用吗?

我不知道为什么标准允许这种声明。

class A
{
public:
    A() : bar(0){}
    A(int foo) : bar(foo){}
private:
    int bar;
};

class B : public A
{
public:
    B() = delete;
};
Run Code Online (Sandbox Code Playgroud)

B无法实例化。

  B b1; //error: use of deleted function ‘B::B()’
  B b2(2); //error: no matching function for call to ‘B::B(int)’
Run Code Online (Sandbox Code Playgroud)

仍然适用

class A
{
public:
    A() : bar(0){}
private:
    int bar;
};

class B : public A
{
public:
    B() = delete;
};
Run Code Online (Sandbox Code Playgroud)

这使

B b1; //error: use of deleted function ‘B::B()’
Run Code Online (Sandbox Code Playgroud)

注意:这与没有默认的ctor不同:

class …
Run Code Online (Sandbox Code Playgroud)

c++ constructor default-constructor

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

如何使用字符串变量作为运算符?

我想使用字符串作为运算符:

if [[ "$show_output" = "yes" ]]; then
    redirect_cmd_str=' 2>&1 | tee -a'
else
    redirect_cmd_str=' &>>'
fi
$my_command $redirect_cmd_str $my_log
Run Code Online (Sandbox Code Playgroud)

是否可以在 shell/bash 中使用?

bash shell operators

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

如何避免在多态打印宏中使用 #if

让我们尝试运行以下代码:

#include <stdio.h>
#define MY_MACRO1(isArray,y) do { \
                      if(isArray) \
                        printf("%d", y[0]); \
                      else \
                        printf("%d", y); \
                     }while(0)

int main()
{
    int a = 38;
    int b[]={42};

    MY_MACRO1(0,a);

    return 0;
}

Run Code Online (Sandbox Code Playgroud)

它返回错误:

main.c: In function ‘main’:
main.c:12:39: error: subscripted value is neither array nor pointer nor vector
                         printf("%d", y[0]); \
Run Code Online (Sandbox Code Playgroud)

好的,所以我们需要一个 #if 语句来运行 y[0] 仅当变量是一个数组时:

#define MY_MACRO2(isArray,y) do { \
                      #if isArray \
                      printf("%d", y[0]); \
                      #else \
                      printf("%d", y); \
                      #endif \
                     }while(0)

int main()
{
    int a …
Run Code Online (Sandbox Code Playgroud)

c macros if-statement

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

分支预测是否仍在显着加快阵列处理速度?

我正在阅读一篇有趣的文章,内容涉及为什么处理排序数组比未排序数组更快?并看到@ mp31415发表的评论说:

仅作记录。在Windows / VS2017 / i7-6700K 4GHz上,两个版本之间没有区别。两种情况都需要0.6s。如果外部循环中的迭代次数增加了10倍,则两种情况下的执行时间也会增加10倍,至6s

因此,我在一个在线c / c ++编译器(我想是现代服务器体系结构)上进行了尝试,得到的排序和未排序分别为〜1.9s和〜1.85s,没有太大区别,但可重复。

因此,我想知道现代建筑是否仍然适用?问题是从2012年开始的,距离现在不远...还是我错在哪里?


重新开启的问题精确度:

  • 请忘记我添加C代码作为示例。这是一个可怕的错误。不仅代码是错误的,而且将代码发布误导了专注于代码本身而不是问题的人们。

  • 当我第一次尝试上面链接中使用的C ++代码时,只有2%的差异(1.9s和1.85s)。

  • 我的第一个问题和意图是关于上一篇文章,其c ++代码和@ mp31415的注释。

  • @rustyx发表了一个有趣的评论,我想知道它是否可以解释我观察到的内容。

    有趣的是,调试版本在排序/未排序之间的差异为400%,而发布版本的差异最大为5%(i7-7700)。

换句话说,我的问题是:

  • 为什么上一篇文章中的c ++代码不能像上一版OP所声称的那样具有良好的性能?

精确度:

  • 发布版本和调试版本之间的时间差异是否可以解释?

c c++ performance branch-prediction

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