我正在寻找为我的日志记录实现api like printf.它应该类似于调用printf.例如:
persistent_log(LogType0, "This is buffered writing %d", i);
Run Code Online (Sandbox Code Playgroud)
我查看了变量参数的内容,但似乎我需要知道那里的参数的数量和类型.所以我需要在这方面提供更多帮助.
我有一个记录列表,在开始我不知道记录的数量.我需要将它们读入数组.因此,建议逐个读取所有记录并逐个进行重新分配,然后随着元素的增加继续增加数组大小,或者我应该花一次通过来识别记录数并只执行一次malloc吗?哪一个计算成本会低一些?
我试图了解mmap的工作原理.mmap的用户级调用如下所示.
void *mmap(void *addr, size_t len, int prot, int flags,
int fildes, off_t off);
Run Code Online (Sandbox Code Playgroud)
但特定设备驱动程序的内核级别mmap如下所示:
int <device_name>_mmap(struct file*fp, struct vm_area_struct *vma)
Run Code Online (Sandbox Code Playgroud)
我也查看了源代码,但我无法找到它们之间的连接.
特定设备的mmap如何获取其参数"struct vm_area_struct*vma"?你能帮我理解一下吗?感谢您的帮助.
下面的代码将stdout重定向到文件fname,然后重定向回原始标准输出.这对我来说可以.但我无法理解它是如何运作的.如果有人能帮助我理解我将会相信它.
printf("\n This is console");
fflush(stdout);
fgetpos(stdout, &pos);
fd = dup(fileno(stdout));
freopen(fname, "a+", stdout);
printf("inside file op");
fflush(stdout);
dup2(fd,fileno(stdout));
close(fd);
clearerr(stdout);
fsetpos(stdout, &pos);
printf("\nBack to Console");
Run Code Online (Sandbox Code Playgroud) 我遇到了一段python list decclaration.我对它的行为感到有点困惑.可以somone请解释这个.感谢您的帮助.
>>> v = [[0]*2]*2
>>> v
[[0, 0], [0, 0]]
>>> v[1][1] = 23
>>> v
[[0, 23], [0, 23]]
>>> v[1][1] = 44
>>> v
[[0, 44], [0, 44]]
>>>
Run Code Online (Sandbox Code Playgroud) 我需要通用的方法来检查void*是否包含0到num_bytes.我提出了以下方法.*p每次都不包含相同类型的数据因此无法做到*(type*)p
bool is_pointer_0(void *p, int num) {
void *cmp;
cmp = (void*)malloc(num);
memset(cmp, 0, num);
if (memcmp(p, cmp, num)) {
free(cmp);
return false;
} else {
free(cmp);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
该函数在每次调用时分配和释放num个字节,我认为不是很好.请建议更快的方法.感谢帮助.
更新:
这种方法怎么样?
bool is_pointer_0(void *p, int num) {
void *a = NULL;
memcpy(&a, p, num);
return a == NULL;
}
Run Code Online (Sandbox Code Playgroud) 如果长字符串通过,则获得以下代码的双倍空闲.我尝试过各种各样的事情.如果我删除了自由线,它会消失.不知道为什么会这样.
void format_str(char *str1,int l,int o) {
char *s = malloc(strlen(str1)+1);
char *s1=s, *b = str1;
int i=0;
while(*str1!='\0') {
i++;
*s1++=*str1++;
if(i>=l) {
if(*str1!=',') {
continue;
}
*s1++=*str1++;
*s1++='\n';
for(i=0;i<o;i++) {
*s1++=' ';
}
i = 0;
}
}
*s1 = '\0';
strcpy(b,s);
free(s);
}
Run Code Online (Sandbox Code Playgroud) 我想在我的程序中包含一个文件说啊,位于某个位置说(/ ws/uname/bd/lib)并且该文件又有不同的include语句,其文件位于截然不同的位置.我怎么能通过将文件包含在"/ ws/uname/bd/lib/ah"中来实现.我不想改变任何头文件.
感谢您的帮助