0

What is the best practice for swapping arrays in C?

I got the following use case:

void func1 () {
    uint32_t a[2] = {0x00000001,0x40000000};
    uint32_t t = 2;
    do_some_magic(&t, a); 
    work_with_modefied(t,a);
}

void do_some_magic(uint_32_t *t,*a){
    //while being a magician 
    uint32_t *out;
    out = (uint32_t *) malloc((*t+1)*4);
    //modify a[i] and store in out[i];
    //some other work
    //the tricky part
    *t++;        // works excellent
   // a = out      wouldn't work
   // *a = *out    wouldn't work
}
4
  • 5
    It's hard to tell what you're actually trying to do. Can you clarify your question a little? Commented Dec 15, 2013 at 22:48
  • It is a constant object that can not be rewritten array. It will substitute a pointer to such purposes. Commented Dec 15, 2013 at 23:18
  • If you are expecting a[1] to have the value of out[1], it won't. *a is equivalent to a[0] and *out is equivalent o out[0]. The end result is a[0] getting the value of out[0] and a[1] is preserved to whatever value it had prior to the step *a = *out; Commented Dec 15, 2013 at 23:54
  • 1
    Arrays are not assignable. That is all. Commented Dec 16, 2013 at 0:26

1 Answer 1

1

What you are trying to do is assign a to point to the newly allocated memory, from what I gather. This won't work because a is an array, not a pointer. In order to achieve what you want you need to store and modify a pointer to an array. You can achieve your swap in two ways. For both, func1 will be:

void func1 () {
    uint32_t t = 2;
    uint32_t a[2] = {0x00000001,0x40000000};
    uint32_t * b = a;
    b = do_some_magic(&t); 
    work_with_modified(t,b);
}

uint32_t * do_some_magic(uint32_t *t){
    *t++;
    return malloc((*t) * sizeof(uint32_t));
}

or alternatively:

void func1 () {
    uint32_t t = 2;
    uint32_t a[2] = {0x00000001,0x40000000};
    uint32_t * b = a;
    do_some_magic(&t, &b); 
    work_with_modified(t,b);
}

void do_some_magic(uint32_t *t, uint32_t **b){
    *t++;
    *b = malloc((*t) * sizeof(uint32_t));
}

The second is closer to your original code. Of course error checking has been ignored here as with your original example. You also need to take note of the fact that do_some_magic has allocated memory on the heap. This memory needs to be freed later. If you call the do_some_magic multiple times, you need to free the memory pointed to by b before each subsequent call (except the first call where you used a automatically allocated array).

Finally, this and your original code are not really swapping arrays. The code simply allocates a new array in place of an old one. But I think this answers the essence of your question.

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

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.