小编ano*_*est的帖子

我怎么能模拟缺少文件描述符?

我想挑起一个没有文件描述符的情况.

我一直在考虑两种可能性:

  1. 随机打开数千个文件,直到打开结果为-1
  2. 设置一个非常低数量的文件描述符(比如stdin,stdout和stderr).

你会怎么做?

c linux file-descriptor

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

找不到C中的getrandom系统调用

通过升级C库解决了该问题.


我想使用syscall getrandom(http://man7.org/linux/man-pages/man2/getrandom.2.html)

gcc-5 -std = c11 test.c

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <linux/random.h>
#include <sys/syscall.h>

int main(void)
{
        void *buf = NULL;
        size_t l = 5;
        unsigned int o = 1;
        int r = syscall(SYS_getrandom, buf, l, o);
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

要么

 int main(void)
    {
            void *buf = NULL;
            size_t l = 5;
            unsigned int o = 1;
            int r = getrandom(buf, l, o);
            return 0;
    }
Run Code Online (Sandbox Code Playgroud)

无论如何,当我尝试用gcc-5编译它时:

test.c: …
Run Code Online (Sandbox Code Playgroud)

c linux ubuntu gcc system-calls

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

程序在c中读取一个char数组

我对C很新.

#include <string.h>
#include <stdio.h>
int main(int argc, char *argv[])
{ 
    char* c=argv[1];
    for (int i=0;i<sizeof(c);i++)
    {
        printf("%c\n",c[i]);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试编写一个程序来打印单词的每个字符.

当我尝试测试时:它显示当我尝试使用testtesttest时:显示:t e s t

我不明白为什么,你能告诉我为什么好吗?

c

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

迭代器和模板

我试图操纵迭代器.

template <typename Mytype>
class Myclass
 {
   public:
   Myclass ( const Mytype & myarg)
   {
    this->data=myarg;
   }
   ~Myclass ( void ){}
    set<int> Test ( const Mytype & myarg ) const
    {
      set<int> result;
      typename Mytype::iterator it=myarg.begin();
      return result;
    }
   private:
    //
    mutable Mytype data;
 };
Run Code Online (Sandbox Code Playgroud)

这段代码正在编译.但是当我想使用它时,它不再编译:这是一个例子:

int main()
 {
    Myclass <string> t0 ( "caacaa" );
    set<int> r=t0.Test("a");
    return 0;
 }
Run Code Online (Sandbox Code Playgroud)

我现在得到一个错误:

test.cpp: In member function ‘std::set<int>
Myclass<Mytype>::Test(const Mytype&) const [with Mytype =
std::basic_string<char>]’:
test.cpp:38:26: instantiated from here
test.cpp:25:48: …

c++ generics templates iterator c++11

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

泛型类中的语法

我试图用模板解决一个练习.我的代码在大多数情况下运行良好,但我发现有一个案例无效,这是我的代码的一部分.默认比较器是<.

template <typename type1, typename typecomparator=less<typename type1::valuetype1> >
class Myclass
 {
   public:
   Myclass ( const type1 & arg1,const typecomparator & comparator = typecomparator () )
   {
    this->seq=arg1;
    this->comp=comparator;
   }
   ~Myclass ( void ){}
   // ...
   private:
    mutable type1 seq;
    typecomparator comp;
 };
Run Code Online (Sandbox Code Playgroud)

代码工作几乎是所有情况.为例:Myclass <string> test ( "abcabcabc" );

但是当我想要使用一个类时:

class another
 {
   public:
                   another ( bool caseSensitive )
                    : m_CaseSensitive ( caseSensitive ) { }
    bool           operator () ( const string & a, const string & b ) const …
Run Code Online (Sandbox Code Playgroud)

c++ templates comparator

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