Copying a struct to dereferenced pointer to a struct.

Hi guys,

I need a quick sanity check, can I copy struct like on the example below?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct MyStruct
{
	int a{ 0 };
	float b{ 0 };
	bool c{ 0 };
};

MyStruct EvaluateResult()
{
  MyStruct result;
  return result;
}

bool MyFunction(MyStruct* result)
{
  *result = EvaluateResult(); // Is it OK to copy struct that way?
  result->c = false;
}


So my thinking is that I am dereferencing pointer to struct and do implicit copy assignment. Is this correct?

The thing is that I have a C# code that is consuming MyFunction (from DLL) and sometimes result->c is returned as true on the C# side. So I am starting to think that maybe I am messing up with the pointer by doing *result = EvaluateResult(), because if I comment out this line, result->c is always false.

Thanks for helping me.
Last edited on
So my thinking is that I am dereferencing pointer to struct and do implicit copy assignment. Is this correct?
Yes.

Maybe the problem in c# is the it has another layout regarding padding and what not. See:

https://stackoverflow.com/questions/10838825/convert-c-struct-to-c-sharp
Thanks for your input. Indeed the problem was in C# marshaling, it treats bool as 4 bytes by default instead of 1.
Topic archived. No new replies allowed.