根据gperftools文档,可以使用以下任何方法启动探查器:
CPUPROFILE环境变量设置为将保存配置文件信息的文件名CPUPROFILESIGNAL和发送适当的信号以启动或停止采样.ProfilerStart(filename)和ProfileStop()从您的代码直接所有这三种方法都需要libprofiler.so链接.
当我尝试这个时,第三种方法有效,但是当我只是设置时CPUPROFILE,没有生成分析信息.
不起作用:
$ cat foo.c
#include <stdio.h>
int main(void) {
printf("Hello, world!\n");
}
$ gcc foo.c -std=c99 -lprofiler -g && CPUPROFILE=foo.prof ./a.out
Hello, world!
$ ls foo.prof
ls: cannot access foo.prof: No such file or directory
Run Code Online (Sandbox Code Playgroud)
工作:
$ cat bar.c
#include <stdio.h>
#include <gperftools/profiler.h>
int main(void) {
ProfilerStart("bogus_filename");
printf("Hello, world!\n");
ProfilerStop();
}
$ gcc -std=c99 bar.c -lprofiler -g && CPUPROFILE=foo.prof ./a.out …Run Code Online (Sandbox Code Playgroud) 抱歉,如果我冒犯了任何人,但是我本周开始学习C,并且在编译此代码时遇到了段错误。我能否再请一双眼睛帮助我解决此错误?
void Space(void *empty, size_p s)
{
empty = malloc(s);
}
int main()
{
int *p = NULL;
Space(p, sizeof(p));
*p = 7;
return;
}
Run Code Online (Sandbox Code Playgroud) 该代码不适用于测试用例17 13 3 15。在我尝试过的其他测试案例中,它运行良好。
#include <stdio.h>
int max_of_four(int a,int b,int c,int d)
{
int max;
a=max;
if(b>max)
max=b;
if(c>max)
max=c;
if(d>max)
max=d;
return max;
}
int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int ans = max_of_four(a, b, c, d);
printf("%d", ans);
return 0;
}
Run Code Online (Sandbox Code Playgroud)