标签: allocation

如何确定 Rust 中的 new() 何时在堆栈或堆上分配

有没有办法(除了查看实现和文档)来确定 rust 中的 new() 何时在堆栈或堆上分配?

理想情况下,我正在寻找一种方法来在编写源代码时一目了然地确定这一点。例如,可以添加限制是否可以进行堆分配的特征或参数。

memory stack allocation heap-memory rust

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

创建一个 `Pin<Box<[T; N]>>` 在 Rust 中,当 `[T; N]`太大,无法在堆栈上创建

广义问题

如何pinned_array_of_default在稳定的 Rust 中实现一个[T; N]太大而无法放入堆栈的通用函数?

fn pinned_array_of_default<T: Default, const N: usize>() -> Pin<Box<[T; N]>> {
    unimplemented!()
}
Run Code Online (Sandbox Code Playgroud)

或者,如果这样可以使过程更容易,则T可以实施。Copy

fn pinned_array_of_element<T: Copy, const N: usize>(x: T) -> Pin<Box<[T; N]>> {
    unimplemented!()
}
Run Code Online (Sandbox Code Playgroud)

将解决方案保存在安全的 Rust 中会更好,但似乎不太可能。

方法

最初,我希望通过实现,Default我也许能够Default处理初始分配,但是它仍然在堆栈上创建它,因此这对于较大的 值不起作用N

let boxed: Box<[T; N]> = Box::default();
let foo = Pin::new(boxed);
Run Code Online (Sandbox Code Playgroud)

我怀疑我需要使用它MaybeUninit来实现这一点,并且有一个Box::new_uninit()函数,但它目前不稳定,我理想地希望将其保留在稳定的 Rust 中。我也有点不确定转变为Pin<Box<MaybeUninit<B>>>是否Pin<Box<B>>会对Pin.

背景

使用 a 的目的Pin<Box<[T; N]>>是保存一个指针块,其中 …

allocation rust maybeuninit

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

既然有了虚拟内存,为什么还需要内存保护呢?

通过内存保护,我的意思是以下程序将在许多机器上抛出运行时异常:

#include <iostream>

