When to use memset on objects?

A free API for the communication with control devices according to UDS (ISO 14229-1)
Post Reply
ValeV

When to use memset on objects?

Post by ValeV » Tue 26. Mar 2019, 15:36

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

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: When to use memset on objects?

Post by K.Wagner » Tue 26. Mar 2019, 16:26

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.
Best regards,
Keneth

ValeV

Re: When to use memset on objects?

Post by ValeV » Wed 27. Mar 2019, 12:32

Thank you for kind explanation. Happy to help.

Post Reply