我使用 C 和 ASM 代码的混合在 STCubeIDE 中对 STM32H745 进行编程。
我有以下结构来确保在两个核心 CM4 和 CM7 之间共享的变量的严格排序:
struct miscSharedMemoryStruct {
volatile uint32_t triggerPtr;
volatile uint32_t bufferHead;
}
__attribute__((section(".sram3"))) struct miscSharedMemoryStruct miscSharedMemory;
Run Code Online (Sandbox Code Playgroud)
该部分.sram3是在链接器脚本中定义的,并且在两个内核的 C 代码中访问我的变量效果很好。到目前为止,一切都很好。然而...
为了允许访问 的条目miscSharedMemory,我在 ASM 代码中将该变量设置为全局变量:
.global miscSharedMemory
Run Code Online (Sandbox Code Playgroud)
为了使汇编器能够识别正确的地址,我在标头中添加了以下定义语句,其中定义了上述结构:
#define BUFFER_HEAD_ADR ((uintptr_t)(&miscSharedMemory.bufferHead))
Run Code Online (Sandbox Code Playgroud)
最后,想法是从上面的地址获取数据到我的 ASM 代码中的寄存器中:
ldr R1, =BUFFER_HEAD_ADR
Run Code Online (Sandbox Code Playgroud)
但是,当编译我的代码时,我得到了
对“BUFFER_HEAD_ADR”的未定义引用
使汇编器识别结构中变量的正确地址的正确方法是什么?我可能不需要执行该.global语句,只要存储的地址BUFFER_HEAD_ADR是正确的并被汇编器识别即可,但我只是不知道如何让它工作。
我目前正在用 C++ 编写一个国际象棋引擎。我使用 github 进行版本控制。当我对引擎进行更改时,我需要针对旧版本进行测试,看看它是否有所改进。这是我遇到麻烦的部分。到目前为止,我将不同版本的引擎保存在不同的文件中,然后将这些文件导入到一个compare_playing_strength.cpp文件中,使它们相互对战。然而,这是代码重复,并且很难保持所有内容兼容。我确信有更好的方法来做到这一点。
我觉得我的问题很笼统,但我还没有找到任何方法来解决它。我考虑过为不同的版本使用不同的 git 分支,但是我无法相互测试引擎。我是否以错误的方式思考这个问题,或者有没有办法让它发挥作用?
我需要正则表达式方面的帮助。我有一个包含 pod 状态(包括年龄)的文件
NAMESPACE NAME READY STATUS RESTARTS AGE
util tomcat-1 0/1 Running 0 38m
util tomcat-2 0/1 Running 0 15m
util tomcat-3 0/1 Running 0 7m
util tomcat-4 0/1 Running 0 4m
util tomcat-5 0/1 Running 0 125s
util tomcat-6 0/1 Running 0 88s
Run Code Online (Sandbox Code Playgroud)
现在我需要打印 Age 大于 6 分钟的 pod。所以在我的输出中我应该看到前 3 个 pod
我一直在使用这个正则表达式,但它在任何几秒和几分钟内都失败,其中包含 0-6 位数字,如 15m
awk '$NF !~ /[0-6][ms]/ { print }' file.txt
Run Code Online (Sandbox Code Playgroud) 是否可以在Python中使用装饰器来实现protected和private访问类的修饰符?如何?
功能应该类似于下面的代码:
class A:
def public_func(self):
self.protected_func() # Runs without warning and error (Because is called in the owner class)
self.private_func() # Runs without warning and error (Because is called in the owner class)
@protected
def protected_func(self):
print('protected is running')
self.private_func() # Runs without warning and error (Because is called in the owner class)
@private
def private_func(self):
print(f'private is running')
a = A()
a.public_func() # Runs without any warning and error (Because has no access modifier) …Run Code Online (Sandbox Code Playgroud) class C
{
public:
C(C&&) = default; // (1)
C& operator=(C&&) = default; // (1)
C(C&&) noexcept = default; // (2)
C& operator=(C&&) noexcept = default; // (2)
}
Run Code Online (Sandbox Code Playgroud)
据我所知,如果移动构造函数/赋值运算符是由用户隐式生成或显式默认的(1),编译器将noexcept根据类的所有成员是否noexcept为移动操作提供保证来决定是否应该使用这些特殊成员函数。但是,如果我想使用默认移动特殊成员并强制它们不noexcept考虑移动操作的底层成员异常保证,该怎么办?(1)和声明之间的编译器有什么区别吗(2)?
我在野外发现了这段代码(这里进行了简化),我想知道这是否被认为是一个好的做法,也就是说,为了更新属性而制作浅拷贝(在本例中是对象的属性) (并减少冗长?)。我问这个问题是因为这不是我第一次看到这种模式,这让我有点困扰。
class DrawingBoard:
def __init__(self):
self.raw_points = []
def add_point(self, x, y):
Point = collections.namedtuple('Point', ['x', 'y'])
points = self.raw_points
points.append(Point(x, y))
if len(points) > ......:
# DO SOMETHING
Run Code Online (Sandbox Code Playgroud)
board = DrawingBoard()
for x in np.arange(0, 10, 0.1):
board.add_point(x, np.sin(x))
Run Code Online (Sandbox Code Playgroud)
对于上下文来说,当添加了一定数量的点时,实际的类正在执行一些操作。在我第一次阅读时,我错过了这次更新的发生(代码相当复杂)。
我的问题是:难道只有我不习惯这种模式,这只是一个简单与清晰的问题吗?还是我还缺少其他东西?我可以看到,如果我们更新多个属性,它可能会变得非常冗长,但我发现调用self.raw_points.append(Points(x,y))而不是points.append(Points(x,y))更清晰。
这是我的程序:
#include <stdio.h>
int main()
{
printf("%g\n", 100000.5);
printf("%g\n", 100001.5);
printf("%g\n", 100002.5);
printf("%g\n", 100003.5);
printf("%g\n", 100004.5);
printf("%g\n", 100005.5);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我使用gcc得到这个输出:
100000
100002
100002
100004
100004
100006
Run Code Online (Sandbox Code Playgroud)
在 VS2019 中,我得到不同的输出,看起来更一致:
100001
100002
100003
100004
100005
100006
Run Code Online (Sandbox Code Playgroud)
为什么输出不同?与编译器有关吗?
从 C#12 开始,该语言支持Collection 表达式,它允许使用扩展元素合并集合。
我的问题是,元组是否有一些类似的语法,特别是组合元组?(见下面的例子)
using System;
// Collection expression
int[] arr1 = [1, 2];
int[] arr2 = [3, 4];
int[] arr3 = [..arr1, ..arr2]; // combine using spread element => [1, 2, 3, 4]
// tuples, similar syntax
var tpl1 = (1, 2);
var tpl2 = (3, 4);
// Sadly this doesn't work?
var tpl3 = (..tpl1, ..tpl2); // Cannot implicitly convert type '(int, int)' to 'System.Index'
Run Code Online (Sandbox Code Playgroud)
我知道,我可以这样做:
var tpl3b = (tpl1.Item1, tpl1.Item2, tpl2.Item1, tpl2.Item2); …Run Code Online (Sandbox Code Playgroud) 我在理解类型何时推导出std::ranges::dangling何时使用命名局部变量与纯右值作为std::ranges::sort算法的参数时遇到问题。例如,我有一个函数将 a 返回prvalue 到标准容器,比如 std::vector ,我直接将其用作 的参数std::ranges::sort,然后我期望std::ranges::dangling在尝试取消引用迭代器时收到编译错误,这就是我得到的:
#include <vector>
#include <algorithm>
auto get_data(){
return std::vector<int>{1, 2, 99, 5, 9, 4};
}
auto get_sorted(){
return std::ranges::sort(get_data());
}
int main(){
auto it = get_sorted();
*(it-1); //Compiler Error because it is a std::ranges::dangling iterator
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我稍微更改get_sorted上面的函数以首先捕获命名变量中的向量并使用它来返回 的结果std::ranges::sort,那么我不会得到悬空迭代器,即使main命名变量中分配的向量应该是函数get_sorted返回后销毁:
auto get_sorted(){
auto vec = get_data();
return std::ranges::sort(vec);
}
int main(){
auto it = get_sorted();
*(it-1); //Okay result = …Run Code Online (Sandbox Code Playgroud) 我一直认为下一个例子是未定义的行为(访问数组越界):
int main()
{
int* a= new int[3];
a[0] = 100;
a++;
unsigned long index = 0;
printf("%d\n", a[-1]);
printf("%d\n", a[index - 1]);
printf("%ul\n", index - 1);
}
Run Code Online (Sandbox Code Playgroud)
然而,这个输出很好:
100
100
4294967295l
Run Code Online (Sandbox Code Playgroud)
为什么下标运算符不会发生下溢?