Difference between memmove and memcpy

  Рет қаралды 25,756

CodeVault

CodeVault

Күн бұрын

Пікірлер: 47
@homelessrobot
@homelessrobot 3 жыл бұрын
A buffer is actually not a general purpose solution to the problem, since the size of this buffer would depend on the size of the overlap. Implementing a memmove in this way would need to assume dynamic allocation can happen, or a static overlapping relationship between strings; which is outside of the capabilities of the c compiler's type system. the way memmove actually works in most implementations is that it chooses the order in which the bytes are copied depending on the structure of the overlap. If the end of the destination overlaps with the beginning of the source, you copy in the forward direction so that the first bits in the source don't get clobbered before they are captured. If the beginning of the destination overlaps with the end of the source, the copying in reverse preserve the overlapping last bits of the source. Kind of like a shift, you can think of the intermediate stages between the first and the last operation functioning as a logical buffer to 'carry' state transitions without interfering with future state transitions, and also without requiring a secondary buffer. This is why your example worked the same way for both memmove and memcpy; memcpy is the special case memmove uses for the overlapping structure that your source and destination have. This is not reliably true with optimizations turned on though, because memcpy's type promises to the compiler that the source and destination do not overlap at all, and so a specific optimization of the operation might violate this logical relationship between memmove and memcpy. This is particularly the case for vectorization and x86 movs* instructions that might parallelize things and move things in chunks larger than a byte at a time, so order is either undefined, or operations are simply happening at a granularity larger than a single byte. In addition, memmove and memcpy, while standard library functions, are also language primitives as far as most compilers are concerned. Several operations (like assignment to struct and array lvalues) depend on their implementation for the language to know what to do. I ran into this when doing some work where I did not want to link with libc. I still needed implementations for at least memmove and memcpy... and this is also how I ran into the the ordering stuff. I had to implement them and a buffer just didn't make sense.
@CodeVault
@CodeVault 3 жыл бұрын
Thanks for the detailed explanation!
@LogoSystemCG
@LogoSystemCG 2 жыл бұрын
the buffer works totally fine like the native func, I'm curious about case where the buffer doesn't work :) please share
@homelessrobot
@homelessrobot 2 жыл бұрын
It can only work if a static buffer will suffice (you don't care that a 10 byte and a 20 gigabyte copy/move are reserving enough space for the largest concievable move/copy), or if you are OK with the functions dynamically allocating the buffer. Neither of these is OK in the general case for something as primitive as memcopy and memmove.
@Hardcore_Remixer
@Hardcore_Remixer Жыл бұрын
8:49 If you want the compiler to no longer change anything, then -O0 may help (most C/C++ compilers have this option).
@yanisnwe9763
@yanisnwe9763 8 ай бұрын
Tu m'as fait perdre 9 minutes 14 de ma vie pour nous donner une explication FAUSSE !! FRAUDULEUX
@CodeVault
@CodeVault 6 ай бұрын
What's false about it?
@messageconveyor8210
@messageconveyor8210 3 жыл бұрын
Finally memmove is avoid from overlap. Good sir.please upload more videos.
@shiffterCL
@shiffterCL 4 жыл бұрын
omg your video finally helped me finish my project. Thank you!
@ngoduc5523
@ngoduc5523 3 жыл бұрын
Thanks You Man ! You help me a lot , hope you have a nice day
@mrmo7ox
@mrmo7ox Ай бұрын
thnx for the best explaining ever and i think u will see more comment of me here
@splodys
@splodys 3 жыл бұрын
Sorry but this is a waste of time Should've used an example that actually showed the difference, not only that but your interpretation of how memmove works is wrong, if you look at the code it doesnt use any array to store temporary values if uses if else statments and compares memory location..
@CodeVault
@CodeVault 3 жыл бұрын
It doesn't always need to use a temporary storage, that's right. Still, certain implementations do. In the description there is an example of such an implementation. Here's another one: clc-wiki.net/wiki/memmove.
@splodys
@splodys 3 жыл бұрын
@@CodeVault sorry for my reply, was a bit rude didnt mean it, bugs made me do it. But the issue is that looking at your video it's very hard to understand the difference as you dont exactly show anything, altough the video looked very promising at the start. You explain a bit their difference but make it very confusing and when you try to show an exaple it doesnt work like you intended which would be to show that the copy isnt properly done if they overlap using memcpy only.
@CodeVault
@CodeVault 3 жыл бұрын
No, you're right, the video was a bit rushed and didn't get the message across. I did test a few library implementations to see if there are any differences and all of them show no difference between memcpy and memmove when dealing with overlapping memory. I will be making a follow-up video to take a look at the different possible implementations of memcpy and which ones could cause issues when memory is overlapping. Thanks for the feedback!
@Hardcore_Remixer
@Hardcore_Remixer Жыл бұрын
Thanks for the explaination. I was kind of wondering whether memcpy is useful or not and if not, then what I would use instead (there had tk be something).
@kylabey
@kylabey 2 жыл бұрын
Your explanation is awsome! Thanks a lot!
@hamedelahi2249
@hamedelahi2249 2 жыл бұрын
Excellent. Thank you so much.
@krishnababu5190
@krishnababu5190 10 ай бұрын
How does memcpy() and memmove() will work on dynamically allocated memory ? will same as the strings ( read only memory) or it may be different (after moving we can deallocate the dynamically allocated data)
@CodeVault
@CodeVault 9 ай бұрын
Exactly the same way. For those functions the type of memory doesn't matter
@sayalideo3158
@sayalideo3158 3 жыл бұрын
Thank you so much. This is really helpful.
@di3289
@di3289 4 жыл бұрын
Thank you for the video! It really help me!!!
@thuannguyen-thai4803
@thuannguyen-thai4803 Жыл бұрын
Thank you, CodeVault. Working with memory, should be careful.
@Fine_Mouche
@Fine_Mouche 2 жыл бұрын
what about sames exercices with string like "ßtartiñg Štöp" i mean `n * sizeof(char)` will be not okay with a string containing some multi-character character constant.
@CodeVault
@CodeVault 2 жыл бұрын
You could actually try using wchar for this: www.cplusplus.com/reference/cwchar/
@adirk1259
@adirk1259 2 жыл бұрын
THANK YOU
@vaibhovbhardwaj
@vaibhovbhardwaj 2 жыл бұрын
What if I use volatile in my original string to avoid caching, will the result be any different?
@CodeVault
@CodeVault 2 жыл бұрын
memcpy doesn't work well with volatile memory. See here: stackoverflow.com/questions/54964154/is-memcpyvoid-dest-src-n-with-a-volatile-array-safe
@songchovui1905
@songchovui1905 3 жыл бұрын
I see a vlog about: "Is strcpy dangerous and what should be used instead?" they recommend me to use memcpy instead but when I call : "char a[10] ; char b[]="zzzz" ; memcpy(a,b,strlen(b)); when I printf("%s",a"); output is zzz??????? but when I use strcpy the output is zzz what happen which zzz"???????" why I see that? can u help me find a good way to instead strcpy ?
@CodeVault
@CodeVault 3 жыл бұрын
It's because strcpy copies strlen(b) + 1 number of characters (it includes the NULL terminator) Try calling it like so: memcpy(a,b,strlen(b) + 1);
@songchovui1905
@songchovui1905 3 жыл бұрын
@@CodeVault Thanks💝
@exaktic
@exaktic 4 жыл бұрын
What kinds of compilers or architectures seem to be affected by this case?
@CodeVault
@CodeVault 4 жыл бұрын
We've tested most popular compilers (gcc, Visual C, cygwin and mingw) and all of them implemented both functions using a buffer. Seems like, sometimes, it's just safer to go beyond the specifications and implement it more safely. A viewer has implemented their own versions of memcpy and memmove following exactly the specifications and you should see a difference there. The code is in the description.
@dannygonzal3z
@dannygonzal3z 4 жыл бұрын
Thanks so much!
@arvindersingh9863
@arvindersingh9863 4 жыл бұрын
If, float x=0.01; double y=0.01; Then why x is not equal to y ?
@ajmalc1346
@ajmalc1346 4 жыл бұрын
Because float id 4 byte and double is 8 byte. Bitwise comparison takes adjacent 64 bits, value next 4 bytes of x must be garbage.
@messageconveyor8210
@messageconveyor8210 3 жыл бұрын
@@ajmalc1346 hi I want to know, overlap is disadvantage in memmove and memcpy.
@tuncayusta640
@tuncayusta640 2 жыл бұрын
Prefer to compare non-integer numbers with some tolerance whenever possible.
@pixelmxr
@pixelmxr 3 жыл бұрын
Find diff in source code
@exoticcoder5365
@exoticcoder5365 3 жыл бұрын
good one !!!!!!!!!!!
@arvindersingh9863
@arvindersingh9863 4 жыл бұрын
👍👍👍👍👍
@adeled8833
@adeled8833 4 жыл бұрын
Yay
@Adam-gp3ij
@Adam-gp3ij 4 жыл бұрын
Does not make any sense! WTH
@CodeVault
@CodeVault 4 жыл бұрын
I've put in the description code that implements the functions by the standard (kudos to a fellow viewer). You can try them and you will see the difference then.
@erenkolordo2818
@erenkolordo2818 2 жыл бұрын
this guy talk like indians
How to properly deal with dynamically allocated memory
13:44
CodeVault
Рет қаралды 9 М.
[Day 18] - memset, memcpy, strcpy, memmove
25:27
Mike Shah
Рет қаралды 3,5 М.
Friends make memories together part 2  | Trà Đặng #short #bestfriend #bff #tiktok
00:18
🕊️Valera🕊️
00:34
DO$HIK
Рет қаралды 12 МЛН
Sigma baby, you've conquered soap! 😲😮‍💨 LeoNata family #shorts
00:37
VAMPIRE DESTROYED GIRL???? 😱
00:56
INO
Рет қаралды 8 МЛН
Is memcpy dangerous?
14:08
Jacob Sorber
Рет қаралды 24 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 178 М.
Dereferencing in C
10:29
CodeVault
Рет қаралды 10 М.
Create Your Own memcpy() Memory Copy Function | C Programming Example
13:21
When to free memory in C
13:45
CodeVault
Рет қаралды 10 М.
why do void* pointers even exist?
8:17
Low Level
Рет қаралды 376 М.
Object-Oriented Programming is Embarrassing: 4 Short Examples
28:03
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
Friends make memories together part 2  | Trà Đặng #short #bestfriend #bff #tiktok
00:18