标签: realloc

使用realloc调整缓冲区大小

如果指向的区域被移动,则完成自由(ptr).

你能解释一下上面的一行realloc()吗?这一行来自calloc,malloc,realloc和free的手册页.

c realloc

4
推荐指数
2
解决办法
2899
查看次数

可以realloc缩小左侧的数组(仅限C)吗?

我想移动我记忆中的一大块数据.不幸的是,这些数据被保存为数组,我无法改变它.我不能使用循环数组,因为我不想改变的一些fortran方法也使用相同的内存.最重要的是,在运动之间非常频繁地访问阵列.所以我可以这样做:

int *array = (int*) malloc(sizeof(int)*5);
int *array2=NULL;
//Now i want to move my data one step to the left
array=(int*) realloc(array,6);
array2=array+1;
memmove(array,array2,5*sizeof(int));
array=(int*) realloc(array,5);
Run Code Online (Sandbox Code Playgroud)

这应该工作正常,但它看起来很浪费;).如果我可以告诉我的编译器拿走缩小数组左侧的数据,我的数据会在内存中蔓延,但我不需要进行任何复制.像这样:

int *array = (int*) malloc(sizeof(int)*5);
//Now i want to move my data one step to the left
array=(int*) realloc(array,6);
array=(int*) realloc_using_right_part_of_the_array(array,5);
Run Code Online (Sandbox Code Playgroud)

所以基本上我想用指针完成,array+1剩下的4个字节被释放.我打得四处free()malloc(),但它没有工作......我知道,realloc的还可能会导致的memcpy调用,但不是每次!所以它可能会更快,不是吗?

c realloc shrink

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

从被调用者的角度来看,如何检测内存是动态的还是静态的?

注意:当我在这里说"静态字符串"时,我指的是realloc无法处理的内存.

嗨,我已经编写了一个带有char*参数的过程,我想创建一个副本,因为内存不能通过realloc重定位/调整大小.因此,该过程是一个"重型"字符串处理器,因此无论是否无效并且复制字符串,无论它是否是静态的,将来肯定会导致一些内存开销/处理问题.

我曾尝试使用异常处理程序来修改静态字符串,应用程序只是退出而没有任何通知.我退后一步,看看C并说:"我没有留下深刻的印象." 如果我听说过一个,那将是一个例外.

我试图使用异常处理程序在静态变量上调用realloc ... Glib报告说它找不到一些结构的私有信息(我敢肯定)我不知道并且显然在程序上调用abort意味着它不是一个可以用longjmp/setjmp或C++尝试捕获的异常,最后捕获.

我很确定必须有办法合理地做到这一点.例如,动态内存很可能不在静态内存附近的任何位置,所以如果有办法从地址泄露这些信息......我们可能只有一个宾果游戏.

我不确定C/C++预处理器中是否有任何宏可以识别宏参数的来源和类型,但如果没有,那将是非常愚蠢的.Macro Assemblers非常聪明.从缺乏强大的错误处理来判断,如果没有,我不会感到有点惊讶.

c c++ realloc static-memory-allocation

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

结构中的c ++向量

我对使用有疑问std::vector.如果我std::vector在内存中得到一个带有非定义空间的对象的结构 ,如果需要重新分配(std::vector),结构是否也重新分配了?还是只有std::vector

例如:

struct cluster{
    int a;
    int b;
    struct cluster* parent;
    vector<struct cluster> children;
}

struct cluster obj = {2,1,...};

struct cluster *pobj = &obj;

for(int i = 0; i < 100 ; i ++){
    children.push_back(i); //The capacity will be increased progressly, no?
}
Run Code Online (Sandbox Code Playgroud)

所以,问题是:是pobj==&objfor循环结束?或者obj由于子std::vector对象的重新分配而重新分配?

我希望我解释了我的怀疑.谢谢你的时间.

c++ vector realloc

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

这种realloc的使用是否正确?

原始问题

我可以使用realloc()如下代码的函数:

int *ptr, i, num=5;

for (i=0; i<num; i++)
  void *nptr = realloc (ptr, (i+1) * sizeof(int) );
Run Code Online (Sandbox Code Playgroud)

c realloc alloc

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

realloc()如何工作?

假设我已经使用了分配内存malloc(),如果我在我的代码中执行:

char *newline = realloc ( oldline , newsize );
// Assuming oldline is the pointer to which the starting address
// of the memory which malloc() has returned, is assigned and,
// say, newsize is integer having 100 value.
Run Code Online (Sandbox Code Playgroud)

