0

I need to re-size a malloc'd C array. However, I'm not supposed to use realloc() (it's for an assignment). The below code keeps throwing double free or corruption (out) More specifically, this function seems to do nothing, since the program behaves the same whether I call it or not. I feel like I'm probably missing something basic. Can anyone help me out? Thanks a lot

void double_array_size(float *array, int *size) {
  float *temp = NULL;
  int i;
  temp = (float *) malloc(*size * 2 * sizeof(float));

  for (i = 0; i < *size; i++) {
    temp[i] = array[i];
  }

  *size *= 2;

  free(array);
  array = temp;
}
5
  • 2
    What do you expect void f(int x) {x = 5;} int main() {int i = 7; f(i); printf("%d\n", i); return 0;} to print? Commented Oct 4, 2015 at 22:44
  • @immibis right, but I thought the idea of passing pointers was that it let you change things like that from within another method Commented Oct 4, 2015 at 22:47
  • 1
    you need passing pointer to pointer like as float **array. Commented Oct 4, 2015 at 22:48
  • @B1CL0PS You need to post the rest of the code, the main issue is addressed here, but I am still curious to see the rest of the code to see how do you expect it to work, and never use free() like that, the context where you call free() seems wrong and dangerous to me. Commented Oct 4, 2015 at 23:13
  • @B1CL0PS passing pointers allows the function to change the objects pointed to by the pointer. But double_array_size() is also changing the pointer itself. Commented Oct 4, 2015 at 23:26

2 Answers 2

2

Like BLUEPIXY said, pass a double pointer or if you don't want to, better this:

float *double_array_size(float *array, int *size) {
  float *temp = NULL;
  int i;
  temp = (float *) malloc(*size * 2 * sizeof(float));

  for (i = 0; i < *size; i++) {
    temp[i] = array[i];
  }   

  *size *= 2;

  free(array);
  return temp;
}   

and to increase the performance of you code, do not use a for loop, use memcpy instead:

temp = (float *) malloc(*size * 2 * sizeof(float));
memcpy(temp, array, sizeof *array * *size);

You should always check the return value of malloc and friends. They might return NULL and depending on your code, you might have to react to this if you don't want your code to crash.

Another thing: if you are using C only, do not cast malloc and I suggest to use sizeof *var instead of sizeof(float) because you are hardcoding the type. Let's say that you have to change the type of the array. If you hardcore your types, you have to change the types everywhere, otherwise only in the declarations, much less work and fewer errors.

Sometimes you need more or less the same code for different types. Luckily in C++ you have templates, in C you have to use Macros, if you don't want to repeat basically the same code over and over, for example:

#define DOUBLE_ARRAY_SIZE(type) \
    type *double_array_size_ ##type (type *array, size_t *size) {\
          type *temp; \
          temp = malloc(*size * 2 * sizeof *array);\
          if(temp == NULL)\
              return NULL;\
          memcpy(temp, array, sizeof *array * *size);\
          *size *= 2;\
          free(array);\
          return temp;\
        }

And instead of writing the same code with different types over and over, you can do

DOUBLE_ARRAY_SIZE(int)
DOUBLE_ARRAY_SIZE(double)
DOUBLE_ARRAY_SIZE(record_t)

void foo()
{
    int *ints = malloc(...);
    record_t *recs = malloc(...);
    ...
    new_ints = double_array_size_int(ints, ints_size);
    new_recs = double_array_size_record_t(recs, recs_size);
    ....
}

I know that a lot of people will say do not use macros, they're evil and they are kind of true, but using them wisely may help you more thank you think. One of the reason you should try to hardcore the datatype as less as possible.

// edit my macro snippet, chux's suggestion with size_t

Sign up to request clarification or add additional context in comments.

2 Comments

Minor: size_t *size instead of int *size.
Do not cast the result of malloc & friends in C!
2

One problem with this code is that parameters are call-by-value, so you’re changing only the local copy of array and leaking memory. The value in the rest of the program is never updated. What you want to do is return the reallocated array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.