Page 1 of 1
When to use memset on objects?
Posted: Tue 26. Mar 2019, 15:36
by ValeV
I see in PDF guide that in some examples function memset() is used (ie. in example of SvcDiagnosticSessionControl() function, page 278), while in other examples it doesn't (ie. in example of UDSWrite(), page 266).
Maybe this is more of a C++ question than, but is there a reason why sometimes memset is not used, and sometimes it is? When should I use it?
Sincerely,
ValeV
Re: When to use memset on objects?
Posted: Tue 26. Mar 2019, 16:26
by K.Wagner
Hello,
in C++ it is generally recommended to do an initialization of variables and buffers used (data structures, arrays, etc), specially when those are used as in parameters in function API calls. This is to avoid using illegal data, i.e. assuming wrong values as input. It is a safety measure.
Note that C++ doesn't initialize variables automatically as other programming languages like C# (in C#, bool variables are initialized as false; int variables are initialized with 0, and so on). In C++, non initialized variables just stay with arbitrary data, for instance, values found in the memory address where the variable is allocated.
ValeV wrote:I see in PDF guide that in some examples function memset() is used (ie. in example of SvcDiagnosticSessionControl() function, page 278), while in other examples it doesn't (ie. in example of UDSWrite(), page 266).
The example of the page 266 shows the use of UDS_Write. Since the message structure is not initialized, its Data field stays with arbitrary data, which looks better when seeing the data being sent in the log files. Nevertheless, since this is an example, it also should use variable initialization, and then use a loop to fill the Data to be sent (4095 bytes). We will change this to avoid future misunderstandings. Thanks for binging this to our attention.
Re: When to use memset on objects?
Posted: Wed 27. Mar 2019, 12:32
by ValeV
Thank you for kind explanation. Happy to help.