小编gla*_*des的帖子

alloca() 在内存级别如何工作?

我试图弄清楚alloca()在记忆层面上实际上是如何工作的。来自Linux 手册页

alloca() 函数在调用者的堆栈帧中分配 size 字节的空间。当调用 alloca() 的函数返回到其调用者时,该临时空间会自动释放。

这是否意味着将按字节alloca()转发堆栈指针n?或者说新创建的内存到底分配在哪里?

这不是与可变长度数组完全相同吗?

我知道实现细节可能留给操作系统之类的东西。但我想知道一般来说这是如何实现的。

c alloca stack-frame variable-length-array

45
推荐指数
3
解决办法
4193
查看次数

为什么允许 std::initializer_list 不指定大小并同时分配堆栈?

我从这里了解到不需要std::initializer_list分配堆内存。这对我来说很奇怪,因为您可以在不指定大小的情况下获取std::initializer_list对象,而对于数组,您始终需要指定大小。尽管初始化器列表在内部几乎与数组相同(正如帖子所暗示的那样)。

我很难理解的是,C++ 作为静态类型语言,每个对象的内存布局(和大小)必须在编译时固定。因此,每个std::array都是另一种类型,我们只是从通用模板中生成这些类型。但对于std::initializer_list,此规则显然不适用,因为接收函数或构造函数不需要考虑内存布局(虽然它可以从传递给其构造函数的参数派生)。仅当类型堆分配内存并且仅保留存储来管理该内存时,这对我才有意义。那么差异就很像std::arrayand std::vector,对于后者,您也不需要指定大小。

std::initializer_list不使用堆分配,正如我的测试所示:

#include <string>
#include <iostream>

void* operator new(size_t size)
{
    std::cout << "new overload called" << std::endl;    
    return malloc(size);
}


template <typename T>
void foo(std::initializer_list<T> args)
{
    for (auto&& a : args)
    std::cout << a << std::endl;
}

int main()
{
    foo({2, 3, 2, 6, 7});

    // std::string test_alloc = "some string longer than std::string SSO";
} …
Run Code Online (Sandbox Code Playgroud)

c++ types heap-memory stack-memory stdinitializerlist

19
推荐指数
3
解决办法
1966
查看次数

int 到 double 的转换是否会向上舍入、向下舍入或到最接近的双精度?

简单的问题:从 int(例如 100)转换为双精度数“向上或向下舍入”到下一个双精度数,还是总是舍入到最接近的一个(最小增量)?

例如static_cast<double>(100)

从 int 转换为 double

如果 d2 < d1 会向哪个方向投射?

额外问题:我可以以某种方式强制处理器使用特殊函数向下或向上“舍入”到双精度吗?据我所知,没有floor<double>或者ceil<double>不幸的是。

c++ floating-point rounding static-cast floating-point-conversion

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

Sphinx Read-The-Docs 主题已识别但未“找到”

我从 Github 下载了 sphinx 的 read-the-docs 主题并将其放入 _themes 文件夹中。

\n

conf.py:

\n
html_theme = "sphinx_rtd_theme"\nhtml_theme_path = ["_themes", ]\n
Run Code Online (Sandbox Code Playgroud)\n

但是,运行“make html”时出现此错误:

\n
Sphinx v4.1.2 in Verwendung\nLade \xc3\x9cbersetzungen [de]\xe2\x80\xa6erledigt\nloading pickled environment... erledigt\nWARNING: sphinx_rtd_theme (< 0.3.0) found. It will not be available since Sphinx-6.0\n\nTheme error:\nno theme named 'sphinx_rtd_theme' found (missing theme.conf?)\n
Run Code Online (Sandbox Code Playgroud)\n

不知何故,斯芬克斯似乎找到了主题,然后又忽略了它。有人可以告诉我发生了什么事以及如何解决它吗?

\n

themes python-sphinx read-the-docs

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

“模板化右值引用”而不是通用引用作为参数?

我有一个类对象,它采用仅限于某个概念的通用参数。现在我想将此参数(哪种类型仍然未知)转发到construct适合此引用类型的重载:根据转发的参数选择 rvalue-ref 重载或 const ref 重载。我怎样才能做到这一点?

问题是,一旦我输入双与号,“模板化右值引用”就成为通用引用,我看不到一种方法来规避它。下面的示例显示,在这两种情况下,都会选择“贪婪”通用引用重载,即使我希望第一个实例化与 const ref 一起使用。

演示

#include <concepts>
#include <cstdio>
#include <utility>

template <typename... Args>
struct function
{
    template <std::invocable<Args...> Cb>
    function(Cb&& fn) {
        construct(std::forward<Cb>(fn));
    }

    template<typename Cb>
    auto construct(const Cb&) {
        printf("Overload for const-ref called!\n");
    }

    template <typename Cb>
    auto construct(Cb&&) {
        printf("Overload for rvalue-ref called!\n");
    }
};

struct functor
{
    auto operator()()
    {
        printf("Functor called!\n");
    }
};

int main()
{
    functor foo1;

    function myfunc{foo1};
    function myfunc2{functor{}};
}
Run Code Online (Sandbox Code Playgroud)

输出:

Overload for rvalue-ref …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading forwarding-reference

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

将其他 *.rst 文件中的整个文本部分包含到此文件中?

