小编boy*_*ang的帖子

Linux支持"tail +2"吗?

我注意到tail +2Solaris ksh支持,但在Red Hat Linux中,会出现错误:

c008>> ps -p 4009,6282,31401,31409 | tail +2
tail: cannot open `+2' for reading: No such file or directory
Run Code Online (Sandbox Code Playgroud)

在Solaris中,

bjbldd>> ps -p 2622,16589,11719,846 |tail +2
16589 ??       0:00 xterm
  846 pts/180  0:00 cscope
11719 pts/180  0:00 cscope
2622 pts/114  0:00 apxcscop
Run Code Online (Sandbox Code Playgroud)

该行PID TTY TIME CMD被"tail +2"排除.

我知道grep -v PID会工作.但我想知道Linux尾部是否有类似的选项?

tail

9
推荐指数
1
解决办法
1万
查看次数

此代码段在C中是否合法?

函数wait()在另一个函数中声明.这合法吗?

void panic(const int reason, const char *strg) 
{
int ErrNo;
struct machine_attributes mach;
int ret, docstat, cnt;
pid_t pid, wait(int *), setsid(void); 
    ......
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

c

4
推荐指数
1
解决办法
162
查看次数

sigaction既可以是结构又可以是函数吗?

我注意到它sigaction被定义为结构和函数(http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html):

    int sigaction(int, const struct sigaction *restrict,
       struct sigaction *restrict);
Run Code Online (Sandbox Code Playgroud)

使用它的一个例子是:

    struct sigaction sa;

    /* Set up handler */
    sa.sa_flags = SA_SIGINFO|SA_RESTART;
    sa.sa_sigaction = timer_expiry;

    /* Setup signal watchdog */
    if (sigaction(SIG_WDOG, &sa, NULL) == -1) {
       printf("ERROR: Failed to set wdog signal with %s",
           strerror(errno));
    }
Run Code Online (Sandbox Code Playgroud)

c unix linux sigaction

4
推荐指数
1
解决办法
582
查看次数

C++ 11向量的向量初始化

在C++ 11中,{}首选()变量初始化.但是,我注意到{}无法正确初始化矢量矢量.

考虑下面的代码,vector<vector<int>> mat2(rows, vector<int>(cols, 2))vector<vector<int>> mat4{rows, vector<int>(cols, 4)}如预期工作,但vector<vector<int>> mat1{rows, vector<int>{cols, 1}}vector<vector<int>> mat3(rows, vector<int>{cols, 3})没有.谁能解释为什么?

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

string parse_matrix(const vector<vector<int>>& mat)
{
    stringstream ss;
    for (const auto& row : mat) {
        for (const auto& num : row)
            ss << std::setw(3) << num;
        ss << endl;
    }
    return ss.str();
}

int main()
{
    const int rows …
Run Code Online (Sandbox Code Playgroud)

vector matrix c++11

2
推荐指数
1
解决办法
631
查看次数

如何安全地比较两个无符号整数计数器?

我们有两个无符号计数器,我们需要比较它们以检查一些错误条件:

uint32_t a, b;
// a increased in some conditions
// b increased in some conditions
if (a/2 > b) {
   perror("Error happened!");
   return -1;
}
Run Code Online (Sandbox Code Playgroud)

问题是,ab会溢出一些日子.如果a溢出,它仍然可以.但如果b溢出,那将是一种误报.如何使这个检查防弹?

我知道制作ab uint64_t会延迟这种误报.但它仍然无法完全解决这个问题.

===============

让我澄清一点:计数器用于跟踪内存分配,这个问题可以在dmalloc/chunk.c中找到:

#if LOG_PNT_SEEN_COUNT
  /*
   * We divide by 2 here because realloc which returns the same
   * pointer will seen_c += 2.  However, it will never be more than
   * twice the iteration value.  We divide …
Run Code Online (Sandbox Code Playgroud)

c c++ integer-overflow unsigned-integer

1
推荐指数
1
解决办法
200
查看次数

标签 统计

c ×3

c++ ×1

c++11 ×1

integer-overflow ×1

linux ×1

matrix ×1

sigaction ×1

tail ×1

unix ×1

unsigned-integer ×1

vector ×1