memcpy vs for loop - What's the proper way to copy an array from a pointer?

前端 未结 6 1458
误落风尘
误落风尘 2020-12-13 00:33

I have a function foo(int[] nums) which I understand is essentially equivalent to foo(int* nums). Inside foo I need to copy the conte

6条回答
  •  遥遥无期
    2020-12-13 01:00

    Memcpy will probably be faster, but it's more likely you will make a mistake using it. It may depend on how smart your optimizing compiler is.

    Your code is incorrect though. It should be:

    memcpy(myGlobalArray, nums, 10 * sizeof(int) );
    

提交回复
热议问题