我正在使用 Sphinx,我想自动将给定部分从一个 *.rst 文件“复制”到多个其他文件中,这样当我调整第一个文件中的文本部分时,更改会自动反映在复制的部分中其他文件。

例如:

基础.rst :

This is a section/paragraph I want to see in other *.rst - files.
Run Code Online (Sandbox Code Playgroud)

导数.rst

Here is some text. But the following paragraph should be the paragraph from above ^^:

<that paragraph from above>

Here the file continues.
Run Code Online (Sandbox Code Playgroud)

如何使用 Sphinx 和 reStructuredText 做到这一点?

restructuredtext sections python-sphinx

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

致命:./config 中“receive.denycurrentbranch”的错误配置值

我使用 git 将提交的更改推送到位于服务器上的存储库。现在有这个错误:

$ git push -u origin master
fatal: bad config value for 'receive.denycurrentbranch' in ./config
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Run Code Online (Sandbox Code Playgroud)

在控制台窗口中的样子

现在我想这个错误可能是由于 SSH 连接中的一些失败造成的。我使用 PuTTY 通过 SSH 连接到我的网络服务器。但是,我仍然是一个菜鸟,我不知道这是如何工作的。

所以我所尝试的是我在线生成私钥,下载它,将其翻译成 PPK 格式并将其放入 PuTTY 的身份验证选项卡中。当我通过 PuTTY 登录时,我必须输入密钥密码,然后才能成功连接。

错误仅在 git bash 中。那么我该如何修复它呢?不知何故需要告诉 Git 使用 PuTTY 作为我的 SSH 代理(什么是 SSH 代理?)。或者 Git 默认使用 Windows 10 中的 OpenSSH?

我也尝试使用

$ eval $(ssh-agent -s)
$ ssh-add ~/.ssh/id_rsa
Run Code Online (Sandbox Code Playgroud)

但我不完全知道它的作用。它只是启动 OpenSSH-Agent …

git git-config

6
推荐指数
3
解决办法
3275
查看次数

if 分支比 else 分支快吗?

我发现了这个非常漂亮的信息图,它对某些操作所使用的 CPU 周期进行了粗略估计。在学习时,我注意到一个条目“if的右分支”,我认为如果满足条件,该分支将采用“if”(编辑:正如评论中指出的“右”实际上意味着“正确预测的分支” )。这让我想知道 if 分支与 else 分支相比是否存在任何(即使是很小的)速度差异。

例如,比较以下非常简洁的代码:

演示

#include <cstdio>

volatile int a = 2;

int main()
{
    if (a > 5) {
        printf("a > 5!");
        a = 2;
    } else {
        printf("a <= 5!");
        a = 3;
    }
}
Run Code Online (Sandbox Code Playgroud)

它在 x86 64 位中生成此程序集:

.LC0:
        .string "a > 5!"
.LC1:
        .string "a <= 5!"
main:
        push    rcx
        mov     eax, DWORD PTR a[rip]
        cmp     eax, 5
        jle     .L2
        mov     edi, OFFSET FLAT:.LC0
        xor     eax, eax …
Run Code Online (Sandbox Code Playgroud)

c++ performance x86 assembly branch-prediction

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

即使 .cpanel.yml 文件存在,cPanel 也不会部署

我的存储库是通过网络托管商上的 cPanel 进行管理的。根据各种文档,您需要在存储库的顶部目录中有一个 .cpanel.yml 文件,以将文件部署到生产目录中。但这不起作用。

以下是 .cpanel.yml 的内容(顺便说一句,位于存储库的顶级目录中):

---
deployment:
      tasks:
        - export DEPLOYPATH=/home/glades/public_html
        - /bin/cp -r * $DEPLOYPATH
Run Code Online (Sandbox Code Playgroud)

而“glades”是我的用户名。据我所知,它应该将存储库中的所有文件部署到生产目录中,在本例中为 public_html 。但是当我用文件管理器查看 public_html 文件夹时,找不到任何文件。

文件管理器显示 public_html 文件夹如下

应该有 README.md 和 index.php 文件。但是index.php没有被改变(它是旧的)并且README.md甚至不存在。

现在,当我进入 cPanels“Git Version Control”的“Pull or Deploy”选项卡时,会出现此错误消息。但我可以向您保证,有一个有效的 .cpanel.yml 文件(因为当我删除本地存储库并再次克隆它时,主目录中会下载一个 .cpanel.yml 文件)。另外,签出的分支是主分支,我是目前唯一在处理它的分支,所以它应该是最新的。我不太明白。有人看到我哪里出错了吗?赞赏!

git deployment web-hosting push cpanel

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

调整 std::string 大小而不初始化内存

我正在使用 C 例程写入 std::string 的数据字段。这是一个人为的示例,实际上我通过网络获取一个非空终止的字符串及其大小:

#include <string>
#include <cstdio>
#include <cstring>

const char* str = "Some examplory string.."; 

int main()
{
    std::string some_str;
    some_str.resize(strlen(str));
    std::strcpy(&some_str.front(), str);
    printf(some_str.c_str());
}
Run Code Online (Sandbox Code Playgroud)

现在根据cppref 的说法,不存在resize()仅调整大小且不使用 '\0' 或其他内容初始化内存的重载。但如果我所做的只是再次覆盖它,为什么我要为此付费呢?这可以通过某种方式避免吗?

c++ stdstring c++20

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