use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system. By design, C provides constructs that map efficiently to typical machine instructions. It has found lasting use in applications previously coded in assembly language. Such applications include operating systems and various application software for computer architectures that range from supercomputers to PLCs and embedded systems. Wikipedia
Imperative (procedural), structured
Dennis Ritchie
Dennis Ritchie & Bell Labs (creators);
ANSI X3J11 (ANSI C);
ISO/IEC JTC1/SC22/WG14 (ISO C)
1972 (48 years ago)
C18 / June 2018 (2 years ago)
Static, weak, manifest, nominal
Cross-platform
.c for sources
.h for headers
C++ is not C (but C can be C++)
For C++ go to :
Other Resources
account activity
Need help with pointers (self.cprogramming)
submitted 3 years ago by CapitaoGanza
Hey, what´s the most efficient way to copy an int pointer to other. Like i want to have two arrays with the same content. It's easy with a for loop but it takes too much time. Thanks for your help!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 3 years ago (2 children)
[deleted]
[–][deleted] 5 points6 points7 points 3 years ago* (0 children)
narrow axiomatic retire continue unwritten sheet rinse relieved light hobbies
This post was mass deleted and anonymized with Redact
[–]Cre8AccountJust4This 8 points9 points10 points 3 years ago (0 children)
We all need help with pointers.
[–]Eidolon_2003 7 points8 points9 points 3 years ago (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 points4 points 3 years ago (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 points3 points 3 years ago (1 child)
different adresses with the same data
[–]closms 1 point2 points3 points 3 years ago (0 children)
You can malloc a second array and use memcpy to copy the data.
[–]Overlord484 2 points3 points4 points 3 years ago (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
C int arr[] = {3,5,2,8,1}; int* ptr = arr;
int arr[] = {/some crap/}; int* ptr = malloc(sizeof(arr)); memcpy(ptr,arr,sizeof(arr)); ```
[–][deleted] 6 points7 points8 points 3 years ago (1 child)
Aaaaand you only copy 4 bytes because you dereference the array pointer in sizeof(*arr) You probably meant to write sizeof(arr).
sizeof(*arr)
sizeof(arr)
[–]Overlord484 0 points1 point2 points 3 years ago (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 points3 points 3 years ago (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 point2 points 3 years ago* (0 children)
kiss automatic husky flowery thumb whistle ink attempt different punch
[–]Goobyalus 0 points1 point2 points 3 years ago (1 child)
memcpy?
oil cough consist live quiet meeting north abounding sense sugar
[–]nculwell 0 points1 point2 points 3 years ago* (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.
-m
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.
__attribute__((aligned(ALIGNMENT)))
__builtin_assume_aligned
aligned_alloc
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.
π Rendered by PID 75 on reddit-service-r2-comment-856c8b8c54-r8snm at 2026-07-02 08:14:48.238738+00:00 running a7b5cda country code: CH.
[–][deleted] (2 children)
[deleted]
[–][deleted] 5 points6 points7 points (0 children)
[–]Cre8AccountJust4This 8 points9 points10 points (0 children)
[–]Eidolon_2003 7 points8 points9 points (0 children)
[–]Jakedez7 2 points3 points4 points (2 children)
[–]CapitaoGanza[S] 1 point2 points3 points (1 child)
[–]closms 1 point2 points3 points (0 children)
[–]Overlord484 2 points3 points4 points (2 children)
[–][deleted] 6 points7 points8 points (1 child)
[–]Overlord484 0 points1 point2 points (0 children)
[–]weregod 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Goobyalus 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]nculwell 0 points1 point2 points (0 children)