标签: realloc

realloc仅以2的幂增加分配

当我在我的iOS应用程序中使用realloc时,它似乎总是以2的幂而不是我指定的方式增加应用程序中的内存.

当我根据其存储的对象的大小检查分配的大小时,这会导致一些问题.

这是realloc的设计方式还是我做错了什么?

typedef struct {
    GLfloat r;
    GLfloat g;
    GLfloat b;
    GLfloat a;
} Color;

typedef struct {
    int objectID;
    int modelID;
    GLfloat scale;
    GLfloat translation[3];
    GLfloat rotation[3];
    Color color;
    bool render;
} Object;

Object* objects;

objects = realloc(objects, malloc_size(objects) + sizeof(Object));

NSLog(@"objects size = %lu", malloc_size(objects)); //prints 64 instead of 56
Run Code Online (Sandbox Code Playgroud)

objective-c realloc ios

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

如果我使用"realloc()",为什么Valgrind会产生"无效免费或删除"?

使用realloc()时,我用valgrind检查了它,如下所示

 valgrind --tool=memcheck --leak-check=yes --show-reachable=yes a.out
Run Code Online (Sandbox Code Playgroud)

而valgrind产生的错误信息是

==6402== Memcheck, a memory error detector.
==6402== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
==6402== Using LibVEX rev 1575, a library for dynamic binary translation.
==6402== Copyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP.
==6402== Using valgrind-3.1.1, a dynamic binary instrumentation framework.
==6402== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
==6402== For more details, rerun with: -v
==6402==
dinesh
vignesh
==6402== Invalid free() / …
Run Code Online (Sandbox Code Playgroud)

c valgrind realloc

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

asprintf覆盖realloc的内存

我有以下代码,使用asprintf和时都不起作用realloc.

我得到的错误是:

*** glibc detected *** a.out: realloc(): invalid old size: 0x006f1430 ***
Run Code Online (Sandbox Code Playgroud)

基于我所研究的内容,当我使用asprintf它时,它会覆盖一些realloc使用的内存.这对我来说没有意义,因为asprintf它应该是安全的并且使用适当的字符串长度动态分配.不使用asprintf导致程序运行正常,但我需要asprintf我的项目的功能.

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

