小编Sca*_*cab的帖子

从内部使用静态std :: string的函数返回const char*是否安全?

我正在查看以下代码(简化)并问自己使用此returnMsg函数有多安全:

#include <iostream>

using namespace std;

const char *returnMsg(const char *msg)
{
    static std::string message;

    message = msg;

    return message.c_str();
}

int main(int argc, char *argv[])
{
    const char *msg1 = returnMsg("Hello world");
    printf("msg1 = %p\n", msg1);
    cout << msg1 << endl;

    const char *msg2 = returnMsg("Good bye");
    printf("msg2 = %p\n", msg2);
    cout << msg2 << endl;

    cout << msg1 << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

msg1 = 0x23a6028
Hello world
msg2 = 0x23a6028
Good bye
Good bye …
Run Code Online (Sandbox Code Playgroud)

c++

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

如何使用 musb_hdrc Linux 驱动程序断开特定的 USB 设备?

首先,我是 Linux USB 堆栈的新手,我试图更多地了解它以实现以下结果:我需要重新连接/断开特定的 USB 设备,因为有时,这个设备并不常见,很难不再响应,唯一的解决方案是从设备上物理断开/重新连接 USB 电缆。

在我的主板 (AM33x Sitara) 上,有 2 个 USB 控制器与 musb-hdrc 驱动程序绑定到它们:

# pwd /sys/bus/platform/drivers/musb-hdrc

# ls -lrth
total 0
--w-------    1 root     root        4.0K Jul 11 10:11 uevent
--w-------    1 root     root        4.0K Jul 11 10:13 unbind
lrwxrwxrwx    1 root     root           0 Jul 11 10:13 musb-hdrc.1.auto -> ../../../../devices/ocp.2/47400000.usb/47401c00.usb/musb-hdrc.1.auto
lrwxrwxrwx    1 root     root           0 Jul 11 10:13 musb-hdrc.0.auto -> ../../../../devices/ocp.2/47400000.usb/47401400.usb/musb-hdrc.0.auto
lrwxrwxrwx    1 root     root           0 Jul 11 10:13 module -> ../../../../module/musb_hdrc
--w-------    1 root …
Run Code Online (Sandbox Code Playgroud)

linux embedded arm linux-device-driver linux-kernel

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

是否有相当于Java <?在C++中扩展ClassName>?

我正在查看这个https://docs.oracle.com/javase/tutorial/java/generics/subtyping.htmlhttps://docs.oracle.com/javase/tutorial/java/generics/inheritance.html并询问我自己如何在C++中实现它.

我有这个小例子来说明:

#include <iostream>

class Animal
{
public:
    virtual std::string type() const = 0;
    virtual ~Animal() {}
};

class Dog : public Animal
{
public:
    virtual std::string type() const {
        return "I am a dog";
    }
};

class Cat : public Animal
{
public:
    virtual std::string type() const {
        return "I am a cat";
    }
};

template <typename T>
class AnimalFarm
{
};

void farmTest(const AnimalFarm<Animal *> &farm)
{        
    std::cout << "test farm";
}


int main(int …
Run Code Online (Sandbox Code Playgroud)

c++ java inheritance templates

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

为什么按值传递 QStringView 比引用 const 更快?

我在Qt 的 Doc 中找到了这个:

QStringViews 应该通过值传递,而不是通过引用到常量:

他们给出了以下示例:

void myfun1(QStringView sv);        // preferred
void myfun2(const QStringView &sv); // compiles and works, but slower
Run Code Online (Sandbox Code Playgroud)

这怎么可能?

c++ qt

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

将信号连接到基类构造函数中的纯虚拟槽是否安全?

我问自己以下代码是否安全:

#include <QCoreApplication>
#include <QObject>
#include <QDebug>
#include <QTimer>

class Base : public QObject
{
    Q_OBJECT

public:
    Base()
    {
        // is it safe to do that ?
        connect(this, SIGNAL(signal1()), this, SLOT(slot1()));
    }
    virtual ~Base() {}

signals:
    void signal1();

public slots:
    virtual void slot1() = 0; // could be only virtual
};

class Derived : public Base
{
    Q_OBJECT

public slots:
    virtual void slot1()
    {
        qDebug() << "derived slot";
    }

    void emitSignal1()
    {
        emit signal1();
    }
};

int main(int argc, char …
Run Code Online (Sandbox Code Playgroud)

c++ qt qt5 qt-slot

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

C中的函数指针和内存地址

在以下程序中&foo*foofoo指向相同的内存地址:

#include <stdio.h>

int foo(int arg)
{
    printf("arg = %d\n", arg);
    return arg;
}

int main()
{
    foo(0); // ok
    (*foo)(0); // ok
    (&foo)(0); // ok

    printf("&foo = %lx\n", (size_t)(&foo));
    printf("foo = %lx\n", (size_t)(foo));
    printf("*foo = %lx\n", (size_t)(*foo));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出为:

arg = 0
arg = 0
arg = 0
&foo = 55eeef54c720
foo = 55eeef54c720
*foo = 55eeef54c720
Run Code Online (Sandbox Code Playgroud)

有人可以解释吗?谢谢。

c linux gcc function-pointers

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

Linux 上的 C++ 插件 ABI 问题

我正在开发一个插件系统来替换共享库。

在为共享库和库中的入口点设计 API 时,我知道 ABI 问题,例如导出的类,应该仔细设计。

例如,添加、删除或重新排序导出类的私有成员变量可能会导致不同的内存布局和运行时错误(根据我的理解,这就是 Pimpl 模式可能有用的原因)。当然,在修改导出的类时,还有许多其他的陷阱需要避免。

我在这里建立了一个小例子来说明我的问题。

首先,我为插件开发人员提供以下标头:

// character.h
#ifndef CHARACTER_H
#define CHARACTER_H

#include <iostream>

class Character
{
public:
    virtual std::string name() = 0;
    virtual ~Character() = 0;
};

inline Character::~Character() {}

#endif
Run Code Online (Sandbox Code Playgroud)

然后将插件构建为共享库“ libcharacter.so”:

#include "character.h"
#include <iostream>

class Wizard : public Character
{
public:
    virtual std::string name() {
        return "wizard";
    }
};

extern "C"
{
    Wizard *createCharacter()
    {
        return new Wizard;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后是使用插件的主应用程序:

#include "character.h"
#include <iostream>
#include <dlfcn.h>

int main(int argc, …
Run Code Online (Sandbox Code Playgroud)

c++ linux plugins gcc abi

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

C++协方差返回类型有哪些缺点?

我最近不得不处理C++ 协方差返回类型,例如以下构造:

struct Base
{
     virtual ~Base();
};
struct Derived : public Base {};

struct AbstractFactory
{
    virtual Base *create() = 0;
    virtual ~AbstractFactory();
};

struct ConcreteFactory : public AbstractFactory
{
    virtual Derived *create()
    {
        return new Derived;
    }
};
Run Code Online (Sandbox Code Playgroud)

它允许客户端代码在需要时将Derived对象视为Base类型或Derived类型,特别是不使用dynamic_caststatic_cast.

这种方法的缺点是什么?这是糟糕设计的标志吗?

谢谢.

c++ covariance

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

如何将 Linux 程序的 UDP 套接字接收缓冲区大小增加到最大?

我正在尝试增加 UDP 套接字的接收缓冲区大小,但最终大小似乎不可预测:

    LOG_INFO("UDP echo server default receive buffer size : " << rcv_buf << " bytes");

    // increase default buffer sizes
    rcv_buf *= 3;

    LOG_INFO("trying to increase receive buffer size to : " << rcv_buf << " bytes");

    if (!SockWrap::set_recv_buf_size(m_handle, sizeof(m_sockaddr_in), rcv_buf))
        LOG_ERR("unable to set new receive buffer size");

    // checking the new size after possible modifications if any
    rcv_buf = SockWrap::recv_buf(m_handle, sizeof(m_sockaddr_in));

    if (rcv_buf == -1) {
        LOG_ERR("unable to read UDP echo server receive buffer size after modification"); …
Run Code Online (Sandbox Code Playgroud)

c++ sockets linux networking udp

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