int main() {
    int* my_int = new int[12];
    std::cout << my_int[20000];
    delete[] my_int;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序给出以下错误:

Exception thrown at 0x00007FF7A467101A in myprogram.exe: 0xC0000005: Access violation reading location 0x000002794CA635C0.
Run Code Online (Sandbox Code Playgroud)

由于每个进程都有自己的虚拟内存,因此其他程序已经受到保护,免受我的代码中的访问冲突的影响。在我看来,正确的程序会付出运行时成本,因为不正确的程序可能会访问未分配的内存。

为什么计算机在调试模式之外还要费力地防止访问违规?

编辑:这个问题的一个很好的初步答案是“这些检查通常是在硬件中完成的”。接下来的问题是“如果不需要在访问冲突上引发异常,是否可以制造出更快的硬件? ”由于 CPU 硬件优化的大部分内容都与空间有关,我认为答案是“肯定是的,但不是”足够值得了。” 我们在硬件上为访问违规检查支付了多少成本?

c++ memory-management allocation virtual-memory access-violation

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

我可以在没有赋值运算符的类型的向量中可靠地 emplace_back 吗?

我在 GCC、Clang 和 MSVC 中做了一些测试,发现emplace_back永远不会在包含的类上调用赋值运算符。它仅在重新分配时调用复制或移动构造函数。标准是否以某种方式保证这种行为?

用例是我有一些类按顺序存储在一个数字中,该数字只会随着时间的推移而增长,直到整个向量被破坏。我很乐意从赋值运算符中清除我的代码。

c++ allocation vector move-constructor move-assignment-operator

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

这是在 c 中释放内存的可接受的方法吗?

我有一个函数,它读取文件并为文件内容分配内存,并将文件内容分配给指针,然后返回指针。然后,我使用循环来遍历字符串并使用指针算术打印每个字符。

我很确定我可以/应该使用 realloc 在每次迭代中重新分配更少的内存,而不是使用计数器跟踪迭代,但我不确定如何实现它。

因此,在代码末尾,当我调用时,free()我从指针变量中减去计数器,以释放contents指针最初指向的地址。

下面是我用来读取文件的代码以及循环所在的主函数:

char *read_file(const char *filename) {
    FILE *fp = fopen(filename, "r");
    if (fp == NULL) {
        perror("Failed to open file");
        exit(EXIT_FAILURE);
    }

    // Obtain information about the file
    struct stat st;
    if (fstat(fileno(fp), &st) != 0) {
        perror("Failed to get file information");
        exit(EXIT_FAILURE);
    }
    size_t file_size = st.st_size;

    // Allocate a buffer to hold the contents of the file
    char *buffer = (char *) malloc(file_size + 1);
    if (buffer == NULL) …
Run Code Online (Sandbox Code Playgroud)

c memory malloc allocation realloc

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

C++:"新"这个将继续增长的系列意味着什么?

我是C++的新手.对于"新"系列来说,它究竟意味着什么?例如:

UnicodeStringList* tmp = new UnicodeStringList;
// where UnicodeStringList is typedef to std::list<UnicodeString>
Run Code Online (Sandbox Code Playgroud)

当你"新"某事时,你必须知道你需要它有多大,对吧?因此,当我使用赋值构造函数来复制对象时,计算机将如何知道应该在堆上分配多少内存?例如:

*tmp = another_string_list;
Run Code Online (Sandbox Code Playgroud)

另一个sun_string_list被复制到堆内存中新的UnicodeStringList中,但我最初从未指定堆内存应该有多大.并且编译器不知道another_string_list有多大,所以有多少内存进入堆?

我很困惑,希望我已经说明了我的问题,所以有人可能会理解我,但我不确定.

请帮忙

谢谢,

朱利安

c++ heap allocation new-operator

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

尝试分配通用数组时,C#程序崩溃

请考虑以下代码:

    public class LimitedBuffer<T> {

    /// <summary>
    /// 
    /// </summary>
    public const int DefaultSize = 10;

    /// <summary>
    /// 
    /// </summary>
    private T[] buffer;

    /// <summary>
    /// 
    /// </summary>
    private int poe;

    /// <summary>
    /// 
    /// </summary>
    private int pow;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="size"></param>
    public LimitedBuffer(int size = 10) {
        // Buffer instantiation
        this.buffer = new T[10];
        // Value initialization
        for (int i = 0; i < this.buffer.Length; i++)
            this.buffer[i] = default(T);
        this.pow = 0;
        this.poe …
Run Code Online (Sandbox Code Playgroud)

.net c# stack-overflow arrays allocation

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

在动态分配的指针数组中悬空指针

这是一个悬垂的指针吗?

int x = 25;
int** arr = new int*[5];
*arr[1] = x;
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚为什么这不起作用...*arr [1]只是一个指针,并指向一个有效的内存地址.

c++ memory arrays pointers allocation

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

用于在C中填充动态分配的数组的指针算法

我使用了一个数组,malloc然后尝试使用for循环和指针算法来填充它,但是由于某些原因它不起作用.

int* myArray = (int*)malloc(100*sizeof(int));

for (int i = 0; i < 100 ; i++)
{
    *myArray = i;
    myArray++;      
}
Run Code Online (Sandbox Code Playgroud)

c malloc pointers allocation dynamic

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

C#字节数组未正确分配

奇怪问题的截图

我正在尝试在C#中序列化一个对象.我得到了对象大小并将其保存在上面屏幕截图中207行的变量size1中.Size1的值为160.然后我使用size1在第210行分配一个名为buf的字节数组.Buff出来是一个2字节的数组!怎么会这样?!

c# serialization allocation

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