Moving a block of data around -- memcpy or memmove? [duplicate] - c

This question already has answers here:
What is the difference between memmove and memcpy?
(9 answers)
Closed 4 years ago.
As implied in the title, I wanted to move a block of data around in memory, should memcpy or memmove be utilized? For this particular application, does it make a difference?

I believe the difference is that memmove puts the text in a buffer before trying to write, and memcpy doesn't. Thus, memcpy is faster if the place you're moving the block to doesn't overlap with where it was to start with, while memmove is safer if you are going to overlap.
See this question for more details on the distinction.

Related

Why doesn't malloc initialize allocated memory to zero? [duplicate]

This question already has answers here:
Difference between malloc and calloc?
(14 answers)
Closed 1 year ago.
From Wikipedia:
malloc() does not initialize the memory allocated, while calloc()
guarantees that all bytes of the allocated memory block have been
initialized to 0.
I understand that malloc doesn't initialize the memory to anything, but I have to wonder, why is this the case? Was there a specific reason behind this design decision? It seems like a bit of an antipattern to use calloc(sizeof(T), 1) in my humble opinion and I want to understand the intention.
Because we really don't want to waste all that CPU. It's sooo expensive.
Yes really. You're going to overwrite it anyway.

Is free/=NULL enough to clear memory, or should SecureZeroMemory be placed immediately before it? [duplicate]

This question already has answers here:
When should SecureZeroMemory() be used?
(2 answers)
Is free() zeroing out memory?
(7 answers)
Why doesn't free() zero out the memory prior to releasing it?
(17 answers)
Does using SecureZeroMemory() really help to make the application more secure?
(5 answers)
Closed 5 years ago.
What's the best way to clear out this allocated memory?
Is free/=NULL all that's needed
Does SecureZeroMemory before doing a free/=NULL add to the security of the code?
Or, is adding SecureZeroMemory overkill?
Here's my code:
DWORD tLen = 128;
BYTE *pbData = (BYTE *)malloc(tLen);
memcpy(pbData, chBuffer, tLen);
// ...work done here...
// Clear it
SecureZeroMemory(pbData, tLen);
free(pbData);pbData=NULL;
Thanks!
EDIT: This question is not a duplicate of the question some people have said it is. It is not asking when to use SecureZeroMemory, but the best practice when used with free/=NULL.
It depends what your program is doing. If someone else can look at a buffer of freed memory, is that a concern to you? If the memory contains bank account details, I'd say that it is. If it contains settings for a video game, maybe not (depending on how determined your users are to cheat).
Bit generally it doesn't do any harm to shred memory before freeing it.

How do we normalize a pointer? [duplicate]

This question already has answers here:
what is meant by normalization in huge pointers
(3 answers)
Closed 9 years ago.
Actually i was studying about huge and far pointers.
I come to know huge pointers are by default in normalized form.
I want to know how can we normalize any pointer?
The huge pointer and far pointer are old concept which live in 16-bit
DOS time. You can search something about DOS programming for more
detail about them.
In 8086 programming (MS DOS), a far pointer is normalized if its offset part is between 0 and 15 (0xF).

Why is malloc()'s pool called a "heap"? [duplicate]

This question already has answers here:
Why are two different concepts both called "heap"? [duplicate]
(9 answers)
What is a Memory Heap?
(8 answers)
Closed 9 years ago.
Can anyone explain why the pool of memory managed by malloc() / free() is called a heap?
Based on [1]: http://www.google.com/url?q=http://gee.cs.oswego.edu/dl/html/malloc.html&sa=D&sntz=1&usg=AFQjCNHaQLotbBKKwYqxiiYWN1146BWzFw "Doug Lea's explanation of how his malloc() works",
it's not obvious that the data structure which we call a "heap" is being used at all.
Do we call it a "heap" because it's common for malloc() implementations to use best-fit selection of the memory chunk to return, and that's historically been implemented using a min-heap of chunks, sorted by chunk size?

If a Thrust device_vector's .begin() iterator is stored, does it still point to the correct position after a resize()? [duplicate]

This question already has answers here:
Iterator invalidation rules for C++ containers
(6 answers)
Closed 8 years ago.
For instance:
thrust::device_vector<float> vec(...);
thrust::device_vector<float>::iterator i = vec.begin();
vec.resize(...); // vec may get reallocated and moved in memory here
Now, has vec.begin() also updated and still point validly to the start of vec?
Upon resizing, if the vector originally did not have enough space, your iterator will be invalidated, so you'd have to recall vec.begin() to get a new, valid iterator.

Resources