memset vs SecureZeroMemory
-
Hi All, i have a DLL, there is some code as mentioned below:-
char szName[MAX_PATH];
CString csName = "3424234"; // size can be judged only on run time
wcstombs( szName, csName.operator LPCTSTR(), _tcslen( csName.operator LPCTSTR()));Now i want to initialize szName array to 0, to aviod the unexpected behaviour. Now my question is whether i use memset or SecureZeroMemory? Which is better, and why? Any help will be appreciated.
Величие не Бога может быть недооценена.
modified on Tuesday, February 9, 2010 12:39 AM
-
Hi All, i have a DLL, there is some code as mentioned below:-
char szName[MAX_PATH];
CString csName = "3424234"; // size can be judged only on run time
wcstombs( szName, csName.operator LPCTSTR(), _tcslen( csName.operator LPCTSTR()));Now i want to initialize szName array to 0, to aviod the unexpected behaviour. Now my question is whether i use memset or SecureZeroMemory? Which is better, and why? Any help will be appreciated.
Величие не Бога может быть недооценена.
modified on Tuesday, February 9, 2010 12:39 AM
char szName[MAX_PATH] = {0};
-
Hi All, i have a DLL, there is some code as mentioned below:-
char szName[MAX_PATH];
CString csName = "3424234"; // size can be judged only on run time
wcstombs( szName, csName.operator LPCTSTR(), _tcslen( csName.operator LPCTSTR()));Now i want to initialize szName array to 0, to aviod the unexpected behaviour. Now my question is whether i use memset or SecureZeroMemory? Which is better, and why? Any help will be appreciated.
Величие не Бога может быть недооценена.
modified on Tuesday, February 9, 2010 12:39 AM
SecureZeroMemory
may be a little slow on thex86
platform as it initializes each element of the array in a loop. But forx64
it uses an intrinsic, which will be very fast. You could also use the C-Runtimememcpy_s
function. To initialize the array to zero at the time of creation, the reply by Rejeesh definitely works in Visual Studio.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
SecureZeroMemory
may be a little slow on thex86
platform as it initializes each element of the array in a loop. But forx64
it uses an intrinsic, which will be very fast. You could also use the C-Runtimememcpy_s
function. To initialize the array to zero at the time of creation, the reply by Rejeesh definitely works in Visual Studio.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)Thanks for the reply. So you mean in 64 bit processor, SecureZeroMemory will not make much change in time consumption.
Величие не Бога может быть недооценена.
-
char szName[MAX_PATH] = {0};
But i am looking for details regarding SecureZeroMemory and memset or memcpy, any way thanks for your help :)
Величие не Бога может быть недооценена.
-
But i am looking for details regarding SecureZeroMemory and memset or memcpy, any way thanks for your help :)
Величие не Бога может быть недооценена.
I think _superman_ has already sent you a better answer which I think explains the differences. I usually initialize all arrays to zero as I posted before.
-
Thanks for the reply. So you mean in 64 bit processor, SecureZeroMemory will not make much change in time consumption.
Величие не Бога может быть недооценена.
That's correct. Intrinsics are similar to writing inline assembly instructions. x64 does not support inline assembly and so you have to use intrinsics. Read more about intrinsics in the following links - Intrinsic function[^] Compiler Intrinsics[^]
«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
Hi All, i have a DLL, there is some code as mentioned below:-
char szName[MAX_PATH];
CString csName = "3424234"; // size can be judged only on run time
wcstombs( szName, csName.operator LPCTSTR(), _tcslen( csName.operator LPCTSTR()));Now i want to initialize szName array to 0, to aviod the unexpected behaviour. Now my question is whether i use memset or SecureZeroMemory? Which is better, and why? Any help will be appreciated.
Величие не Бога может быть недооценена.
modified on Tuesday, February 9, 2010 12:39 AM
One thing others didn't point out:
memset
is a CRT call, andSecureZeroMemory
is an API call. That's it. I don't think one would be "faster" over the other (practically) to initialize an array of sizeMAX_PATH
. If you had consideredZeroMemory
, there are chances where the compiler can completely optimize out (remove) the call to this function when it detects that the initialised buffer isn't being used anywhere. However,SecureZeroMemory
would ensure that the buffer be initialised, even if it isn't being used at all (the optimiser won't rule this call out).“Follow your bliss.” – Joseph Campbell
-
Hi All, i have a DLL, there is some code as mentioned below:-
char szName[MAX_PATH];
CString csName = "3424234"; // size can be judged only on run time
wcstombs( szName, csName.operator LPCTSTR(), _tcslen( csName.operator LPCTSTR()));Now i want to initialize szName array to 0, to aviod the unexpected behaviour. Now my question is whether i use memset or SecureZeroMemory? Which is better, and why? Any help will be appreciated.
Величие не Бога может быть недооценена.
modified on Tuesday, February 9, 2010 12:39 AM
Adam Roderick J 09 wrote:
Now i want to initialize szName array to 0, to aviod the unexpected behaviour.
1. You should be using the
wcstombs_s()
function. 2. You should use that function as documented here[^] to calculate the required size of your destination buffer, and allocating that appropriately. In your example above, if the length of your string exceedsMAX_PATH
then you have an immediate memory leak. 3. Clearing the buffer to zeroes before copying the string provides no benefit.MVP 2010 - are they mad?
-
SecureZeroMemory
may be a little slow on thex86
platform as it initializes each element of the array in a loop. But forx64
it uses an intrinsic, which will be very fast. You could also use the C-Runtimememcpy_s
function. To initialize the array to zero at the time of creation, the reply by Rejeesh definitely works in Visual Studio.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)(i think you've got your x86 and x64 reversed here)