memcpy

c语言 memcpy()

谁说我不能喝 提交于 2019-12-05 14:22:29
原文地址: https://blog.csdn.net/qq_21792169/article/details/50561570 头文件:#include <string.h> memcpy() 用来复制内存,其原型为: void * memcpy ( void * dest, const void * src, size_t num ); memcpy() 会复制 src 所指的内存内容的前 num 个字节到 dest 所指的内存地址上。 memcpy() 并不关心被复制的数据类型,只是逐字节地进行复制,这给函数的使用带来了很大的灵活性,可以面向任何数据类型进行复制。 需要注意的是: dest 指针要分配足够的空间,也即大于等于 num 字节的空间。如果没有分配空间,会出现断错误。 dest 和 src 所指的内存空间不能重叠(如果发生了重叠,使用 memmove() 会更加安全)。 与 strcpy() 不同的是,memcpy() 会完整的复制 num 个字节,不会因为遇到“\0”而结束。 【返回值】返回指向 dest 的指针。注意返回的指针类型是 void,使用时一般要进行强制类型转换。 代码示例: #include <stdio.h> #include <stdlib.h> #include <string.h> #define N (10) int main() {

'memcpy'-like function that supports offsets by individual bits?

安稳与你 提交于 2019-12-05 11:51:10
I was thinking about solving this, but it's looking to be quite a task. If I take this one by myself, I'll likely write it several different ways and pick the best, so I thought I'd ask this question to see if there's a good library that solves this already or if anyone has thoughts/advice. void OffsetMemCpy(u8* pDest, u8* pSrc, u8 srcBitOffset, size size) { // Or something along these lines. srcBitOffset is 0-7, so the pSrc buffer // needs to be up to one byte longer than it would need to be in memcpy. // Maybe explicitly providing the end of the buffer is best. // Also note that pSrc has NO

memcpy a buffer and an array not working

风格不统一 提交于 2019-12-05 10:44:17
I have a requirement in which i need to pass an empty array as a parameter to a function. And in this called function, i should be memcpy some data into the passed array. So i have written a small example which is same as my requirement. Below is its code: #include <stdio.h> #include <stdlib.h> void printArr(int *a) { int i; int *b=(int*)malloc(sizeof(int)*10); printf("\n\nEnter 10 lements:\n"); for(i=0;i<10;i++) scanf("%d",&b[i]); printf("\nContents of array b:\n"); for(i=0;i<10;i++) printf("%d\t",b[i]); printf("\n"); memcpy(a,b,10); printf("\nContents of array a:\n"); for(i=0;i<10;i++)

Can I use memcpy to write to multiple adjacent Standard Layout sub-objects?

南笙酒味 提交于 2019-12-05 06:36:58
Disclaimer: This is trying to drill down on a larger problem, so please don't get hung up with whether the example makes any sense in practice. And, yes, if you want to copy objects, please use / provide the copy-constructor. (But note how even the example does not copy a whole object; it tries to blit some memory over a few adjacent(Q.2) integers.) Given a C++ Standard Layout struct , can I use memcpy to write to multiple (adjacent) sub-objects at once? Complete example: ( https://ideone.com/1lP2Gd https://ideone.com/YXspBk ) #include <vector> #include <iostream> #include <assert.h> #include

Microsoft SDL and memcpy deprecation

元气小坏坏 提交于 2019-12-05 01:57:05
As some of you may know, Microsoft banned memcpy() from their Security Development Lifecycle , replacing it with memcpy_s() . void *memcpy(void *dest, const void *src, size_t n); /* simplified signature */ errno_t memcpy_s(void *dst, size_t dstsize, const void *src, size_t n); So if your code used to be: if (in_len > dst_len) { /* error */ } memcpy(dst, src, in_len); it becomes: if (memcpy_s(dst, dst_len, src, src_len)) { /* error */ } Or, with truncation, memcpy(dst, src, min(in_len, dst_len)); vs (void)memcpy_s(dst, dst_len, src, src_len); The question: how does an extra length parameter

Forcing GCC to perform loop unswitching of memcpy runtime size checks?

只愿长相守 提交于 2019-12-04 23:44:06
Is there any reliable way to force GCC (or any compiler) to factor out runtime size checks in memcpy() outside of a loop (where that size is not compile-time constant, but constant within that loop), specializing the loop for each relevant size range rather than repeatedly checking the size within it? This is an test case reduced down from a performance regression reported here for an open source library designed for efficient in-memory analysis of large data sets. (The regression happens to be because of one of my commits...) The original code is in Cython, but I've reduced it down to a pure

Go- Copy all common fields between structs

青春壹個敷衍的年華 提交于 2019-12-04 23:11:31
I have a database that stores JSON, and a server that provides an external API to whereby through an HTTP post, values in this database can be changed. The database is used by different processes internally, and as such have a common naming scheme. The keys the customer sees are different, but map 1:1 with the keys in the database (there are unexposed keys). For example: This is in the database: { "bit_size": 8, "secret_key": false } And this is presented to the client: { "num_bits": 8 } The API can change with respect to field names, but the database always has consistent keys. I have named

copy_from/to_user详解

落花浮王杯 提交于 2019-12-04 22:23:05
参考:http://www.wowotech.net/memory_management/454.html 宋大侠的文章精彩,郭大侠的评论也精彩。 结论简单摘录如下: 无论是内核态还是用户态访问合法的用户空间地址,当虚拟地址并未建立物理地址的映射关系的时候,page fault的流程几乎一样,都会帮助我们申请物理内存并创建映射关系。所以这种情况下memcpy()和copy_{to,from}_user()是类似的。 当内核态访问非法用户空间地址的时候,通过 .fixup 和 __ex_table 两个段的帮助尝试修复异常。这种修复异常并不是建立地址映射关系,而是修改do_page_fault()返回地址。memcpy()由于没有创建这样的段,所以memcpy()无法做到这点。 在使能 CONFIG_ARM64_SW_TTBR0_PAN 或者 CONFIG_ARM64_PAN (硬件支持的情况下才有效)的时候,即使能禁止内核态访问用户态PGD,原理是通过修改TTBR0为一个全0的一级页表(和swap_page_dir一样,通过链接脚本分配占位)。此时我们只能使用copy_{to,from}_user()这种接口,直接使用memcpy()是不行的。 号外:用memory copy取代copy_{to,from}_user()是绝对禁止的,没有地址检查(access_ok)会引起安全问题

ARM64 的 memcpy 优化与实现

梦想的初衷 提交于 2019-12-04 22:21:08
参考:https://www.byteisland.com/arm64-%E7%9A%84-memcpy-%E6%B1%87%E7%BC%96%E5%88%86%E6%9E%90/ libc/string.c 拷贝是一个字节一个字节拷贝,太慢了,现在的系统支持4/8字节拷贝,甚至16字节拷贝,故linux内核针对拷贝做了优化 copy_templat.S, 详见链接文章 来源: https://www.cnblogs.com/zengjianrong/p/11886714.html