memcpy

When __builtin_memcpy is replaced with libc's memcpy

China☆狼群 提交于 2019-12-18 03:56:00
问题 There is a version of C99/posix memcpy function in GCC: __builtin_memcpy . Sometimes it can be replaced by GCC to inline version of memcpy and in other cases it is replaced by call to libc's memcpy. E.g. it was noted here: Finally, on a compiler note, __builtin_memcpy can fall back to emitting a memcpy function call. What is the logic in this selection? Is it logic the same in other gcc-compatible compilers, like clang/llvm, intel c++ compiler, PCC, suncc (oracle studio)? When I should prefer

optimized memcpy

不想你离开。 提交于 2019-12-18 03:08:29
问题 Are there faster alternatives to memcpy() in C++? 回答1: Unlikely. Your compiler/standard library will likely have a very efficient and tailored implementation of memcpy. And memcpy is basically the lowest api there is for copying one part of memory to another. If you want further speedups, find a way to not need any memory copying. 回答2: First, a word of advice. Assume that the people who wrote your standard library are not stupid. If there was a faster way to implement a general memcpy, they'd

Is memcpy of a pointer the same as assignment?

て烟熏妆下的殇ゞ 提交于 2019-12-17 16:32:26
问题 Introduction: This question is part of my collection of C and C++ (and C/C++ common subset) questions regarding the cases where pointers object with strictly identical byte-wise representation are allowed to have different "values", that is, to behave differently for some operation (including to have defined behavior on one object and undefined behavior on the other). Following another question which caused much confusion, here is question about pointer semantics that will hopefully clear

Will memcpy or memmove cause problems copying classes?

点点圈 提交于 2019-12-17 13:06:51
问题 Suppose I have any kind of class or structure. No virtual functions or anything, just some custom constructors, as well as a few pointers that would require cleanup in the destructor. Would there be any adverse affects to using memcpy or memmove on this structure? Will deleting a moved structure cause problems? The question assumes that the memory alignment is also correct, and we are copying to safe memory. 回答1: In the general case, yes, there will be problems. Both memcpy and memmove are

Why would the behavior of std::memcpy be undefined for objects that are not TriviallyCopyable?

和自甴很熟 提交于 2019-12-17 02:12:15
问题 From http://en.cppreference.com/w/cpp/string/byte/memcpy: If the objects are not TriviallyCopyable (e.g. scalars, arrays, C-compatible structs), the behavior is undefined. At my work, we have used std::memcpy for a long time to bitwise swap objects that are not TriviallyCopyable using: void swapMemory(Entity* ePtr1, Entity* ePtr2) { static const int size = sizeof(Entity); char swapBuffer[size]; memcpy(swapBuffer, ePtr1, size); memcpy(ePtr1, ePtr2, size); memcpy(ePtr2, swapBuffer, size); } and

memmove() -- 拷贝内存内容

和自甴很熟 提交于 2019-12-14 23:53:48
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> memmove() -- 拷贝内存内容 2007年07月06日 星期五 11:41 相关函数: bcopy(), memccpy() , memcpy() , strcpy(), strncpy() 表头文件: #include <string.h> 定义函数: void *memmove(void *dest, const void *src, size_t n); 函数说明: memmove()与memcpy()一样都是用来拷贝src所指的内存内容前n个字节到dest所指的地址上。不同的是,当src和dest所指的内存区域重叠时,memmove()仍然可以正确的处理,不过执行效率上会比使用memcpy()略慢些。 返回值: 返回指向dest的指针。 附加说明: 指针src和dest所指的内存区域可以重叠。 memcpy()、 memmove()和memccpy() ------------------------------------------------------- 这三个函数的功能均是将某个内存块复制到另一个内存块。前两个函数的区别在于它们处理内存区域重叠(overlapping)的方式不同。第三三个函数的功能也是复制内存,但是如果遇到某个特定值时立即停止复制。 对于库函数来说

memcpy的内存重叠问题

