So wondering -
if
LVCOLUMNW TableCol; // Make Column struct
then is it better to use
memset(&TableCol, 0, sizeof(TableCol)); // Reset Column
or
memset(&TableCol, 0, sizeof(LVCOLUMNW)); // Reset Column
or
Both are OK to use w/o distinction
?
John Z
ChatGPT says this and I agree.
Both options are technically correct and will have the same effect since sizeof(TableCol) and sizeof(LVCOLUMNW) are equivalent.
However, using sizeof(TableCol) is generally preferred because it directly ties the memset operation to the actual variable you're working with. This approach improves code maintainability. If the type of TableCol ever changes, the code will still work correctly without needing to update the memset call.
So, while both are okay, using sizeof(TableCol) is often considered better practice.
Great - Thanks MrBcx!
I was using TableCol but thought I'd ask to be sure.
Appreciate it!
John Z
I always use this when initializing LVITEMW struct
LVITEMW lvItemFileName;
memset(&lvItemFileName, 0, sizeof(LVITEMW));
...
You can use memset() or LVITEMW lvItemFileName = { 0 }. I prefer memset().
Quote from: WiiLF23 on August 17, 2024, 06:27:33 AM
I always use this when initializing LVITEMW struct
LVITEMW lvItemFileName;
memset(&lvItemFileName, 0, sizeof(LVITEMW));
...
You can use memset() or LVITEMW lvItemFileName = { 0 }. I prefer memset().
For simple datatypes (ints, floats, etc),
={0}; works great.
For
user-defined types, some compilers may issue warnings, in which case we can explicitly
zero each member using, for example, this approach: struct
MyStruct mstr = { 0, 0, 0.0, 0.0 };
={0};
optimizer can generate smaller code, so useful in small code ;)
This is more than sufficient
LVITEMW lvi = { 0 };
Unless you are working with a RECT, you dont need to input extra parameters. memset is more than enough.
I have read that using memset is not always generated by the compiler prefer to use RtlZeroMemory
From Pelle's
Fills an object with a value.
Syntax: void __stosb(unsigned char *dst, unsigned char val, size_t num);
void __stosw(unsigned short *dst, unsigned short val, size_t num);
void __stosd(unsigned long *dst, unsigned long val, size_t num);
void __stosq(unsigned long long *dst, unsigned long long val, size_t num); (X64 only)
Declared in: <intrin.h>
The __stosb, __stosw, __stosd and __stosq function fills the object at dst with num byte, word, double-word, or quad-word values from val using the X86/X64 instruction REP STOSB, REP STOSW, REP STOSD, or REP STOSQ, respectively.
The __stosb, __stosw, __stosd and __stosq functions are only available as intrinsics.
Returns: Nothing
#code
https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlzeromemory
https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlfillmemory
Reading \PellesC\Include\win\winnt.h :
#define RtlZeroMemory(Destination,Length) (void)memset((Destination),0,(Length))
optimizer can also replace memset to rep stosb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int __cdecl main(void)
{
//char b[80] = {0};
char b[80];
memset(b, 63, 80);
b[5] = 0;
printf("size: %d %s\n", sizeof(b), b);
return 0;
}
Vortex, I know that but what I read was that in the form the macro RtlZeroMemory is created the compiler cannot remove or replace it as it can do with memset. If I find the link I post it. memset with its stosb is not very efficient and could be easily replaced with __stosXX macro family
memset(b,63,80) can be replace __stosq((unsigned long long *) b, (unsigned long long) 0x3f3f3f3f, sizeof(b) / sizeof(__int64)) ; just 10 loops rather than 80the compiler sets rcx to 80 / 8, sets 0x3f3f3f3f to rax and creates rep stosq instruction
[0000000140001CDB] 31C0 xor eax,eax
[0000000140001CDD] B907000000 mov ecx,7
[0000000140001CE2] F348AB rep stosq
__stosq((unsigned long long *) &_Lvc,0,sizeof(LVCOLUMN) / 8
Hi HellOfMice,
It works as you described, here is a quick example :
#include <stdio.h>
#include <intrin.h>
int main(void)
{
char p[]={0,1,2,3,4,5,6,7,0,8,9};
__stosq((unsigned long long *)p, 0x4141414141414141,1);
printf("%s\n",p);
return 0;
}
Examining the assembly code :
lea rdi,[rsp+25H]
mov rax,4141414141414141H
mov ecx,1
rep stosq
Vortex you are an expert, not me
What we said can be applied to memset, memcpy...