skwear skwear - 1 year ago 76
C# Question

Does space have to be allocated to ProcessStartInfo?

I am porting C++ code to C# and I came across this in the C++ code,

memset(&shell, 0, sizeof(shell));
//the properties in shell are set

So now I am using
for the C# code. Do I have to allocate memory for
or is that done for me when I call the
constructor? And just for the sake for satiating my curiosity, is this way of using
in C++ a practice of an unmanaged language or am I understanding something incorrectly?

Thank you.

Answer Source

As you stated, memory it is allocated for you by .Net runtime env when you call constructor of your ProcessStartInfo obj with the keyword new.

This is the core meaning of the expression "managed .Net CLR languages".

The answer for your other question is: yes.

So called "unmanaged" languages as C++, or better all languages that as well as C++ have memory management in charge of developer and compilers of whom don't initialize allocated memory, have, after have instantiated or called object constructor, to initialize memory allocated otherwise its content will be dirty and could cause run time errors.

In your particular case, you are not calling a constructor, because you have a struct or value type variable, so the SHELLEXECUTEINFO shell; statement it is not a simple declaration but it is a real "variable definition", that is this statement causes a memory allocation.

Now, in C++ new allocated memory is always not initialized (instead "managed" languages have always allocated memory initialized) and so it is the developer to always initialize every new variables (value types or objects) when "defined" (that is allocated), and in C++ memset function does this.

So it is correct to think at the use you mentioned of memset in C++ as a best practice.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download