I have code in C++, which I will call from C# via interop mechanism.
In my C++ code, this is my class structure
extern "C" struct __declspec(dllexport) LegList
{
SimpleList simple_List;
int* inputList;
};
This is my C# code:
[StructLayout(LayoutKind.Sequential)]
public struct LegList
{
SimpleList simple_List;
public IntPtr inputList;
}
The inputList
is not used anywhere in C#.
My questions are:
inputList=0
, or inputList=NULL
, or inputList=(int*)malloc(0)
?AccessViolationException
.inputList=(int*)malloc(0)
, or if I don't set it at all, will the program crash at a different place at a later time, in a very unpredictable and inconsistent manner, depending on which machine the code is running on?Will assigning a pointer to (int*)malloc(0)
causes unpredictable crash? How should I properly set an unused pointer?
Simply set it to NULL
or, if your compiler supports it, to nullptr
. This makes it clear that it doesn't point to anything useful.
So either inputList=NULL
or inputList=nullptr
.
Obviously, not setting it at all, is the worst thing to do. If there is any code that uses it, it will behave unpredictably.
I'm not sure, why one would want to do inputList=(int*)malloc(0)
.