memcpy

Please look into this inexplicable behavior and output of memcpy() for overlapping memory blocks

感情迁移 提交于 2019-11-28 12:58:06
问题 After reading the following about memcpy() , I proceeded to read about memmove() : To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at least num bytes, and should not overlap (for overlapping memory blocks, memmove is a safer approach). (LINK) And after checking the program used to illustrate the working of memmove() I decided to tweak it by using memcpy() instead to see how different is the output.To my surprise,they are the same even

Is memcpy of a pointer the same as assignment?

心已入冬 提交于 2019-11-28 10:39:14
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 things up: Is this program valid in all cases? The only interesting part is in the "pa1 == pb" branch.

memcpy(), what should the value of the size parameter be?

删除回忆录丶 提交于 2019-11-28 09:04:21
I want to copy an int array to another int array. They use the same define for length so they'll always be of the same length. What are the pros/cons of the following two alternatives of the size parameter to memcpy()? memcpy(dst, src, ARRAY_LENGTH*sizeof(int)); or memcpy(dst, src, sizeof(dst)); Will the second option always work? Regardless of the content? One thing that favors the last one is that if the array were to change, it'll be some house-keeping to update the memcpy()'s. Thanks Timbo As long as dst is declared as an array with a size, sizeof will return the size of that array in

Is it undefined behaviour to memcpy from an uninitialized variable?

走远了吗. 提交于 2019-11-28 06:58:18
问题 Is using an uninitialized variable as the src for memcpy undefined behaviour in C? void foo(int *to) { int from; memcpy(to, &from, sizeof(from)); } 回答1: The C committee proposed response to defect report 451: instability of uninitialized automatic variables is: The answer to question 3 is that library functions will exhibit undefined behavior when used on indeterminate values. The question in the defect had sought an exemption for memcpy and fwrite if this was indeed the case saying: [...]

Memcpy() in secure programming?

◇◆丶佛笑我妖孽 提交于 2019-11-28 06:57:37
I recently stumbled across an article that claims Microsoft is banning the memcpy() function in its secure programming shops. I understand the vulnerabilities inherent in the function, but is it necessary to ban its use entirely? Should programs I write be avoiding memcpy() entirely, or just ensuring that it's used safely? What alternatives exist that provide similar but safer functionalilty? Microsoft provides alternatives to memcpy and wmemcpy that validate their parameters. memcpy_s says, "Hmm, before I read from this address, let me verify for myself that it is not a null pointer; and

kfifo

只愿长相守 提交于 2019-11-28 04:13:10
kfifo 的一些伪代码 kfifo_len() out = LOAD fifo->out smp_rmb() len = LOAD fifo->in - out kfifo_in() kfifo_out() kfifo_len() kfifo_len() smp_mb() smp_rmb() off = LOAD fifo->in + off off = LOAD fifo->out + off /* memcpy */ /* memcpy */ smp_wmb() smp_mb() STORE fifo->in += off STORE fifo->out += off kfifo_in 只修改 fifo->in 的值,含一个 STORE 指令,及若干 fifo->out fifo->in 的 LOAD 指令 kfifo_out 相反,只修改 kfifo->out 的值,同样含一个 STORE 指令及若干 LOAD 指令 把代码中的内存屏障去掉 kfifo_len() out = LOAD fifo->out /* smp_rmb() */ len = LOAD fifo->in - out kfifo_in() kfifo_out() kfifo_len() kfifo_len() /* smp_mb() */ /* smp_rmb() */ off = LOAD fifo-

Understanding the implementation of memcpy()

半城伤御伤魂 提交于 2019-11-28 02:58:33
问题 I was looking the implementation of memcpy.c, I found a different memcpy code. I couldnt understand why do they do (((ADDRESS) s) | ((ADDRESS) d) | c) & (sizeof(UINT) - 1) #if !defined(__MACHDEP_MEMFUNC) #ifdef _MSC_VER #pragma function(memcpy) #undef __MEMFUNC_ARE_INLINED #endif #if !defined(__MEMFUNC_ARE_INLINED) /* Copy C bytes from S to D. * Only works if non-overlapping, or if D < S. */ EXTERN_C void * __cdecl memcpy(void *d, const void *s, size_t c) { if ((((ADDRESS) s) | ((ADDRESS) d)

Meaning of overlapping when using memcpy

和自甴很熟 提交于 2019-11-28 02:13:16
I am trying to understand the function memcpy() which is defined in the C library <string.h> Syntax: void *memcpy(void*dst,const void*src,size_t n); I know that this function is used to copy the contents of the memory pointed by pointer src to the location pointed by the dst pointer and return a address pointed by dst pointer. I am not able to understand the following important statement regarding memcpy() : When using memcpy() , memory address should not overlap, if it overlaps then the memcpy() is undefined. Another query is: Is the value passed to third argument of the function i.e size_t n

Dereferencing an out of bound pointer that contains the address of an object (array of array)

浪子不回头ぞ 提交于 2019-11-28 01:46:45
Is the following well defined, for different values of REF ? #include <stdio.h> #define REF 1 #define S 1 int main(void) { int a[2][S] = {{1},{2}}; int *q = REF ? a[1] : 0; int *p = a[0] + S; memcpy (&q, &p, sizeof q); printf ("q[0] = %d\n", q[0]); return 0; } Note that p points to the after the last element of a[0] , not to an element in the array a[0] , hence not dereferenceable. But the address stored in p is the address of a[1][0] . p semantically (intentionally?) points "to" (well, out of) a[0] but physically points into a[1] . Can a copy of the bit pattern of a pointer point semantically

Getting GCC to compile without inserting call to memcpy

坚强是说给别人听的谎言 提交于 2019-11-27 23:42:28
I'm currently using GCC 4.5.3, compiled for PowerPC 440, and am compiling some code that doesn't require libc. I don't have any direct calls to memcpy(), but the compiler seems to be inserting one during the build. There are linker options like -nostdlib, -nostartfiles, -nodefaultlibs but I'm unable to use them as I'm not doing the linking phase. I'm only compiling. With something like this: $ powerpc-440-eabi-gcc -O2 -g -c -o output.o input.c If I check the output.o with nm, I see a reference to memcpy: $ powerpc-440-eabi-nm output.o | grep memcpy U memcpy $ The GCC man page makes it clear