Reputation: 78
I am trying to swap two arrays in C by pointing from array B to array A and then free up A so I am only left with array B with the contents of array A. Is this possible?
Code:
int *a = malloc(4*sizeof(int));
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
int *b = malloc(4*sizeof(int));
//Assign pointer from b to memory position of a so b[] = a[]?
Thank you in advance,
Wouter
Upvotes: 0
Views: 113
Reputation: 5265
#include <string.h>
#define ARRAY_LENGTH 4
// insert your code above here.. and as the other answer says you should
// always check malloc's return value to make sure it succeeded before
// continuing or you will seg fault. If it fails, you should gracefully
// handle the error
memcpy(b, a, sizeof(int) * ARRAY_LENGTH); // copies sizeof(int)*ARRAY_LENGTH bytes
// from the memory space pointed to by a
// to the memory space pointed to by b
free(a); // returns the memory pointed to by a to the heap
More information about memcpy
can be found here. It's a highly optimized function for copying memory. If you only have 4 values, I doubt you'll see much performance difference between your own loop, memcpy
, or simply manually-assigning (unrolling the loop) each value,, unless you're running this many thousands or millions of times.
And just a side note, as a general rule of thumb, you want to use malloc
as sparingly as possible. The only times you should use it are if you don't know how much memory you'll need until runtime, or if you want the scope of the memory to persist outside of the function. Incorrectly managing memory is the source of many, many bugs that can be difficult to track down in large programs since they don't always manifest themselves at the same time in the same place the same way. Here, you don't show enough code for me to know exactly what you're doing. But you do know the size of the array ahead of time (4 int
s), so unless you need these arrays outside of the function, just go ahead and put them in localized scope (on the stack in most systems):
int a[4];
int b[4];
// perform assignment to a
// perform copy to b
// do work on data
// ....
// now when the function ends, these arrays will automatically get destroyed, saving you the trouble
I'll just take you at your word that you have a good reason for copying the a
array, as that's not evident from your code.
Finally, this was a dupe and neither of us should've answered it :) How to copy one integer array to another
Upvotes: 4
Reputation: 6404
If you create an array on the heap, with malloc(), you get a pointer which is like any other variable in that it may be assigned or copied. However there is only one memory buffer.
so
int *a = malloc(n * sizeof(int));
int *b = a;
means both a and b point to the same region of memory. That's called pointer aliasing, and it causes deep problems if you don't keep tight control of what you are doing. Generally if a points to the buffer, we should use a to identify it. A copy isn't useful to us.
If we have, as in your example
int *a = malloc(n * sizeof(int));
int *b = malloc(n * sizeof(int));
we have two buffers. So assigning b = a would make b point to a's buffer, and orphan the buffer that b is pointing to. Again, not what we want.
However if we do this
int *a = malloc(n * sizeof(int));
int *b = malloc(n * sizeof(int));
int * temp'
temp = a;
a = b;
b = temp
We have swapped a and b. b now points to the buffer a pointed to previously, a now points to the buffer b pointed to previously.
That's fine. And occasionally it's sensible to do that.
Upvotes: 0
Reputation: 121407
You can use a loop:
for (size_t i = 0; i < 4; i++)
b[i] = a[i];
Note: always check the return value of malloc()
for failure.
Upvotes: 0