我发现几乎每个虚拟析构函数的代码片段都将它作为公共成员函数,如下所示:
class Base
{
public:
virtual ~Base()
{
cout << "~Base()" << endl;
}
};
class Derived : public Base
{
public:
~Derived()
{
cout << "~Derived()" << endl;
}
};
Run Code Online (Sandbox Code Playgroud)
虚拟析构函数必须是公共的还是存在非公共虚拟析构函数有意义的情况?
我为linux做了一个简单的shell.它与getline()逐行读取,直到ctrl + d(eof/-1)进入标准输入.
在进入stdin时,逐行代码:
ls -al &
ls -a -l
Run Code Online (Sandbox Code Playgroud)
我的shell工作得很好.
我试图通过我的shell运行脚本,但它不起作用.当我执行脚本时,我的shell会自动执行(第1行),但shell不会解释其他行.
#!/home/arbuz/Patryk/projekt/a.out
ls -al &
ls -a -l
Run Code Online (Sandbox Code Playgroud)
什么可能导致它?我不得不说我是linux的初学者而老师并没有对所有这些东西说什么.只是一个功课.我已经完成了一些研究,但这些都是我发现的.
这是我的Shell的代码.我已经将shell路径添加到etc/shells中,但它仍然无效
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
int main()
{
ssize_t bufer_size = 0;
char* line = NULL;
int line_size;
while ((line_size = getline(&line, &bufer_size, stdin)) != -1) // while end of file
{
char** words_array;
words_array = (char**)malloc(200 * sizeof(char*));
int words_count = 0;
int i;
int j = 0;
int words_length = 0;
char …Run Code Online (Sandbox Code Playgroud) 可能重复:
如何计算32位整数中的设置位数?
给出一个unsigned char类型值,计算它中的总位数.最快的方法是什么?我写了三个函数如下,最好的方法是什么,有人能想出一个更快的吗?(我只想要极快的一个)
const int tbl[] =
{
#define B2(n) n, n+1, n+1, n+2
#define B4(n) B2(n), B2(n+1), B2(n+1), B2(n+2)
#define B6(n) B4(n), B4(n+1), B4(n+1), B4(n+2)
B6(0), B6(1), B6(1), B6(2)
};
char naivecount (unsigned char val)
{
char cnt = 0;
while (val)
{
cnt += (val & 1);
val = val >> 1;
}
return cnt;
}
inline tableLookUp(int val)
{
assert(val >= 0 && val <= 255);
return tbl[val];
}
int asmCount(int val)
{
int res = …Run Code Online (Sandbox Code Playgroud) Meyers的书" Effective Modern C++ "第16项中的一个例子.
在一个缓存昂贵的计算int的类中,您可能会尝试使用一对std :: atomic avriable而不是互斥锁:
class Widget {
public:
int magicValue() const {
if (cachedValid) {
return cachedValue;
} else {
auto val1 = expensiveComputation1();
auto val2 = expensiveComputation2();
cachedValue = va1 + val2;
cacheValid = true;
return cachedValue;
}
}
private:
mutable std::atomic<bool> cacheValid { false };
mutable std::atomic<int> cachedValue;
};
Run Code Online (Sandbox Code Playgroud)
这样可以工作,但有时它会比它应该工作得多.考虑:一个线程调用Widget :: magicValue,将cacheValid视为false,执行两个昂贵的计算,并将它们的总和分配给cachedValud.此时,第二个线程calidget Widget :: magicValue也将cacheValid视为false,因此执行与第一个线程刚刚完成的相同的昂贵计算.
然后他用互斥量给出了一个解决方案:
class Widget {
public:
int magicValue() const {
std::lock_guard<std::mutex> guard(m);
if (cacheValid) {
return cachedValue;
} else …Run Code Online (Sandbox Code Playgroud) 我试图将两个字母附加到一个字符串,但似乎字符串没有改变:
void fun()
{
string str;
str += 'a' + 'b';
cout << str;
}
Run Code Online (Sandbox Code Playgroud)
我检查了STL的源代码并找到了执行operator+=,但我仍然不知道为什么.
basic_string&
operator+=(_CharT __c)
{
this->push_back(__c);
return *this;
}
Run Code Online (Sandbox Code Playgroud) 对于地址清理程序也有类似的问题,但对于线程清理程序,它不起作用,我尝试中断 __sanitizer_print_stack_trace,但它也不起作用。
这是一个用于检查数字是否为素数的代码:
bool IsPrime(int num)
{
if(num<=1)
return false;
if(num==2)
return true;
if(num%2==0)
return false;
int sRoot = sqrt(num*1.0);
for(int i=3; i<=sRoot; i+=2)
{
if(num%i==0)
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
表达"num*1.0"是什么意思?
我编写下面的代码来测试talloc:
#include "talloc.h"
typedef struct linklist
{
char* str;
struct linklist* next;
}LinkList;
int main(int argc,char* argv[])
{
LinkList* lptr=talloc(NULL,LinkList);
lptr->str=talloc_strdup(lptr,"Test ptr");
talloc_free(lptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但得到了编译错误:
talloctest.c:(.text+0x21): undefined reference to `talloc_named_const(void const*, unsigned int, char const*)'
talloctest.c:(.text+0x39): undefined reference to `talloc_strdup(void const*, char const*)'
talloctest.c:(.text+0x4d): undefined reference to `talloc_free(void*)'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
然后我找到相关宏和函数的定义:"talloc.h"中的定义:
#define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
char *talloc_strdup(const void *t, const char *p);
Run Code Online (Sandbox Code Playgroud)
"talloc.c"中的实现:
void *talloc_named_const(const void *context, size_t size, …Run Code Online (Sandbox Code Playgroud) 我对myname数组的生命周期感到困惑,它是否仍然存在于if语句之外?我们在c和c ++中得到了相同的答案吗?
int main (int argc, char* argv[])
{
char* host;
if (1 == argc)
{
/*code below is copied from a book*/
char myname[256];
gethostname(myname, 255);
host = myname;
/*code above is copied from a book*/
}
else
{
/* */
}
printf("%s\n",host);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
代码片段来自本书Begining Linux Programming 4th edition,第15章:套接字,我很抱歉作者犯了这样的错误.但是我认为这本书很好,不包括这段代码.
假设我们1e10每天都有关于日志文件的行,每个行包含:ID号(长度小于15位的整数),登录时间和注销时间.某些ID可能会多次登录和注销.
问题1:
如何计算已登录的ID总数?(我们不应将每个ID计算两次或更多)
我试图在这里使用哈希表,但我发现我们应该获得的内存可能非常大.
问题2:
计算在线用户数量最大的时间.
我想我们可能拆分一天的时间到86400秒,那么日志文件的每一行,在网上区间加1到每个秒.或者我可以按登录时间对日志文件进行排序?