In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of memory functions in C language. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Memory function memcpy memory copy
The function memcpy copies num bytes of data back from the location of source to the memory location of destination.
This function does not stop when it encounters'\ 0'.
If there is any overlap between source and destination, the result of replication is undefined.
Original format
Analysis.
* * literally everything in memory can be copied, so the spell of string copy is broken, and any type can be copied, so there is no need to think about it. It must have something to do with universal type (universal type pointer-untyped pointer) void*, because it was impressive to do qsort at that time.
/ * num means several bytes * / void* my_memcpy (void* dest, const void* src, size_t num) {assert (dest & & src); void* ret = dest; while (num--) {/ / divide it into minimum with void* char*, and then pass * (char*) dest = * (char*) src one by one ((char*) dest) + +; / / if void* is untyped, it is difficult to add directly, so the problem of forcibly converting char* and adding ((char*) src) + +;} return ret;} memory copy
1. If memory is related, errors will be copied.
So how do you solve the memory correlation without the above error? the memory space with the intersection of positive assignments will be operated twice and the original value will be changed. What do we do? if we come from behind, the first two operations will change the back, then take away the assignment first, and indirectly change the original situation, so the above code has to be modified. This is the case of copying to the back. if the item is copied to the front, is there a problem coming from the back? instead, it will be more perfect from the front, so we have to consider both situations.
So in order to solve the problem of overlapping copies, we have the function memmove.
two。 When there is not enough memory, you have to copy the direct program inside and hang up.
Memmove memory overlay copy
Used to deal with memory overlap
C language regulation
Memcpy only needs to handle copies that do not overlap memory.
Memmove handles overlapping memory copies
We rewrite the code of memcpy to meet the requirements of C language. In the compiler of vs, memcpy actually overfulfills the task, and its effect is the same as that of memmove.
You will find that the effect of their running is the same, so I tested the above test with my own code (which has reached the C language standard).
Let's be more detailed and we will see the content of memmove.
Original format
Analysis.
/ * num means several bytes * / void* my_memmove (void* dest,const void* src, size_t num) / / memmove and memcpy have the same parameters {assert (dest & & src); void* ret = dest; if (dest)
< src) { while (num--) { //sre内存从前向后拷贝 //和void*连用char*,分成最小然后一个一个传 *(char*)dest = *(char*)src; ((char*)dest)++;//void*无类型不好直接加加,就强转char*再加加 ((char*)src)++; } } else { while (num--) { //sre内存从后向前拷贝 //和void*连用char*,分成最小然后一个一个传 *((char*)dest+num) = *((char*)src+num); //((char*)dest)++;//void*无类型不好直接加加,就强转char*再加加 //((char*)src)++; } } return ret;} memset内存设置 将缓冲区设置为指定的字符。 原格式 memcmp内存比较 和strcmp相似,只不过一个是比较字符串,一个比较内存,由于不知道什么类型,所以后面有字节个数限制,准确的说应该和strncmp相似,因为后面都有一个个数的参数 原格式 分析 基本和字符串比较一样,就是变成了内存比较罢了 //buf1内存里的内容比buf2内存里的内容大就>0, vice versa 0) return 1; if (* (char*) buf1-* (char*) buf2 < 0) return-1; return 0;}
Don't overshoot your count, because it operates memory and does not have the function of string patching.
This is the end of this article on "sample analysis of memory functions in C language". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.