0

I allocated memory to an array (using malloc) but what if it needs more space, is it possible to expand the array later in the program? Or maybe create a new array and have the last element in the first array point to the new array?
I know that realloc would be much easier to use but I am trying to do this only using malloc.

7
  • To change the size look at realloc Commented Oct 15, 2014 at 1:50
  • I am trying to do this without using realloc only malloc Commented Oct 15, 2014 at 1:53
  • Take a look at the man page for memcpy, instead of copying elements in the for loop. But yes, this is the right idea. Commented Oct 15, 2014 at 2:15
  • 1
    I'm confused about the line newArray=new array(array1) You're using malloc, so I'm not sure what that is meant to be Commented Oct 15, 2014 at 2:18
  • Also, don't cast the void pointer returned from malloc. If you absolutely must, then make sure that you #include <stdlib.h> Commented Oct 15, 2014 at 2:19

3 Answers 3

3

The general algorithm is

allocate array of 100
while more input
    if no-room-in-array
        allocate another array 100 bigger than the current array
        copy values from current array into newly created array
        free(current array)
        current array = newly created array (the one you copied into)
    // now there will be room, so
    put input in array
Sign up to request clarification or add additional context in comments.

2 Comments

Don't change your question. If you need to add something add it.
No it doesn't, my implementation of current array = newly created array (the one you copied into) is wrong. Not really sure why
1

Yes, you can use realloc(). Be careful to check the return value before you assign it to the original pointer though. See here: https://stackoverflow.com/a/1986572/4323

3 Comments

Like I said I cant use realloc
@user2737810: what platform are you on that you can't use realloc()?
The program specs dont allow realloc
1

Wrong size passed to malloc().

Rather than passing n bytes, code should pass n * sizeof(int).

// int *array1=(int *)malloc(100);
int *array1 = malloc(100 * sizeof *array1);

// int *newArray=(int *)malloc(size+100);
int *newArray =  malloc((size+100) * szeof *newArray);

Other ideas include

1) No need to cast

    int *array1 = (int *) malloc(...;
    int *array1 = malloc(...);

2) Simplify with memcpy()

    // for(i=0; i<size; i++) newArray[i]=array1[i];
    memcpy(newArray, array, size * sizeof *newArray);

3) Be sure to free()

4) new is a C++ operator, this is C, use malloc().

5) Use size_t rather than int for size.

6) Grow exponentially, rather than linearly

// int *newArray=(int *)malloc(size+100);
size_t newsize = size*3/2;
int *newArray = malloc(newsize);

7) Check for malloc() failure

int *newArray = malloc(newsize);
if (newArray == NULL && newsize > 0) Handle_Failure();

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.