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
.
Asked
Active
Viewed 97 times
0

user2737810
- 21
- 1
- 7
-
To change the size look at realloc – Jerry Jeremiah Oct 15 '14 at 01:50
-
I am trying to do this without using realloc only malloc – user2737810 Oct 15 '14 at 01: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. – Don Shankin Oct 15 '14 at 02:15
-
1I'm confused about the line `newArray=new array(array1)` You're using malloc, so I'm not sure what that is meant to be – Don Shankin Oct 15 '14 at 02:18
-
Also, don't cast the void pointer returned from malloc. If you absolutely must, then make sure that you `#include
` – Don Shankin Oct 15 '14 at 02:19 -
I wasn't sure how to implement what Dwayne explained. The current array = newly created array (the one you copied into) – user2737810 Oct 15 '14 at 02:22
-
You need to check whether `num` is out of range *before* you write to `array1[num]`, not afterwards. In your current code you'll write to `array1[100]` before you figure out that that's past the end of your array. – Crowman Oct 15 '14 at 02:59
3 Answers
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

Dwayne Towell
- 8,154
- 4
- 36
- 49
-
Don't change your question. If you need to add something add it. – Dwayne Towell Oct 15 '14 at 02:15
-
No it doesn't, my implementation of current array = newly created array (the one you copied into) is wrong. Not really sure why – user2737810 Oct 15 '14 at 02:33
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

Community
- 1
- 1

John Zwinck
- 239,568
- 38
- 324
- 436
-
-
@user2737810: what platform are you on that you can't use realloc()? – John Zwinck Oct 15 '14 at 01:53
-
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();

chux - Reinstate Monica
- 143,097
- 13
- 135
- 256