小编sel*_*bie的帖子

没有自动提交的Git合并

是否可以做一个git merge,但没有提交?

"man git merge"说:

With --no-commit perform the merge but pretend the merge failed and do not autocommit,
to give the user a chance to inspect and further tweak the merge result before
committing.
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用git merge--no-commit它仍然自动提交.这是我做的:

$> ~/git/testrepo$ git checkout master
Switched to branch 'master'

$> ~/git/testrepo$ git branch
* master
  v1.0

$> ~/git/testrepo$ git merge --no-commit v1.0
Updating c0c9fd2..18fa02c
Fast-forward
 file1 |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

$> …
Run Code Online (Sandbox Code Playgroud)

git

350
推荐指数
5
解决办法
20万
查看次数

如何写2 ** n-1作为递归函数?

我需要一个接受n并返回2 n -1的函数。听起来很简单,但是该函数必须是递归的。到目前为止,我只有2 n

def required_steps(n):
    if n == 0:
        return 1
    return 2 * req_steps(n-1)
Run Code Online (Sandbox Code Playgroud)

练习指出:“您可以假设参数n始终为正整数且大于0”

python recursion

49
推荐指数
3
解决办法
5149
查看次数

如何修复seekbar-bar-thumb-centering-issues

我设置了一个搜索栏的拇指图像,但我的拇指看起来在搜索栏的下方.如何将拇指设置在搜索栏的正确位置.看看附图在此输入图像描述

<SeekBar android:id="@+id/PP_Player_SeekBar" 
         android:thumb="@drawable/music_player_playerhead"
         android:paddingLeft="8dip"
         android:paddingRight="8dip"                 
         android:progressDrawable="@drawable/seekbar_drawable_xml_background"
         android:layout_width="236dip"
         android:layout_centerHorizontal="true"
         android:layout_height="wrap_content"
         android:layout_marginTop="47dip"></SeekBar>
Run Code Online (Sandbox Code Playgroud)

谢谢Sunil Kumar Saoo

android seekbar

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

唤醒线程在accept()调用时被阻塞

Linux上的套接字问题

我有一个在accept()调用时阻塞的工作线程.它只是等待传入的网络连接,处理它,然后返回监听下一个连接.

当程序退出的时候,我如何发信号通知此网络工作线程(从主线程)从accept()调用返回,同时仍能正常退出其循环并处理其清理代码.

我试过的一些事情:

  1. pthread_kill发送信号.感觉很难做到这一点,加上它不能可靠地允许线程执行它的关闭逻辑.也使程序终止.如果可能的话,我想避免发出信号.

  2. pthread_cancel可以.与上述相同.这是对线程的严厉杀戮.那个,线程可能正在做其他事情.

  3. 从主线程关闭listen套接字以使accept()中止.这不可靠.

一些限制:

如果解决方案涉及使监听套接字无阻塞,那很好.但我不想接受一个解决方案,该解决方案涉及线程每隔几秒钟通过一次选择呼叫唤醒以检查退出条件.

退出的线程条件可能与退出的进程无关.

从本质上讲,我想要的逻辑看起来像这样.

