Rust 的文档中有一些我不明白的地方:它是pub前面的关键字use,它有什么作用?这是 Rust 文档中的示例(此处):
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {
println!("Added to waitlist");
}
}
}
pub use crate::front_of_house::hosting;
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
hosting::add_to_waitlist();
hosting::add_to_waitlist();
}
Run Code Online (Sandbox Code Playgroud)
当我尝试制作一个与之交互的主程序时,我想出了:
use tests::eat_at_restaurant;
fn main() {
eat_at_restaurant();
}
Run Code Online (Sandbox Code Playgroud)
但是当我删除关键字时,use它会执行相同的操作,并且在任何情况下都无法hosting::add_to_waitlist从 main 调用,那么这里会发生什么?如果我不输入关键字有什么区别pub?
我正在尝试获取汇编寄存器rdi, rsi, rdx, rcx, 的值r8,但是我得到了错误的值,所以我不知道我正在做的是获取这些值还是告诉编译器在这些值上写入寄存器,如果是这种情况,我怎么能实现我想要做的事情(将汇编寄存器的值放在 C 变量中)?
当此代码编译时(使用gcc -S test.c)
#include <stdio.h>
void beautiful_function(int a, int b, int c, int d, int e) {
register long rdi asm("rdi");
register long rsi asm("rsi");
register long rdx asm("rdx");
register long rcx asm("rcx");
register long r8 asm("r8");
const long save_rdi = rdi;
const long save_rsi = rsi;
const long save_rdx = rdx;
const long save_rcx = rcx;
const long save_r8 = r8;
printf("%ld\n%ld\n%ld\n%ld\n%ld\n", save_rdi, …Run Code Online (Sandbox Code Playgroud) 如何在不使用任何外部库的情况下在 C 中播放不同音调的声音?我知道 C 中有几十个声音库可以让你播放声音,但我想知道它背后是如何工作的?如何告诉计算机以特定的音调/频率演奏特定的音符?我知道在 Windows 上可以使用该sound()函数,但我找不到任何有关 Linux 的文档,我找到的只是输出默认终端蜂鸣声的beep()函数(或write(1, "\a", 1)),但我不知道如何播放不同的声音。
我试图在 C 中重现 std::string 的行为,但是有一件事我真的不知道该怎么做。有一个max_size成员方法可以提供字符串(或向量等的任何其他数组)可以具有的最大大小,所以我的问题是我知道这个值可能取决于系统,那么容器如何确定这个数字?我可以在 C 中得到它吗?
晚上好,如果我的英语不好,请提前抱歉,我是法国人。
因此,在 C 中,有不同的变量类型,例如 int、long、...根据类型需要多个字节,如果我没记错的话,“最大”类型是 long long int(或只是long long) 需要 8 个字节的内存(比如 long 很奇怪,所以如果有人能解释我也谢谢)
所以我的第一个问题是:我可以创建我的自定义变量类型,例如需要 16 个字节,或者如果数字对于 long long(或 unsigned long long)来说太高,我是否被迫使用字符串?
我正在尝试在 C++ 中实现 unique_ptr 类,但如何知道我们传递给它的指针是否是使用new或new[]不使用 using分配的default_delete(我的学校标准不允许 c++11)。我的意思是,当您将指针传递给构造函数时,例如:
unique_ptr<int> ptr(new int[10]);
Run Code Online (Sandbox Code Playgroud)
你如何知道班级内部是否需要打电话delete[]或delete?
标题可能不是很清楚,我解释一下:我只是想在一个字符串被销毁时显示一条消息,所以我这样做了:
std::string::~std::string(void)
{
std::cout << "Destroyed string" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
它没有编译,然后我尝试了这个:
namespace std
{
string::~string(void)
{
cout << "Destroyed string" << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
而且它也不起作用,所以有没有办法做我想做的事?谢谢你的帮助。
我有一个问题,我想使用rand()0 到 6 之间的随机数,但每次运行时它总是给我 4,即使我打电话srand(time(NULL))
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
srand(time(NULL));
int rd = rand() % 7;
printf("%d\n", rd);
return (0);
}
Run Code Online (Sandbox Code Playgroud)
4每次运行时输出
我有这个iter函数,它接受一个指向value_type、 a 的指针,以及一个应该接受 a作为参数的size_type函数指针:fun_typevalue_type&
template <
class value_type,
class size_type,
class fun_type
> void iter(value_type *arr, size_type size, fun_type function)
{ while (size--) function(arr[size]); }
Run Code Online (Sandbox Code Playgroud)
它工作得很好,直到我们有一个带有模板的函数,假设我们想使用这个函数:
template <
class T
> void print(const T &value) { std::cout << value << std::endl; }
Run Code Online (Sandbox Code Playgroud)
然后我们得到这个编译错误:
main.cpp:35:1: error: no matching function for call to 'iter'
iter( tab, 5, print );
^~~~
./iter.hpp:17:8: note: candidate template ignored: couldn't infer template argument 'fun_type'
> void iter(value_type *arr, …Run Code Online (Sandbox Code Playgroud) 我在汇编中的函数指针有问题,即使我的函数返回一个负数,它总是设置rax为一个正数,我做了一个最小的可重现示例,它使用一个比较两个整数的函数,它做同样的事情:
ASM 功能代码 [编辑]:
global foo
section .data
msg: db `superior\n`
msg_len: equ $-msg
section .text
foo:
push rbx
mov rbx, rdi
mov rdi, 2
mov rsi, 1
sub rsp, 8 ; align the stack frame
call rbx
add rsp, 8
test rax, rax ;[EDIT] correct: test eax, eax
js bar
mov rax, 1
mov rdi, 1
mov rsi, msg
mov rdx, msg_len
syscall
bar:
mov rdi, 1
mov rsi, 2
sub rsp, 8 ; same here
call …Run Code Online (Sandbox Code Playgroud) 堆栈对齐在 ASMx64 中是如何工作的?什么时候需要在函数调用前对齐堆栈,需要减去多少?
我不明白这样做的目的是什么。我知道还有其他关于这个的帖子,但对我来说还不够清楚。例如:
extern foo
global bar
section .text
bar:
;some code...
sub rsp, 8 ; Why 8 (I saw this on some posts) ? Can it be another value ? Why do we need to substract?
call foo ; Do we need to align stack everytime we call a function?
add rsp, 8
;some code...
ret
Run Code Online (Sandbox Code Playgroud) assembly x86-64 calling-convention memory-alignment stack-pointer