realloc

    0热度

    2回答

    我写了一个小片的这样的代码: template <class T> void test() { T* ptr = nullptr; ptr = (T*)malloc(1 * sizeof(T)); new ((void*)ptr) T(T()); ptr = (T*)realloc(ptr, 2 * sizeof(T)); new (

    3热度

    1回答

    假设我正在使用std::allocator编写自定义矢量来包装new和delete。 当元素的数量超过向量的容量时,我想重新分配缓冲区到更大的值。我可以通过拨打realloc()轻松实现。我不想这样做,因为我认为分配/释放的责任应该驻留在分配器中。 然而,看std::allocator的界面,我不明白我怎么可以做一个重新分配。只有对的方法: T* allocate(std::size_t n);

    1热度

    1回答

    我正在尝试通过使用realloc来动态调整数组的大小。这个数组在我的函数之外使用malloc初始化。 这里是我的功能: size_t verarbeite_anlagendatei(ANLAGE *anlage_arr) { FILE *fp; ANLAGE anlage; fp = fopen("anlagen.dat", "r"); if(fp =

    1热度

    1回答

    我需要帮助完成学校任务,特别是调整分配给没有realloc的指针的内存量。 我在我的程序中有以下声明。 struct GraphicElement { enum{ SIZE = 256 }; unsigned int numLines; Line* pLines; char name[SIZE]; }; typedef struct {

    0热度

    1回答

    我真的需要一些内存泄漏的帮助。我一直盯着我的代码一段时间,仍然无法找出那些泄漏。我想我可以解放所有东西,甚至有时候是不必要的,但是还有一些东西。 代码 #include <stdio.h> #include <stdlib.h> struct Matrix{ int **data; int rows; int columns; }; struct Matr

    2热度

    1回答

    我有几个关于理解 realloc行为的问题。 #include <stdio.h> #include <stdlib.h> int main() { int *str; /* Initial memory allocation */ str = malloc(5 * sizeof(int)); *str = 1; *(str + 1) =

    1热度

    2回答

    我想写一个函数来改变动态数组的大小,并允许用户一次填充它。我知道,我应该使用的“realloc的”做(所以我做,所以它的工作原理...),但我第一次尝试是这样的: void ChangeDynamicArraySize(int* dArray, int oldSize, int newSize){ int* tempArray = (int*) malloc(sizeof(int) *

    -3热度

    6回答

    据我所关注的,如果realloc失败,我们失去设置缓冲区(指针)的信息和对reallocNULL 去考虑下面的程序: #include<stdio.h> #include<stdlib.h> #include<string.h> int main(void){ char *ptr = malloc(256); if (!ptr){ printf("Erro

    0热度

    1回答

    我目前正试图了解动态内存分配如何工作。 我有这样的代码: int main() { int **mat, i; mat = calloc(3, sizeof(int*)); for(i = 0; i < 3; i++) mat[i] = calloc(3, sizeof(int)); mat = realloc(mat, 1*sizeof(

    1热度

    3回答

    假设我有一个指向结构s1的数组(在堆上)的指针p1,其中每个结构s1也有一个指向另一个结构s2(在堆上)的指针。如果我在p1上调用realloc()来调整它的大小,那么将释放pre-realloced数组的结构中的旧内存(堆中的s2)? 我相当肯定的答案,这个问题是没有,因为文档指出if the area pointed to was moved, a free(ptr) is done这意味着它