将C++ string转换为char数组非常简单,使用c_strstring函数然后执行strcpy.但是,如何做到相反?
我有一个char数组,如:char arr[ ] = "This is a test";转换回:
string str = "This is a test.
我遇到了#define他们使用的一个__builtin_expect.
文件说:
内置功能:
long __builtin_expect (long exp, long c)您可以使用
__builtin_expect为编译器提供分支预测信息.一般来说,你应该更喜欢使用实际的配置文件反馈(-fprofile-arcs),因为程序员在预测程序实际执行情况方面是非常糟糕的.但是,有些应用程序难以收集此数据.返回值是值
exp,它应该是一个整数表达式.内置的语义是预期的exp == c.例如:Run Code Online (Sandbox Code Playgroud)if (__builtin_expect (x, 0)) foo ();表示我们不打算打电话
foo,因为我们预计x会为零.
那么为什么不直接使用:
if (x)
foo ();
Run Code Online (Sandbox Code Playgroud)
而不是复杂的语法__builtin_expect?
我有这个程序打印2个不同实例之间的时差,但它打印精度为秒.我希望以毫秒为单位打印它,另一个以纳秒为单位进行打印.
//Prints in accuracy of seconds
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now, later;
double seconds;
time(&now);
sleep(2);
time(&later);
seconds = difftime(later, now);
printf("%.f seconds difference", seconds);
}
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?
我已经创建了一个pthread,并在其中安装了一个信号处理程序,就像我们在main( )函数中一样.线程的信号处理程序是一个单独的函数.令人惊讶的是,它不起作用,即线程的信号处理程序无法捕获信号.
这是代码:
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <signal.h>
typedef struct data
{
char name[10];
int age;
}data;
void sig_func(int sig)
{
printf("Caught signal: %d\n",sig);
signal(SIGSEGV,sig_func);
}
void func(data *p)
{
printf("This is from thread function\n");
signal(SIGSEGV,sig_func); // Register signal handler inside thread
strcpy(p->name,"Mr. Linux");
p->age=30;
sleep(2); // Sleep to catch the signal
}
int main()
{
pthread_t tid;
pthread_attr_t attr;
data *ptr;
pthread_attr_init(&attr);
pthread_create(&tid,&attr,(void*)func,ptr);
pthread_kill(tid,SIGSEGV);
pthread_join(tid,NULL);
printf("Name:%s\n",ptr->name);
printf("Age:%d\n",ptr->age);
}
Run Code Online (Sandbox Code Playgroud)
输出:
分段错误(这意味着信号不会被处理程序捕获)
如果系统调用函数失败,我们通常使用perror来输出错误消息.我想使用fprintf输出perror字符串.我该怎么做这样的事情:
fprintf(stderr, perror output string here);
Run Code Online (Sandbox Code Playgroud) 有人可以告诉我:
caddr_t?void*?void*以及何时使用caddr_t?提前致谢.
我有以下与处理文件和映射它们相关的问题(mmap):
mmap然后写入该文件到内存?mmap- ,,PROT_NONE 使用文件,那么相同的保护级别也可以实现.,等等.那么,为什么?PROT_READPROT_WRITEO_RDONLYO_RDWRmmapmmap有一个文件到内存,如果我们写入mmap返回的内存位置,它是否也同时写入该文件?请帮我回复所有问题.
非常感谢提前.
*编辑:在线程之间共享文件*
据我所知,如果我们在两个线程(非进程)之间共享一个文件,那么建议将mmap它放入内存然后使用它,而不是直接使用该文件.
但我们知道使用文件意味着它肯定在主内存中,那么为什么线程需要再次进行mmaped?
我的git日志显示为:
enter code here
[git_trial]$ git log
commit 4c5bc66ae50780cf8dcaf032da98422aea6e2cf7
Author: king <king@king.ap.com>
Date: Thu Jun 30 15:09:55 2011 +0530
This is third commit
commit 8072be67ddd310bc200cab0dccb8bcb2ec4f922c
Author: king <king@king.ap.com>
Date: Thu Jun 30 14:17:27 2011 +0530
This is the second commit
commit 3ba6ce43d500b12f64368b2c27f35211cf189b68
Author: king <king@king.ap.com>
Date: Thu Jun 30 14:00:01 2011 +0530
This is the first git commit for file1
Run Code Online (Sandbox Code Playgroud)
问题1)现在如何只检查我的第一个版本?问题2)当我这样做时,git只登录File1,为什么它只显示第一次提交?
[git_trial]$ git checkout 3ba6ce43d500b12f64368b2c27f35211cf189b68
Note: moving to "3ba6ce43d500b12f64368b2c27f35211cf189b68" which isn't a local branch
If you want to create a …Run Code Online (Sandbox Code Playgroud) 我正在使用struct timespec结构,这里是:
struct timespec {
time_t tv_sec; /* Seconds */
long tv_nsec; /* Nanoseconds */
};
Run Code Online (Sandbox Code Playgroud)
事实是,用户将为每个成员输入值,我想要检查最多.用户可以输入的值.
我可以拿最大值 的值time_t作为INT最大值?ie INT_MAXfor tv_sec和LONG_MAX(在limits.h中定义)tv_nsec?两者的最低可接受值是多少?它是零吗?我猜不能接受负值?只需添加,这些值将在计时器中使用.
PS:typedef在哪里time_t?在time.h.找不到它.
如果我们要删除我们使用的所有文件和目录,rm -rf *.
但是如果我想要删除所有文件和目录,除了一个特定的文件,该怎么办?
那有什么命令吗?rm -rf *一次性便于删除,但删除了我最喜欢的文件/目录.
提前致谢