void* WorkerThread(void* args)
{
    DoSomeImportantInitialization();  // initialize listen socket and some thread specific stuff

    while (HasExitConditionBeenSet()==false)
    {
        listensize = sizeof(listenaddr);
        int sock = accept(listensocket, &listenaddr, &listensize);

        // check if exit condition has been set using thread safe semantics
        if (HasExitConditionBeenSet())
        {
            break;
        }

        if (sock < 0)
        {
            printf("accept returned %d (errno==%d)\n", sock, errno);
        }
        else
        {
            HandleNewNetworkCondition(sock, &listenaddr);
        }
    }

    DoSomeImportantCleanup(); // close listen socket, close connections, cleanup …
Run Code Online (Sandbox Code Playgroud)

c sockets pthreads

25
推荐指数
2
解决办法
2万
查看次数

成员结构是否可以从构造函数初始化列表中归零,而无需调用memset?

假设我有以下结构声明(没有构造函数的简单结构).

struct Foo
{
    int x;
    int y;
    int z;
    char szData[DATA_SIZE];
};
Run Code Online (Sandbox Code Playgroud)

现在让我们说这个结构是C++类的成员,如下所示:

class CFoobar
{
     Foo _foo;
public:
     CFoobar();
};
Run Code Online (Sandbox Code Playgroud)

如果我声明CFoobar的构造函数如下:

CFoobar::CFoobar()
{
    printf("_foo = {%d, %d, %d}\n", _foo.x, _foo.y,_foo.z);
    for (int x = 0; x < 100; x++)
       printf("%d\n", _foo.szData[x]);
}
Run Code Online (Sandbox Code Playgroud)

正如您所料,当CFoobar的构造函数运行时,垃圾数据被打印出来显然,简单的解决方法是memset或ZeroMemory&_foo.这就是我一直以来所做的......

但是,我注意到如果将_foo添加到构造函数的初始化列表中而没有参数,如下所示:

CFoobar::CFoobar()
: _foo()
{
Run Code Online (Sandbox Code Playgroud)

这似乎将_foo的成员变量清零.至少在Linux上使用g ++就是这种情况.

现在这里是我的问题:这是标准的C++,还是这个编译器的特定行为?

如果这是标准行为,有人可以引用我的官方来源参考吗?关于更复杂的结构和类的隐式零初始化行为的任何"陷阱"?

c++ visual-c++

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

在关闭它之前是否有必要从epoll中取消注册套接字?

假设以下代码,其中"sock"是先前使用epfd指定的epoll文件描述符注册的TCP套接字的句柄.

epoll_ctl(epfd, EPOLL_CTL_DEL, sock, &ev);
close(sock);
Run Code Online (Sandbox Code Playgroud)

如果套接字随后会被关闭,是否仍然需要调用epoll_ctl?或者套接字是否因为关闭而被隐式取消注册?

sockets linux epoll

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

无法传递临时对象作为参考

这是一个非常小的例子:

class Foo
{
public:
    Foo(int x) {};
};

void ProcessFoo(Foo& foo)
{
}

int main()
{
    ProcessFoo(Foo(42));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

以上编译在Visual Studio上很好,但在Linux和Mac上生成错误.

编译上面会生成这个:

$ g++ -std=c++11 -c newfile.cpp

newfile.cpp: In function ‘int main()’:
newfile.cpp:23:23: error: invalid initialization of non-const reference of type ‘Foo&’ from an rvalue of type ‘Foo’
     ProcessFoo(Foo(42));
                       ^
newfile.cpp:14:6: note: in passing argument 1 of ‘void ProcessFoo(Foo&)’
 void ProcessFoo(Foo& foo)
Run Code Online (Sandbox Code Playgroud)

我找到了三个解决方法:

  1. 避免使用内联临时变量来调用ProcessFoo.

像这样:

Foo foo42(42);
ProcessFoo(foo42);
Run Code Online (Sandbox Code Playgroud)
  1. ProcessFoo采用const引用: void ProcessFoo(const Foo& foo)

  2. ProcessFoo只是让Foo按值传递. void ProcessFoo(Foo foo)

为什么编译器禁止我的原始代码?(这是什么防守)?上面三个满足编译器的解决方法中的每一个都有什么用?MSVC会允许什么,但不是g …

c++ visual-c++ c++11

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

如何在openssl中管理错误队列(SSL_get_error和ERR_get_error)

在OpenSSl中,大多数SSL_*调用的手册页通过返回值<= 0来指示错误,并建议调用SSL_get_error()来获取扩展错误.

但是在这些调用的手册页以及其他OpenSSL库调用中,有一些模糊的引用在OpenSSL中使用"错误队列" - 在SSL_get_error的手册页中就是这种情况:

   The current thread's error queue must be empty before the TLS/SSL I/O
   operation is attempted, or  SSL_get_error() will not work reliably.
Run Code Online (Sandbox Code Playgroud)

在同一个手册页中,SSL_ERROR_SSL的描述说明了这一点:

   SSL_ERROR_SSL
       A failure in the SSL library occurred, usually a protocol error.
       The OpenSSL error queue contains more information on the error.
Run Code Online (Sandbox Code Playgroud)

这意味着错误队列中有一些值得阅读的东西.无法读取它会使后续调用SSL_get_error变得不可靠.据推测,make的调用是ERR_get_error.

我打算在我的代码中使用非阻塞套接字.因此,重要的是我可靠地发现错误条件是SSL_ERROR_WANT_READ还是SSL_ERROR_WANT_WRITE,这样我就可以将套接字置于正确的轮询模式.

所以我的问题是这样的:

  • SSL_get_error()是否为我隐式调用ERR_get_error()?或者我需要同时使用两者吗?

  • 我应该在每次OpenSSL库调用之前调用ERR_clear_error吗?

  • 在OpenSSL库调用完成后,队列中是否可能存在多个错误?因此,是否存在队列中的第一个错误比上一个错误更相关的情况?

sockets ssl openssl

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

getopt_long_only分段错误

我正在尝试使用getopt_long_only来解析命令行.我的应用程序读取了一些命令行选项.

例如"app --alpha = 1 --beta = 2 --cecil = 3"

只要传入有效的命令行参数,getopt_long_only就可以正常工作.但是如果在末尾和其他不适当的位置调用带有无效"单个虚线"选项的应用程序,则会发生seg故障崩溃.这里发生了什么?似乎getopt_long_only对错误的参数没有弹性.或者我调用函数错了?

例:

> ./app --beta=1 -?
starting
index = 1   ret=0  optarg=1
Segmentation fault
Run Code Online (Sandbox Code Playgroud)

下面的代码(C++:app.cc)

#include <stdio.h>
#include <getopt.h>

void ProcessCommandLineArgs(int argc, char** argv)
{

    option longopts[] = {
        {"alpha", optional_argument, 0, 0},
        {"beta",  optional_argument, 0, 0},
        {"cecil", optional_argument, 0, 0}
    };

    int index;
    int ret;
    bool fParseError = false;

    while (true)
    {
        ret = ::getopt_long_only(argc, argv, "", longopts, &index);

        if (ret < 0)
        {
            break;
        }

        if ((ret …
Run Code Online (Sandbox Code Playgroud)

linux command-line getopt

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

C++如何通过套接字发送结构?

说我有一个结构:

struct person
{
    char name[10];
    int age;
};

struct car
{
    int locationX;
    int locationY;
};

struct company
{
    vector<person> employees;
    vector<car> cars;
};
Run Code Online (Sandbox Code Playgroud)

例如,我想要send/recv整个company使用套接字(UDP).所以,发送和recv一次.

我怎样才能做到这一点?你能给我一些代码吗?如何发送所有内容并阅读所有内容.

谢谢!

c++ sockets serialization struct

11
推荐指数
5
解决办法
2万
查看次数