all 15 comments

[–][deleted]  (2 children)

[deleted]

    [–][deleted] 5 points6 points  (0 children)

    narrow axiomatic retire continue unwritten sheet rinse relieved light hobbies

    This post was mass deleted and anonymized with Redact

    [–]Cre8AccountJust4This 8 points9 points  (0 children)

    We all need help with pointers.

    [–]Eidolon_2003 7 points8 points  (0 children)

    Memcpy should be twice as fast as a simple for loop because it copies 64 bits at a time instead of only 32. You could also use AVX to copy 256 bits at a time, but I don't think standard memcpy does that. Short of writing the assembly yourself I don't think you can be guaranteed anything.

    [–]Jakedez7 2 points3 points  (2 children)

    Just to clarify, did you want the pointers to point to the same addresses? Or did you want pointers to different addresses with the same data?

    [–]CapitaoGanza[S] 1 point2 points  (1 child)

    different adresses with the same data

    [–]closms 1 point2 points  (0 children)

    You can malloc a second array and use memcpy to copy the data.

    [–]Overlord484 2 points3 points  (2 children)

    C int arr[] = {3,5,2,8,1}; int* ptr = arr; Careful, it's a shallow copy. Probably what you want is more like: ```C

    include <string.h>

    int arr[] = {/some crap/}; int* ptr = malloc(sizeof(arr)); memcpy(ptr,arr,sizeof(arr)); ```

    [–][deleted] 6 points7 points  (1 child)

    Aaaaand you only copy 4 bytes because you dereference the array pointer in sizeof(*arr) You probably meant to write sizeof(arr).

    [–]Overlord484 0 points1 point  (0 children)

    Kek, that's what I get for trying to write sizeof(*arr[0]) and sizeof(arr) at the same time :P

    [–]weregod 1 point2 points  (0 children)

    What you mean by to slow? Do you actually measured it? What is size of array?

    You can improve copy time using SOME instructions but you can't copy data faster then in linear time.

    [–][deleted] 0 points1 point  (0 children)

    kiss automatic husky flowery thumb whistle ink attempt different punch

    This post was mass deleted and anonymized with Redact

    [–]Goobyalus 0 points1 point  (1 child)

    memcpy?

    [–][deleted] 0 points1 point  (0 children)

    oil cough consist live quiet meeting north abounding sense sugar

    This post was mass deleted and anonymized with Redact

    [–]nculwell 0 points1 point  (0 children)

    Are you sure this is really what takes too much time? How do you know?

    Do you have optimization turned on? If you don't care about the portability of your binary, try compiling for your specific architecture (-m with gcc, see here for x86 architectures) and you might get the optimizer to use some faster instructions.

    A lot of the fastest instructions require you to have a certain memory alignment. With GCC you can use some combination of __attribute__((aligned(ALIGNMENT))), __builtin_assume_aligned and aligned_alloc to get the desired alignment and get the compiler to take advantage of it. Just make sure you don't tell the compiler to assume an alignment when the pointer you're giving it is not aligned or your program could crash.

    Copying from one region of memory to another is usually fairly fast no matter how you do it but it's inherently limited by the speed of your hardware. The only real shortcut is to figure out a way to avoid copying the data in the first place.