int main() {
  int ifCount = 1;
  int stringCount = 1;
  char** IFs = NULL;

  //Broken code
  char* message;
  asprintf(&message, "Hello: %d", stringCount);

  //Working code, but not the alternative I want to take
  //char* message = "Hello";

  IFs = (char**) realloc(IFs, sizeof(char*) * ifCount);
  IFs[ifCount - …
Run Code Online (Sandbox Code Playgroud)

c printf realloc

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

Realloc功能无法正常工作?

为什么以下代码输出4次,而不是8次和20次?谢谢

int size = 0;
int *pointer;
pointer = malloc(2 * sizeof(int));
size = sizeof(pointer);
printf("%d", size);
int *temp = realloc(pointer, 5 * sizeof(int));
if (temp != NULL) //realloc was successful
{
    pointer = temp;
} else //there was an error
{
    free(pointer);
    printf("Error allocating memory!\n");
}
size = sizeof(pointer);
printf("%d", size);
Run Code Online (Sandbox Code Playgroud)

c memory memory-management sizeof realloc

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

c realloc():下一个大小无效

我得到这个错误,在线搜索没有解决它,这是我的代码^^:

void addSoggetto(char* s)
{
    soggetti_length++;
    if(realloc(soggetti, soggetti_length*sizeof(int))==NULL) 
    {
        printf("Realloc Failed");
        return;
    }
Run Code Online (Sandbox Code Playgroud)

基本上我有一个指针数组(soggetti)和它的长度(soggetti_length).每次运行此函数时,我都会重新分配大小以便为另一个指针设置位置.问题他,正是第五次调用该函数,我得到:

realloc(): invalid next size
Run Code Online (Sandbox Code Playgroud)

你知道我该怎么办?我想我可以排除我realloc的内存不是enaugh,我依旧增加它并且没有任何变化.哦,我用gdb调试它,该函数在返回之前崩溃,所以我甚至得不到像NULL返回的东西

c size null pointers realloc

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

realloc():下一个大小无效:后跟32位寄存器

所以我一直在用C编写一个mtf编码器,无论我做什么,我都遇到了realloc()错误.我已经通过使用print语句查看我的逻辑中是否存在错误(可能存在错误),看看我是否超出了当前malloc'd数组的范围(添加了超出原始数组大小的字符串)这似乎不是问题.我使用过GDB和Valgrind,而GDB给我一个神秘的信息,而Valgrind遇到了一个分段错误.这是我第一次使用动态内存,我对这个问题很困惑,下面是我的代码以及GDB错误:

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

int count = 0;


move_to_front(int index, char** words){
    int i;
    char *t = words[index];

    for(i = index; i>1; i--){
       words[i] = words[i-1];
    }

    words[1] = t;

}


char** reallocate_words(char** words, int* words_size_pointer){
   printf("We're entering here\n");
   printf("%d", *words_size_pointer);
   int temp = *words_size_pointer;
   char** tempor;
   temp = temp*2;
   printf("%d", temp);
   tempor = (char**) realloc(words, temp);
   int i = *words_size_pointer;
   for(i; i<temp; i++){
       tempor[i] = (char*) malloc(120);
    }
   words_size_pointer = &temp;
   return tempor;

 } …
Run Code Online (Sandbox Code Playgroud)

c malloc realloc dynamic-memory-allocation

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

为什么不重新分配工作

我在一个更大的项目中遇到了这个问题,其中由于某种原因,该realloc函数绝对没有任何作用.我错过了一些明显的东西吗

这是一个简化的例子:

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

int main()
{
    int x = 1, y = 2, z = 3;
    int* arr, *arr1;
    int** arra;

    arra = (int**)malloc(sizeof(int*));
    arr = (int*)malloc(sizeof(int));
    arr[0] = x;
    arra[0] = arr;
    arra = (int*)realloc(arra, sizeof(int*) + sizeof(int*));
    arr1 = (int*)malloc(sizeof(int));
    arr1[0] = y;
    arra[1] = arr1;

}
Run Code Online (Sandbox Code Playgroud)

当我调试时,最后的arra结果是{{1}},即使我的理解应该是{{1},{2}}.

c arrays realloc

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

为什么realloc在这个while循环中不起作用?

我很想知道为什么realloc()在我的循环中不起作用.我做了一个grep函数,我在一个大文本文件上测试,突然程序崩溃告诉我"堆的腐败"所以我决定分解它并尝试它规模较小,但问题仍然存在.可以解释一下有什么问题吗?

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

void grep(const char *Pattern,FILE *file);

int main(void)
{
    FILE *file;
    if(fopen_s(&file,"file.txt","r"))
        return 1;
    grep("word",file);
    fclose(file);
    return 0;
}

void grep(const char *Pattern,FILE *file)
{
    size_t size = 5*sizeof(char);
    char *_Buf = (char*)malloc(size);
    int n = 0, c;
    while(c=getc(file))
    {
        _Buf[n++] = c;
        if(c == '\n' || c == EOF)
        {
            _Buf[n] = '\0';
            if(strstr(_Buf,Pattern))
                printf("%s",_Buf);
            if(c == EOF)
                break;
            n = 0;
        }
        if(n == size)
        { …
Run Code Online (Sandbox Code Playgroud)

c pointers loops realloc dynamic-memory-allocation

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

当我将缓冲区重新分配为较小时,是否会删除缓冲区中的数据,直到达到正确的大小?

我的问题有点令人困惑,但我要问的是,realloc当它最小化缓冲区时,剩余数据是如何处理的?例如,我只想说我有一个填充50字节的缓冲区(假设我的缓冲区最多可以容纳50个字节的数据).稍后在我的代码中,我调整缓冲区的大小realloc,现在只能容纳30个字节.什么是realloc与左超过20个字节呢?

c realloc

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

realloc():无效指针已中止(核心转储)

我正在创建一个类似于bash的shell.尝试获取与命令关联的参数(即ls -l)时,我遇到了错误.我在网上搜索过,没有找到任何有用的东西.

char *userInCopy;
char *ret;
userInCopy = (char*)calloc(1024, sizeof(char));
ret = (char*)calloc(64, sizeof(char));
strcpy(userInCopy, userIn);
int i = 0;
while((ret = strsep(&userInCopy, " "))){
    *(args + i) = (char*)calloc(strlen(ret), sizeof(char));
    strcpy((*(args+i)), ret);
    i++;
    ret = (char*) realloc(ret, (64)* sizeof(char));
} 
Run Code Online (Sandbox Code Playgroud)

我从char userIn中获取用户的输入并正确分配.然后我使用strsep单独获取每个参数来解析输入.我可以得到ls参数和-l参数,但是当它在-l之后转到realloc时它会给我"realloc():无效指针"错误.我迷失了为什么这会在获得ls参数后第一次工作但在获得-l参数后失败.有什么建议?

c memory coredump realloc

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