First写和写有区别吗Second?
template<class T>
void fun(T&& /*var*/) {}
// First
template<>
void fun<int>(int&& var) = delete;
// Second
template<int>
void fun(int&& var) = delete;
First写和写有区别吗Second?
template<class T>
void fun(T&& /*var*/) {}
// First
template<>
void fun<int>(int&& var) = delete;
// Second
template<int>
void fun(int&& var) = delete;
test["hello"]?fun(test["hello"]),但在函数内部fun我们没有赋值,然后我们调用test.find("hello"). 我们有元素吗?如果有,那里会有什么(零,垃圾......)?代码:
void fun(int& val) {
val = 900;
}
int main()
{
std::unordered_map<std::string, int> test;
fun(test["hello"]);
return 0;
}
我read()的角色驱动程序功能
static ssize_t my_driver_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
printk(KERN_ALERT "Function read() works and received %zu characters\n", count);
if (copy_to_user(buf, my_driver_buf, count)) {
printk(KERN_ALERT "Error in function copy_to_user()\n");
return -EFAULT;
}
*f_pos += count;
return count;
}
好吧,一个功能write()以防万一
static ssize_t my_driver_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
printk(KERN_ALERT "Function write() works and received %zu characters\n", count);
if (copy_from_user(my_driver_buf, buf, count)) {
printk(KERN_ALERT "Error in function copy_from_user()\n");
return -EFAULT;
}
*f_pos += count;
return count;
}
我的缓冲区被声明为全局的
static char my_driver_buf[140000];
这些函数的实际问题(由file_operations字符驱动程序中的结构成员指出):
当我编写这样一个命令strace echo hello > /dev/my_driver(设备文件)时,我得到以下输出(修剪到最后几行)
write(1, "hello\n", 6) = 6
close(1) = 0
close(2) = 0
exit_group(0) = ?
+++ exited with 0 +++
而当我写这样一个命令strace cat /dev/my_driver时,输出是这样的(从上面和下面截掉文字)
read(3, "hello\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 131072) = 131072
write(1, "hello\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 131072hello
) = 131072
read(3, "hello\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 131072) = 131072
write(1, "hello\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 131072hello
) = 131072
read(3, "hello\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 131072) = 131072
write(1, "hello\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 131072hello
) = 131072
(重复直到我按下 Ctrl+C)
问题正在酝酿:
1)为什么count函数中的变量write()等于6(hello\n),而当我调用函数时read(),count它等于131072(hello\n\0\0\0....),而不是6?
2)为什么函数read()进入循环并再次被调用,虽然我写return count;在函数的末尾read(),这是最好的选择(所有数据都被接收)?
loff_t *f_pos3)如果我们仍然不向文件(если я правильно понимаю, то мы работаем с буфером пользовательского пространства, который предоставляет ядро и с буфером, который мы сами создаем, в нашем случае - my_driver_buf)写入任何内容并且我们不需要知道其中的位置(至少在这个例子中),为什么我们需要?
要使用символьный драйвер- 我们需要创建файл устройста,它是специальный файлor узел дерева файловой системы。对于需要什么,我有两种意见,请告诉我哪一种是正确的(如果有的话)。
1) 这是一个用作一种接口的文件。例如,当我们编写这样的命令echo "hello" > /dev/my_driver时,该行"hello"不是写入文件本身/dev/my_driver,而是写入内核提供给我们的缓冲区。函数等也一样open,read它们并不打开文件本身,而只是简单地发出信号,表明有必要创建和打开某个文件或读取数据。
2)这是一个常规文件,我们将数据写入其中并从那里读取。使用相同的命令echo "hello" > /dev/my_driver,字符串"hello"将直接写入文件/dev/my_driver。
问题描述如下,这是一个可选的介绍
有两个函数,第一个接收带有文件位置的字符串,第二个接收该位置的所有文件(我不确定这些函数是否有问题,但为了清楚起见,我仍然会插入他们)
char *fun_get_cur_path(void){
char *buf = malloc(sizeof (char) * BUF_PATH);
if( getcwd(buf, BUF_PATH) == NULL){
if(errno == ERANGE){
printf("Buffer is small\n");
exit(EXIT_FAILURE);
}
perror("Can't get current dir");
exit(EXIT_FAILURE);
}
// printf("\n");
return buf;
}
int list_in_dir(const char *path, struct dirent **new_entry)
{
struct dirent *entry;
DIR *dir;
int i = 0;
dir = opendir(path);
if (dir == NULL) {
printf("Can't open path: %s\n", path);
exit(EXIT_FAILURE);
}
while ( (entry = readdir(dir)) != NULL) {
if( !strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue;
new_entry[i++] = entry;
}
closedir(dir);
return i;
}
嗯,有主要
int main(){
char *buf_path = NULL;
struct dirent *entry[COUNT_OF_FILES];
int count_of_entry_files = 0;
buf_path = fun_get_cur_path();
count_of_entry_files = list_in_dir(buf_path, entry);
printf("---------------------------------------------------------------------------\n");
printf("FILES\n\n");
for(int i = 0; i < count_of_entry_files; i++)
printf("%s\n", entry[i]->d_name);
printf("\n");
}
实际问题是什么:当我运行此代码时,显示以下内容:
--------------------------------------------------------------------------
FILES
-------
eg
test
main.o
Makefile
testing.txt
Press <RETURN> to close this window...
它应该输出以下内容
--------------------------------------------------------------------------
FILES
ASCII.jpeg
test
main.o
Makefile
testing.txt
Press <RETURN> to close this window...
也就是说,zanks-----替换了文件名。如果我禁用缓冲stdout,那么一切正常。或者,如果我添加printf("\n")到函数的末尾fun_get_cur_path(),那么它似乎也可以工作(但在我看来,这是暂时的,直到我增加字符数-----)。
可能是什么问题呢?
为什么这段代码工作不正确(从内存中给出随机数)?
#include <stdio.h>
int fun(int n, ... ){
int *ptr = &n;
ptr++;
return *ptr;
}
int main()
{
printf("%d\n", fun(1, 10));
return 0;
}
在网上找到一个打印“Hello, world”的NASM程序。请解释为什么_start在程序开始时需要一个标签,如果没有它一切都可以工作?(如果我没记错的话)
global _start
section .text
_start: mov rax, 1 ; system call for write
mov rdi, 1 ; file handle 1 is stdout
mov rsi, message ; address of string to output
mov rdx, 13 ; number of bytes
syscall ; invoke operating system to do the write
mov rax, 60 ; system call for exit
xor rdi, rdi ; exit code 0
syscall ; invoke operating system to exit
section .data
message: db "Hello, World", 10 ; note the newline at the end
1-2天前我问了一个问题,但不是很正确,所以现在我将尝试更正确地提出。假设有一个游戏,您需要按其中一个键盘键。我,作为一个懒惰的人,想要自动化所有这些动作,所以我的梦想是编写一个 C/C++ 应用程序,它会随着游戏打开,并将按下的键盘按键的信号发送给游戏而不是我。所以我想知道是否可以在 C/C++ 中创建这样的应用程序?如果可能的话,请告诉我要阅读什么以及在哪里看,我将不胜感激。