Live data from Hacker News

Stop Memsetting Structures

anmolsarma.in

51–60 of 277 posts

Re: Stop Memsetting Structures

#51
post #37

Am I the only one who finds this int yes=1; setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (yes)) ; much cleaner than this setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &(int) {1}, sizeof(int)); ?

I agree anything is better than a magic number, but really only if the variable is properly descriptive. What are we saying 'yes' to?

Whatever you pass the yes to.

Re: Stop Memsetting Structures

#52

Earlier quoted context omitted.

> Sending structures that include padding or that you don’t know the exact layout of over the network is another thing you shouldn’t do, period. What if you do know the exact layout and it happens to have a gap between fields?

Tbh I'm not a network person, but my naive brain says instead of casting to char* and writing to the buffer, couldn't you just write out individual fields to a buffer so you control every byte? That seems smarter and safer.

Do you write a separate serialization function for every single struct and sub-struct you send over a network? And then continue maintaining every single one to keep it consistent whenever a field is added/removed/modified in any struct?

(Though I should mention that macros actually do make this doable, but they're hardly pretty, and I have yet to see many people embrace this approach, if they realize it's possible at all.)

Re: Stop Memsetting Structures

#53
post #26

If you only use super simple structures maybe. It is quite common to zero out a structure and use only parts of it. It is also quite common to have to pragmatically fill in the structure.

> It is quite common to zero out a structure and use only parts of it.

That point is addressed in TFA.

Re: Stop Memsetting Structures

#54

Earlier quoted context omitted.

> Sending structures that include padding or that you don’t know the exact layout of over the network is another thing you shouldn’t do, period. What if you do know the exact layout and it happens to have a gap between fields?

Tbh I'm not a network person, but my naive brain says instead of casting to char* and writing to the buffer, couldn't you just write out individual fields to a buffer so you control every byte? That seems smarter and safer.

It depends. It was common case for years in games to just cast your networked data to an array and send over the network. Nowadays it's much more in fashion to pick a serialization library to sit in between, as PCs are faster. But one might choose not to depending on the problem space. Embedded devices might not have the spare cycles or the space for a full serialization library.

So...you're not wrong, but it's basically the same reason people use C over something with more ergonomics.

Edit: the last time I wrote a networking library, I also just cast to raw structures, mainly because it was less work (didn't want to integrate a serialization library on both sides of the link). This was fine for a while (and very fast), but also hid some bugs in the protocol, and made sending arrays of data very ugly. Eventually we put protobuf on top, and things ended up much nicer.

Re: Stop Memsetting Structures

#55
post #37

Am I the only one who finds this int yes=1; setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (yes)) ; much cleaner than this setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &(int) {1}, sizeof(int)); ?

I agree anything is better than a magic number, but really only if the variable is properly descriptive. What are we saying 'yes' to?

> *What are we saying 'yes' to?

The flag SO_REUSEADDR (SO = Socket Option); really, I'd say we're setting the flag / setting the flag to true, but true is a bit of a reserved word, which I'm guessing is why they went with "yes".

Re: Stop Memsetting Structures

#56

Because it’s 2019! It really irritates me when people give arguments like this. I seriously don't care what year it is, I care that the code I write works. A lot of projects still use C89 because they want the widest portability. C99 isn't very new, but especially in the embedded and other niche industries, C89 (often with some extensions) is all you get. That said, "= { 0 };" is almost always usable instead of a mem…

> but especially in the embedded and other niche industries, C89 (often with some extensions) is all you get.

Embedded and niche industries are forcing their programmers to work with 30 year old tooling? (Instead of 20 year tooling?)

I think at some point, writing yourself a transpiler from C99 to C89 becomes profitable… (The JS community did it for a lot less than that w/ babel…)

Re: Stop Memsetting Structures

#58
post #54

Earlier quoted context omitted.

Tbh I'm not a network person, but my naive brain says instead of casting to char* and writing to the buffer, couldn't you just write out individual fields to a buffer so you control every byte? That seems smarter and safer.

It depends. It was common case for years in games to just cast your networked data to an array and send over the network. Nowadays it's much more in fashion to pick a serialization library to sit in between, as PCs are faster. But one might choose not to depending on the problem space. Embedded devices might not have the spare cycles or the space for a full serialization library. So...you're not wrong, but it's basic…

How does sending raw memory to the socket work if you have a struct filled with pointers?

Re: Stop Memsetting Structures

#59
post #37

Earlier quoted context omitted.

I agree anything is better than a magic number, but really only if the variable is properly descriptive. What are we saying 'yes' to?

Definitely agree that calling it `reuse_local` makes more sense, but OP was just quoting the blogpost.

And the blog post was just quoting Beej.

Re: Stop Memsetting Structures

#60
post #54

Earlier quoted context omitted.

It depends. It was common case for years in games to just cast your networked data to an array and send over the network. Nowadays it's much more in fashion to pick a serialization library to sit in between, as PCs are faster. But one might choose not to depending on the problem space. Embedded devices might not have the spare cycles or the space for a full serialization library. So...you're not wrong, but it's basic…

How does sending raw memory to the socket work if you have a struct filled with pointers?

I think they simply don't do that with structs that contain pointers?
Post reply on HN