无效的realloc老大小 [英] realloc invalid old size

查看:364
本文介绍了无效的realloc老大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

声明:本网站的功课。我试图它,不要指望或希望任何人做对我来说。就在几个指针(嘿嘿)我要去哪里错了是AP preciated。

该作业要求我创建一个持有10个元素为int * 数组,然后试图插入亿整数进去。如果阵列需要调整大小每个插入检查,并且如果这样做,我增加它的大小,以便它可以容纳多一个元素

当我插入10,000个元素,它工作正常,但如果我尝试100000元,我得到以下错误:

  *** glibc的检测*** ./set2:realloc的():无效的老大小:0x00000000024dc010 ***

这是code我跑。所以它的易于阅读我评论过了。

 无效的主要()
{
    //大小为10时开始
    INT currentsize = 10;
    为int * ARR =的malloc(currentsize *的sizeof(INT));
    INT I;    // initalize设置为INT_MAX所有元素
    对于(i = 0; I< currentsize;我++){
        改编[I] = INT_MAX;
    }
    //插入随机元素
    对于(i = 0; I< 100000;我++){
        currentsize =添加(RAND()100%,编曲,currentsize);
    }    免费(ARR);
}/ *
    如果需要的方法调整大小数组,并返回该阵列的新的大小
    还插入元素到数组
* /
加INT(INT X,INT *改编,诠释大小)
{
    //查找第一个可用位置
    INT newSize =大小;
    INT I;
    对于(i = 0; I<大小;我++){
        如果(ARR [I] == INT_MAX)
            打破;
    }    如果(ⅰ&GT =大小){
        //需要的realloc
        newSize ++;
        ARR =的realloc(ARR,newSize *的sizeof(INT));
    }    改编[I] = X;    返回newSize;
}


解决方案

该错误可能是因为你正确使用realloc的改变改编在函数添加,但添加返回该修改的值丢失。因此,要下一次调用添加将接受老,现在坏的价值。

另外,我不明白为什么你使用了循环搜索。你知道你要在最后添加元素,那么为什么搜索?只是重新分配阵列,并在新的插槽中插入新的值。

顺便说一句,我pretty确保你的老师试图让你看到重新分配的每个成员导致一个渐进的运行时间的问题。 的realloc 的多数实现都是复制的的很多的这种算法。这就是为什么真正的程序通过的因素增长数组大小的大于1(通常1.5或2),而不是固定的金额。

通常成语是一个抽象的结构体变量大小的数组:

  typedef结构array_s {
  为int *的ELT;
  INT大小;
} VARIABLE_ARRAY;无效的init(VARIABLE_ARRAY *一)
{
  A->大小= 10;
  A->的ELT =的malloc(A->大小* sizeof的A->的ELT [0]);
  //检查空返回来自malloc()函数HERE
}无效ensure_size(VARIABLE_ARRAY *一,为size_t大小)
{
  如果(A->大小<大小){    // RESET大小的位置,以增加因子原大小的
    //大小= 2 * A->大小;    A->的ELT = realloc的(大小* sizeof的A->的ELT [0]);
    A->大小=;    //检查空返回来自r​​ealloc()的HERE
  }
}//设置第i个阵列的位置。如果有不
//足够的空间,展开数组所以。
空集(VARIABLE_ARRAY *一,INT I,INT VAL)
{
  ensure_size(A,I + 1);
  A->的ELT [I] = VAL;
}无效测试(无效)
{
  VARIABLE_ARRAY一个;  初始化(&放大器;一个);  的for(int i = 0; I< 100000;我++){
    设置(安培; A,I,RAND());
  }  ...}

Disclaimer: This is homework. I am attempting it and do not expect or want anyone to do it for me. Just a few pointers (hehe) where I'm going wrong would be appreciated.

The homework requires me to create an int* array that holds 10 elements, and then attempt to insert a million ints into it. Each insertion checks if the array needs to be resized, and if it does, I increase it's size so it can hold one more element.

When I insert 10,000 elements, it works fine, but if I try 100,000 elements, I get the following error:

*** glibc detected *** ./set2: realloc(): invalid old size: 0x00000000024dc010 ***

This is the code I'm running. I've commented it so it's easily readable.

void main()
{
    //begin with a size of 10
    int currentsize = 10;
    int* arr = malloc(currentsize * sizeof(int));       
    int i;

    //initalize with all elements set to INT_MAX
    for(i = 0; i < currentsize; i++) {
        arr[i] = INT_MAX;
    }


    // insert random elements
    for(i = 0; i < 100000; i++) {
        currentsize = add(rand() % 100,arr,currentsize);
    }

    free(arr);
}

/*
    Method resizes array if needed, and returns the new size of the array
    Also inserts the element into the array
*/
int add(int x, int* arr, int size)
{
    //find the first available location 
    int newSize = size;
    int i;
    for(i = 0; i < size; i++) {
        if (arr[i] == INT_MAX)
            break;
    }

    if (i >= size) {
        //need to realloc
        newSize++;
        arr = realloc(arr, newSize * sizeof(int) );     
    }

    arr[i] = x;

    return newSize;
}

解决方案

The error is probably because you properly use realloc to change arr in the function add, but this modified value is lost when add returns. So the next call to add will receive the old, now bad value.

Also I can't understand why you're using a the for loop to search. You know you want to add at the last element, so why search? Just reallocate the array and plug the new value in the new slot.

Incidentally I'm pretty sure your teacher is trying to get you to see that reallocating for each member causes an asymptotic run time problem. Most implementations of realloc will do a lot of copying with this algorithm. This is why real programs grow the array size by a factor greater than one (often 1.5 or 2) rather than by fixed amounts.

The usual idiom is to abstract the variable size array in a struct:

typedef struct array_s {
  int *elts;
  int size;
} VARIABLE_ARRAY;

void init(VARIABLE_ARRAY *a)
{
  a->size = 10;
  a->elts = malloc(a->size * sizeof a->elts[0]);
  // CHECK FOR NULL RETURN FROM malloc() HERE
}

void ensure_size(VARIABLE_ARRAY *a, size_t size) 
{
  if (a->size < size) {

    // RESET size HERE TO INCREASE BY FACTOR OF OLD SIZE
    // size = 2 * a->size;

    a->elts = realloc(size * sizeof a->elts[0]);
    a->size = size;

    // CHECK FOR NULL RETURN FROM realloc() HERE
  }
}

// Set the i'th position of array a. If there wasn't
// enough space, expand the array so there is.
void set(VARIABLE_ARRAY *a, int i, int val)
{
  ensure_size(a, i + 1);
  a->elts[i] = val;
}

void test(void)
{
  VARIABLE_ARRAY a;

  init(&a);

  for (int i = 0; i < 100000; i++) {
    set(&a, i, rand());
  }

  ...

}

这篇关于无效的realloc老大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