抽象函数和虚函数有什么区别?在哪些情况下建议使用虚拟或抽象?哪一个是最好的方法?
当我在 ios13 xcode11 beta 上运行我的项目时。
[UIApplication sharedApplication].statusBarFrame.size.height
代码返回 0。
我应该怎么做才能使其适应 ios13?
我的问题可能看起来很奇怪,事实上,这是上下文:
我目前面临一个奇怪的问题,同时切换 -在我正在从事的项目上- 从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) 我想在我的一些脚本中添加一些客户标志,以便在 shell 脚本打包之前对其进行解析。
比方说,删除之间的所有多行文本
^([#]|[//]){0,1}[_]+NOT_FOR_CUSTOMER_BEGIN[_]+\n
和之间
^([#]|[//]){0,1}[_]+NOT_FOR_CUSTOMER_END[_]+\n
我希望它具有容错性(关于“_”的数量),这就是我使用正则表达式的原因。
例如:
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)
会变成:
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) 释放由glib g_malloc函数分配的缓冲区两次是安全的还是禁止的?
char *buffer = g_malloc(10);
g_free(buffer);
g_free(buffer);
Run Code Online (Sandbox Code Playgroud) 我正在纠正静态分析 (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 部分中的,并且没有运行时性能影响。
我会错吗?这可能取决于编译器吗?是否有任何优化使其成为现实?
让我们假设以下类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
我不知道为什么标准允许这种声明。
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) 我想使用字符串作为运算符:
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 中使用?
让我们尝试运行以下代码:
#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) 我正在阅读一篇有趣的文章,内容涉及为什么处理排序数组比未排序数组更快?并看到@ 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 ×5
c++ ×4
constructor ×2
embedded ×2
performance ×2
shell ×2
abstract ×1
arrays ×1
awk ×1
bash ×1
double-free ×1
glib ×1
if-statement ×1
ios ×1
ios13 ×1
macros ×1
objective-c ×1
oop ×1
operators ×1
python ×1
riscv ×1
sed ×1
storage ×1
struct ×1
substitution ×1
templates ×1