现在我的问题是: -

  1. 是否有必要newline指向与oldline之前的初始地址相同的地址?
  2. 是这样,在这种情况下,oldline将被释放(隐式)并将newline负责内存寻址?
  3. 完成上述代码并完成工作后,我该怎么办才能释放内存

    free(newline);
    
    Run Code Online (Sandbox Code Playgroud)

    要么

    free(oldline);
    
    Run Code Online (Sandbox Code Playgroud)

    或两者?

c realloc

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

关于realloc功能的困惑

我使用参考读了C中的动态内存分配.

那个文件Say's:

realloc()只应用于动态分配的内存.如果未动态分配内存,则行为未定义.

如果我们使用realloc()这样的东西:

int main()
{
    int *ptr;
    int *ptr_new = (int *)realloc(ptr, sizeof(int));

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

根据该引用,该程序未定义,因为指针ptr未动态分配.

但是,如果我使用类似的东西:

int main()
{
    int *ptr = NULL;
    int *ptr_new = (int *)realloc(ptr, sizeof(int));

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

根据该参考,它是否也是未定义的行为?

我的第二种情况不会调用未定义的行为.我对吗?

c realloc language-lawyer

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

内存缩小时的realloc()行为

手册页中realloc()说:

realloc()函数将指向的存储块的大小更改ptrsize字节。从区域开始到新旧大小的最小值之间的内容将保持不变。如果新的大小大于旧的大小,则不会初始化添加的内存。

但是,手册页没有说明如果新大小小于旧大小会发生什么。例如,如果我有以下代码:

ptr = realloc(ptr, nsize); // Where nsize < the original size and ptr is of type void **
Run Code Online (Sandbox Code Playgroud)

如果原始大小为size,是否表示ptr + nsize + 1仍包含分配的条目?

任何帮助表示赞赏。

c realloc dynamic-memory-allocation

4
推荐指数
2
解决办法
162
查看次数

Realloc使用太多内存

我已经做了一个最小的工作示例,说明如何使用realloc向数组添加元素.这将在具有更多元素的未来计划中进行扩展.

#include <stdio.h>//printf
#include <stdlib.h>//malloc, realloc

int main() {
//BEGIN REALLOCATE-ABLE ARRAY
    unsigned int *array, loop_variable;
    const unsigned int ORIGINAL_ARRAY_SIZE = 4, REALLOC_INDICES = 99;
    array = malloc(ORIGINAL_ARRAY_SIZE*sizeof(unsigned int));
    for (loop_variable = 0; loop_variable < ORIGINAL_ARRAY_SIZE; loop_variable++) {
        array[loop_variable] = loop_variable;
    }
//BEGIN REALLOCATION
    for (loop_variable = 1; loop_variable < REALLOC_INDICES; loop_variable++) {
        array = realloc(array,sizeof(unsigned int)*(ORIGINAL_ARRAY_SIZE+loop_variable));
        array[ORIGINAL_ARRAY_SIZE+loop_variable-1] = 2*(ORIGINAL_ARRAY_SIZE+loop_variable-1);
        printf("reallocate array[%d] = %d\n",ORIGINAL_ARRAY_SIZE+loop_variable-1,array[ORIGINAL_ARRAY_SIZE+loop_variable-1]);
    }
//BEGIN PRINTING ARRAY VALUES
    for (loop_variable = 0; loop_variable < ORIGINAL_ARRAY_SIZE+REALLOC_INDICES-1; loop_variable++) {
        printf("array[%d] = …
Run Code Online (Sandbox Code Playgroud)

c memory arrays valgrind realloc

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

realloc 如何处理字符串中的空字节?

这里是相对较新的 C 程序员。我正在审查以下代码,作为我正在练习 C 的业余项目的教程。该abuf结构的要点是创建一个可以附加的字符串。这是代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct abuf {
    char* str;
    unsigned int size;
} abuf;


void abAppend(abuf *ab, const char *s, int len) {
  char *new = realloc(ab->str, ab->size + len);
  if (new == NULL) return;
  memcpy(&new[ab->size], s, len);
  ab->str = new;
  ab->size += len;
}

int main(void) {
    abuf ab = {
        NULL,
        0
    };

    char *s = "Hello";

    abAppend(&ab, s, 5);
    abAppend(&ab, ", world", 7);

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

一切都编译完毕,我的测试(为简单起见进行了编辑)显示字符串“Hello”存储在 …

c string null-terminated realloc

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