是不是在typeid内部调用的函数?请考虑以下代码.
#include <iostream>
#include <typeinfo>
using namespace std;
int mul10(int &s)
{
static int count = 1;
cout << "Evaluating call " << count << endl;
count++;
s *= 10;
return(s);
}
int main()
{
int i = 5;
cout << typeid(mul10(i)).name() << endl;
cout << i << endl;
return(0);
}
Run Code Online (Sandbox Code Playgroud)
所以这里的输出是
int
5
Run Code Online (Sandbox Code Playgroud)
很明显,价值i并没有改变,而且mul10实际上并没有调用这个功能.这是怎么评估typeid参数的?
我这里有一个使用localtime函数的代码.但是对于其输入参数的某些值,代码崩溃(返回空指针).我想知道它的输入允许范围.
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm g;
struct tm *gp;
__int64 tim;
tim = 7476811632013133299LL; // I know it's a weird number but valid for time_t
rawtime = tim / 1000LL;
gp = localtime(&rawtime);
printf("Pointer gp = %p\n", gp);
g = *gp; // this crahses because gp = NULL
return 0;
}
Run Code Online (Sandbox Code Playgroud)
那么可以说关于本地时间函数的允许输入范围呢?
我写了一个代码,但是我不确定为什么最后一个printf给出了意外的结果。我的命令行参数分别是12和15。代码如下:
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
struct in_addr adr, adr2;
int num, num2;
num = atoi(argv[1]);
num2 = atoi(argv[2]);
adr.s_addr = htonl(num);
adr2.s_addr = htonl(num2);
printf("%x %x\n", num, adr.s_addr); # c c000000
printf("%x %x\n", num2, adr2.s_addr); # f f000000
printf("%s\n", inet_ntoa(adr)); # 0.0.0.12
printf("%s\n", inet_ntoa(adr2)); # 0.0.0.15
printf("%s %s\n", inet_ntoa(adr), inet_ntoa(adr2)); # 0.0.0.12 0.0.0.12
return(0);
}
Run Code Online (Sandbox Code Playgroud)
我期望最终输出为“ 0.0.0.12 0.0.0.15”,但事实并非如此。谁能帮忙吗?
我知道“can”方法检查包是否具有“some_method”方法。但是,“->(animal => $x)”部分发生了什么?
$z = __PACKAGE__->can("some_method")->(animal => $x)
Run Code Online (Sandbox Code Playgroud) 我用 C 编写了一个小代码,其中定义了两个结构类型,它们在定义中具有彼此的成员。情况 1:如果 struct foo 在 struct bar 之前定义,则代码按预期编译。情况 2:如果 struct foo 在 struct bar 之后定义,它将不会编译,这也是预期的,因为无法知道 struct foo 变量的内存要求。但是我期待如果在情况 2 中使用 struct foo 的前向声明它会编译。但它不起作用。我错过了什么?
#include<stdio.h>
#include<stdlib.h>
struct foo; // forward declaration
struct bar
{
int a;
struct bar *next;
struct foo ch;
};
struct foo
{
struct bar *n;
struct foo *nx;
};
int main()
{
struct bar p;
return(0);
}
Run Code Online (Sandbox Code Playgroud) 我有一个类定义.我对一些构造函数行为感到困惑.以下是代码.
#include <iostream>
#include <cstdlib>
using namespace std;
class A
{
int i;
public:
void seti(int v) {cout << "Setting i\n"; i=v;}
void print() {cout << "i = " << i << endl;}
A(){};
A(int v) {cout << "Setting i in par-ctor to " << v << "\n"; i=v;}
A(A& o) {cout << "In copy ctor, changing i to " << o.i << "\n";i=o.i;}
A& operator=(A o) {cout << "In assignment op\n"; this->i = o.i; return(*this);}
};
int main()
{ …Run Code Online (Sandbox Code Playgroud) 我刚写了一个简单的代码来以hh:mm:ss格式显示时间.代码是
#include <stdio.h>
#include <time.h>
int main()
{
time_t curtime;
int h, m, s, ps;
struct tm *x = localtime(&curtime);
time(&curtime);
ps = (*x).tm_sec;
while(1)
{
time(&curtime);
x = localtime(&curtime);
h = (*x).tm_hour;
m = (*x).tm_min;
s = (*x).tm_sec;
if(s != ps)
{
ps = s;
printf("%02d:%02d:%02d\n", h, m, s);
}
}
return(0);
}
Run Code Online (Sandbox Code Playgroud)
代码按预期编译和运行.然而,CPU使用率似乎非常高.当我使用'top'来查看cpu使用情况时,它显示cpu%为96-100%(我可以听到计算机风扇响亮).如何从性能角度改进代码,保持代码简单和简短?
嗨,我是Perl世界的新人.为什么'use strict'不允许我打开并写入文件,如下面的代码?评论'use strict'非常有效.
use strict;
use warnings;
my $filename = "file_abc.txt";
open($fh, '>', $filename) or die("Couldn't open $filename\n");
print $fh "ABC";
print $fh "DEF\n";
print $fh "GHI";
close $fh;
Run Code Online (Sandbox Code Playgroud) 我是Perl的新手.我只需要发送一个将两个参数带到另一个函数的通用函数,然后从第二个函数内部调用第一个函数.我不太清楚如何做到这一点.这是我想写的代码.
sub add { return $_[0] + $_[1]; }
sub subt { return $_[0] - $_[1]; }
sub dosth
{
my ($func, $num0, $num1) = @_;
# how to call code $func with arguments $num0 and $num1 and return the return value of $func
}
print dosth(add, 3, 2) . " " . dosth(subt, 3, 2); # desired output: 5 1
Run Code Online (Sandbox Code Playgroud) 我有三个C代码.在第一个代码c0.c中,整数指针(p)是动态分配的存储器,用于保存一个整数值.值325被分配给该指针变量(p)指向的内存.该指针的整数值存储在一个文件中.在不释放内存的情况下,该指针变量(p)被赋予NULL值.然后将整数值再次读入长变量(i),并为指针变量(p)分配该变量(i)的(int*)值.取消引用并打印时,它会输出值325.代码如下所示.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p = (int*)malloc(sizeof(int));
*p = 325;
FILE *F;
F = fopen("xxx", "w");
fprintf(F, "%ld", (long)p);
fclose(F);
p = NULL;
long i;
F = fopen("xxx", "r");
fscanf(F, "%ld", &i);
p = (int*)i;
fclose(F);
printf("value stored in read pointer = %d\n", *p);
return(0);
}
Run Code Online (Sandbox Code Playgroud)
现在使用两个单独的文件c1.c和c2.c尝试相同的事情.在c1.c中,指针p被分配了存储器,值325被存储在它指向的存储器中.指针的整数值存储在一个文件中,程序执行由scanf暂停.在c2.c中,读取指针的整数值并将其分配给另一个整数指针.取消引用此指针变量,并尝试打印该值.预计产量为325.因此,当c1.c被编译并运行并且暂停时,将运行c2.c的已编译可执行文件.它崩溃了.为什么?
c1.c和c2.c如下.
c1.c:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p = (int*)malloc(sizeof(int));
*p = 325;
FILE *F;
F = fopen("xxx", "w");
fprintf(F, "%ld", (long)p);
fclose(F);
int j; …Run Code Online (Sandbox Code Playgroud)