Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
May 17, 2009 Comparing D structs | ||||
---|---|---|---|---|
| ||||
Structs can't easily be compared in C because of potential 'padding' inside the struct which may (or may not) exist. I was jut wondering if D somehow gets round this, and allows something like memcmp to easily compare two structs. |
May 17, 2009 Re: Comparing D structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dan | Dan wrote:
> Structs can't easily be compared in C because of potential 'padding' inside the struct which may (or may not) exist.
>
> I was jut wondering if D somehow gets round this, and allows something like memcmp to easily compare two structs.
How about using the == operator?
|
May 17, 2009 Re: Comparing D structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dan | On Sun, May 17, 2009 at 3:55 PM, Dan <twinbee42@skytopia.com> wrote:
> Structs can't easily be compared in C because of potential 'padding' inside the struct which may (or may not) exist.
>
> I was jut wondering if D somehow gets round this, and allows something like memcmp to easily compare two structs.
As grauzone said, you just use == to compare them. D solves the issue with C's uninitialized holes by always initializing structs. Therefore the padding holes are always filled with 0s.
|
May 17, 2009 Re: Comparing D structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | That sounds great, and seems like yet another reason for me to switch to D (other than the removal of header files which always seemed like a kludge).
Just for the record though, I think one can initialize/blank/calloc C structs too, but the problem is that some struct elements (of the same array) may have padding while others don't, or perhaps they have different size paddings.
Dan
Jarrett Billingsley Wrote:
> On Sun, May 17, 2009 at 3:55 PM, Dan <twinbee42@skytopia.com> wrote:
> > Structs can't easily be compared in C because of potential 'padding' inside the struct which may (or may not) exist.
> >
> > I was jut wondering if D somehow gets round this, and allows something like memcmp to easily compare two structs.
>
> As grauzone said, you just use == to compare them. D solves the issue with C's uninitialized holes by always initializing structs. Therefore the padding holes are always filled with 0s.
|
May 17, 2009 Re: Comparing D structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dan | On Sun, May 17, 2009 at 5:00 PM, Dan <twinbee42@skytopia.com> wrote:
> That sounds great, and seems like yet another reason for me to switch to D (other than the removal of header files which always seemed like a kludge).
>
> Just for the record though, I think one can initialize/blank/calloc C structs too, but the problem is that some struct elements (of the same array) may have padding while others don't, or perhaps they have different size paddings.
Sure, you can calloc or memset a C struct. I've never heard of padding varying for different instances of the same struct type, but who knows. So much of C is implementation-defined..
|
May 17, 2009 Re: Comparing D structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dan | Dan wrote: > That sounds great, and seems like yet another reason for me to switch to D (other than the removal of header files which always seemed like a kludge). I heard that the compiler can change the padding bytes to non-null on some occasions. For example, the compiler could treat member a as 32 bit value when assigning it: struct { char a; //offset 0 int b; //offset 4 } Nothing in the C standard says that the compiler has to preserve the padding bytes between a and b. So I was told. > Just for the record though, I think one can initialize/blank/calloc C structs too, but the problem is that some struct elements (of the same array) may have padding while others don't, or perhaps they have different size paddings. Not sure what you mean. |
May 17, 2009 Re: Comparing D structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley Wrote:
> On Sun, May 17, 2009 at 5:00 PM, Dan <twinbee42@skytopia.com> wrote:
> > That sounds great, and seems like yet another reason for me to switch to D (other than the removal of header files which always seemed like a kludge).
> >
> > Just for the record though, I think one can initialize/blank/calloc C structs too, but the problem is that some struct elements (of the same array) may have padding while others don't, or perhaps they have different size paddings.
>
> Sure, you can calloc or memset a C struct. I've never heard of padding varying for different instances of the same struct type, but who knows. So much of C is implementation-defined..
Perhaps I'm mistaken there. However, going through about 5-10 results on Google, it would seem there's no real solution in C to compare structs of the same type, because of whatever issues the padding thing may do. If it was as simple as memset/calloc, I'm sure that would've been mentioned.
|
Copyright © 1999-2021 by the D Language Foundation