青春壹個敷衍的年華 提交于 2019-12-14 23:45:13
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 【注】改编自 memmove 和 memcpy的区别 。原作者如有不爽,请告知! memcpy是C语言中的库函数,在头文件string.h中,作用是拷贝一定长度的内存的内容,原型分别如下: void *memcpy(void *dest, const void *src, size_t count) 使用memcpy时,有可能会遇到内存重叠的问题: 第一种情况下,拷贝重叠的区域不会出现问题,内容均可以正确的被拷贝。 第二种情况下, 问题出现在右边的两个字节,这两个字节的原来的内容首先就被覆盖了,而且没有保存 。所以接下来拷贝的时候, 拷贝的是已经被覆盖的内容,显然这是有问题的 。 通过memmove可以避免这一问题。memmove和memcpy实现一样的功能:内存拷贝。原型如下: void *memmove(void *dest, const void *src, size_t count) 以下几点你需要了解: memove可以避免内存拷贝时的重叠问题。 实际上,memcpy只是memmove的一个子集。 memcpy比memmove的速度要快一些。 有兴趣的,可以看看 linux的源码 ,实现很简单,一看就明白。 /** * memcpy - Copy one area of memory to

c memmove和memcpy的实现和区别

微笑、不失礼 提交于 2019-12-14 23:38:55
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> memcpy和memmove()都是C语言中的库函数,在头文件string.h中,作用是拷贝一定长度的内存的内容,原型分别如下: void *memcpy(void *dst, const void *src, size_t count); void *memmove(void *dst, const void *src, size_t count); 他们的作用是一样的,唯一的区别是,当内存发生局部重叠的时候,memmove保证拷贝的结果是正确的,memcpy不保证拷贝的结果的正确。 第一种情况下,拷贝重叠的区域不会出现问题,内容均可以正确的被拷贝。 第二种情况下,问题出现在右边的两个字节,这两个字节的原来的内容首先就被覆盖了,而且没有保存。所以接下来拷贝的时候,拷贝的是已经被覆盖的内容,显然这是有问题的。 实际上,memcpy只是memmove的一个子集。 二者的c语言实现很简单,有兴趣的朋友可以去看看。在实际情况下,这两个函数都是用汇编实现的。 memmove在copy两个有重叠区域的内存时可以保证copy的正确,而memcopy就不行了,但memcopy比memmove的速度要快一些,如: char s[] = "1234567890"; char* p1 = s; char* p2 = s+2;

string, CString, char[]与ASCII的字符表示

情到浓时终转凉″ 提交于 2019-12-14 23:33:31
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 对于字符串的处理在C++中可谓是一个颇为棘手的问题,而像JAVA和C#这种基于托管的平台则不存在此类问题。 我们先来讨论一下memcpy和strcpy这两个方法。 void* memcpy(void *memTo, const void *memFrom, size_t size); char* strcpy(char * dest, const char * src); 这两个方法的区别主要有一下3个: 1. 复制的内容不同,strcpy只能复制字符串,而memcpy则可以复制任何的内容,例如char[],int,struct,class等。 2. 复制的方法不同,strcpy不需要指定要复制的长度,当遇到在src字符串中的“\0”(空字符)时才停止复制,因此很容易出现溢出的现象。而memcpy则是根据其第三个参数决定要复制的长度的,避免了此类问题。 3. 用途不同,通常在复制字符串时用strcpy,而在复制其他类型的数据时则一般采用memcpy。 4. 若要复制ASCII为1的SOH,在memcpy中要用\0表示,如果直接输入0,则代表0这个字符。 需要注意的是: 在发送与设备之间通讯的命令的情况下,很多时候命令会包含空字符null,这种时候就要慎用strcpy了

【面试题】实现memcpy函数

不打扰是莪最后的温柔 提交于 2019-12-14 23:33:11
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 面试中如问到memcpy的实现,那就要小心了,这里有陷阱。 先看下标准memcpy()的解释: void *memcpy(void *dst, const void *src, size_t n); //If copying takes place between objects that overlap, the behavior is undefined. 注意下面的注释,对于地址重叠的情况,该函数的行为是未定义的。 事实上所说的陷阱也在于此,自己动手实现memcpy()时就需要考虑地址重叠的情况。 另外,标准库也提供了地址重叠时的内存拷贝函数:memmove(),那么为什么还要考虑重写memcpy()函数呢? 因为memmove()函数的实现效率问题,该函数把源字符串拷贝到临时buf里,然后再从临时buf里写到目的地址,增加了一次不必要的开销。 下面给出memcpy()的实现,为了与标准库函数区分,我们实现其包裹函数: #include <stdio.h> #include <stdlib.h> #include <string.h> void *Memcpy(void *dst, const void *src, size_t size); int main(int argc, char *argv[]