0

I have an issue where i'm using memcpy() to copy an array to a new array with dynamic memory. And my question is why there is 3 zeros between the numbers? like my original array is a1[] ={1, 2, 3, 4, 5} and when I use memcpy(mem1,a1,n1) then my mem1 will be 1 0 0 0 2 ? Here's my code:

int join_arrays(unsigned int n1, int *a1, unsigned int n2, int *a2, unsigned 
int n3, int *a3)
{
    /*Here I just print the original a1 just to make sure it's correct*/
    for (int j = 0; j < 5; j++) {
        printf("%d  ", a1[j]);
    }
    /*I allocate the memory for the new array*/
    char *mem1;
    mem1 = malloc(n1*sizeof(int));

    /*checking if the allocation succeeded*/
    if (!mem1) {
    printf("Memory allocation failed\n");
    exit(-1);
    }

    /*Using memcpy() to copy the original array to the new one*/
    memcpy(mem1, a1, n1);

    /*Printing the new array and this print gives me "1 0 0 0 2"
      and it should give me "1 2 3 4 5"*/
    printf("\n");
    for (int i = 0; i < 5; i++) {
        printf("%d  ", mem1[i]);
    }
    return 0;
}
int main(void)
{
    /* these are the original arrays which I need to put together to a single array */
    int a1[] = { 1, 2, 3, 4, 5 };
    int a2[] = { 10, 11, 12, 13, 14, 15, 16, 17 };
    int a3[] = { 20, 21, 22 };

    /*The number of elements are before the array itself*/
    join_arrays(5, a1, 8, a2, 3, a3);


    return 0;
}

2 Answers 2

3

Instead of allocating the memory and assigning it to char* take a int* and work with it.

int *mem1;
mem1 = malloc(n1*sizeof(int)); // malloc(n1 * sizeof *mem1);
..
memcpy(mem1,a1,n1*sizeof(a1[0]));

Also check whether malloc failed or not - but add a proper error message:-

if (!mem1) {
   perror("Memory allocation failed\n");
   exit(EXIT_FAILURE);
}

Don't forget to free the dynamically allocated memory, in your case

free(mem1);

As you have said, that you need to return it from the function then you would do something like this

int *join_arrays(..){

  return mem1;
}
int main(void){

  int *p = join_arrays(..);
  /* work with it */

  free(p);
}
Sign up to request clarification or add additional context in comments.

4 Comments

This worked thanks, but my function have to return a pointer to that new array mem1 so do I have to free the memory still?
@michael.: Will add this to answer..wait.
@michael.: Edited.
Okay yea that's what I thought!
1

mem1 here is a char pointer not an int pointer.

Therefore when you try to print mem1[i] it will actually print the byte that is stored at address mem1+i instead of 4 bytes. Apparently the integer 1 is stored like this on your machine :

 00000001 00000000 00000000 00000000

that is why you get 3 zeros.

Try changing your variable type to int* like this :

int *mem1;
mem1 = malloc(n1*sizeof(int));
memcpy(mem1,a1,n1*sizeof(int));

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.