1

I need to write a function that makes a deep copy of an array of integers, using only pointer arithmetic (i.e. cannot use array-element syntax like orig[i]).

The first argument is the array to copy, and its length is specified by the second argument.

Here is what I have so far:

int* copyArray(const int *orig, int size) {

  int *copy = malloc(size * sizeof *copy);

  int **toReturn = ©

  for (int i = 0; i < size; i++, orig++, copy++) {
    *copy = *orig;
  }

  return *toReturn;

}

int main () {

  int testArray[3] = {1, 2, 3};
  int *testptr = testArray;

  int *copied = copyArray(testptr, 3);

  for (int i = 0; i < 3; i++) {

    printf("%d %p", testArray[i], &testArray[i]);
    printf("\n");

  }

  printf("\n");

  for (int i = 0; i < 3; i++) {

    printf("%d %p", copied[i], &copied[i]);
    printf("\n");

  }

return 0;
}

This isn't getting me the right results... The result I get from this is:

1 0x7ffce0ff6fd0
2 0x7ffce0ff6fd4
3 0x7ffce0ff6fd8

0 0xe1301c
0 0xe13020
0 0xe13024

whereas it should be:

1 0x7ffce0ff6fd0
2 0x7ffce0ff6fd4
3 0x7ffce0ff6fd8

1 0xe1301c
2 0xe13020
3 0xe13024

I'm new to pointers and memory allocation... What am I doing wrong?

UPDATE: Problem fixed!

Look at my answer, or the other answers that work as well.

7
  • 2
    You're incrementing copy, so when you return it, it points to the end of the array. Also, you're allocating only 3 bytes of memory; it should be malloc(size * sizeof *copy). Third problem found: use size in the for loop in copyArray(), not 3.
    – potrzebie
    Oct 9, 2016 at 23:32
  • Thanks @potrzebie! I made the changes you recommended... but I'm still getting the same result. I'm not sure if I made the right change to the return statement though. Please have a look! UPDATED. Oct 9, 2016 at 23:41
  • 1
    @RockAndaHardPlace You can't use the line copy++. That changes the value you return. The value of copy that you get from malloc needs to be returned unchanged from your copyArray function. Oct 9, 2016 at 23:57
  • @AndrewHenle I posted some updates that I think address your concern, yet I'm still getting the same result. :( Please look at the updated question. Oct 10, 2016 at 0:00
  • 1
    You're returning the end of the array, you need to return what copy pointed to before you did copy++. Also toReturn is redundant, what you have now is the same as return copy;.
    – M.M
    Oct 10, 2016 at 0:04

2 Answers 2

2

Change copyArray to something like

int *copyArray( const int *orig, size_t size )
{
    int *copy = malloc(size * sizeof( *copy ) );

    for ( size_t ii = 0; ii < size; ii++ )
    {
        copy[ ii ] = orig[ ii ];
    }
    return( copy );
}

Stylistically, I personally like ii instead of i. It's hard to search for i. And size_t is a better choice than int for something that's used to describe the size of something.

You probably should add some error checking should malloc return NULL.

And, without indexing the pointers (because simple code is to be banned?!?)

int *copyArray( const int *orig, size_t size )
{
    int *copy = malloc(size * sizeof( *copy ) );

    memcpy( copy, orig, size * sizeof( *copy ) );
    return( copy );
}

That's likely faster anyway. Proper header inclusion is left as an exersize for the reader... ;-)

It still needs error checking, though.

3
  • Thanks! This will most probably work, but I am not allowed to use array-element syntax like copy[ii] as I mentioned in the question. Oct 10, 2016 at 0:06
  • 1
    @RockAndaHardPlace I am not allowed to use array-element syntax like copy[ii] as I mentioned in the question. OK, I won't ask why. Simple fix on the way.... Oct 10, 2016 at 0:07
  • Thanks again, @AndrewHenie! Oct 10, 2016 at 0:14
1

This is a working version of the copyArray function:

int* copyArray(const int *orig, int size) {

  int *copy = malloc(size * sizeof *copy);

  int *toReturn = copy;

  for (int i = 0; i < size; i++, orig++, copy++) {
    *copy = *orig;
  }

  return toReturn;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.