News for hackers. Stuff that matters.
Re:Tell HN: C Experts Panel – Ask us anything about C by ATsch on Tuesday April 14, 2020 @12:27PM (#22867908)
typedef struct {uint8_t *data; size_t len;} ByteBuf; is the first line of code I write in a C project.
Re:Tell HN: C Experts Panel – Ask us anything about C by mobilemidget on Wednesday April 15, 2020 @04:05AM (#22875565)
Could you add some extra information why this is so helpful or handy to have? Think it will benefit readers that are starting out with C etc.
Re:Tell HN: C Experts Panel – Ask us anything about C by saagarjha on Wednesday April 15, 2020 @04:51AM (#22875749)
In C, dynamically-sized vectors don’t carry around size information with them, often leading to bugs. This struct attempts to keep the two together.
Re:Tell HN: C Experts Panel – Ask us anything about C by GoblinSlayer on Wednesday April 15, 2020 @07:42AM (#22876603)
Memory corruption in sudo password feedback code happened because length and pointer sit as unrelated variables and have to be manipulated by two separate statements every time like some kind of manually inlined function. For comparison putty slice API handles slice as a whole object in a single statement keeping length and pointer consistent.
Re:Tell HN: C Experts Panel – Ask us anything about C by kkdwivedi on Tuesday April 14, 2020 @04:34PM (#22871107)
Another option is a struct with a FAM at the end.

  typedef struct {
      size_t len;
      uint8_t data[];
  } ByteBuf;
Then, allocation becomes

  ByteBuf *b = malloc(sizeof(*b) + sizeof(uint8_t) * array_size);
  b->len = array_size;
and data is no longer a pointer.
Re:Tell HN: C Experts Panel – Ask us anything about C by ATsch on Tuesday April 14, 2020 @05:22PM (#22871623)
Well, your ByteBuf is still a pointer. You also now need to dereference it to get the length. It also can't be passed by value, since it's very big. You can also not have multiple ByteBufs pointing at subsections of the same region of memory.

Thing is, you rarely want to share just a buffer anyway. You probably have additional state, locks, etc. So what I do is embed my ByteBuf directly into another structure, which then owns it completely:

    typedef struct {
        ...
        ByteBuf mybuffer;
        ...
    } SomeThing;
So we end up with the same amount of pointers (1), but with some unique advantages.
Re:Tell HN: C Experts Panel – Ask us anything about C by kkdwivedi on Tuesday April 14, 2020 @05:53PM (#22871965)
Right, totally depends on what you're doing. My example is not a good fit for intrusive use cases.
Re:Tell HN: C Experts Panel – Ask us anything about C by saagarjha on Tuesday April 14, 2020 @05:49PM (#22871908)
sizeof(ByteBuf) == sizeof(size_t), and you can pass it by value; I just don't think you can do anything useful with it because it'll chop off the data.
Re:Tell HN: C Experts Panel – Ask us anything about C by kevin_thibedeau on Tuesday April 14, 2020 @06:31PM (#22872283)
This will an alignment problem on any platform with data types larger than size_t. You'd need an alignas(max_align_t) on the struct. At which point some people are going to be unhappy about the wasteful padding on a memory constrained target.
Re:Tell HN: C Experts Panel – Ask us anything about C by benibela on Wednesday April 15, 2020 @10:18AM (#22877958)
Why not typedef struct {uint8_t *data, dataend} ?

Makes it easier to take subranges out of it

Re:Tell HN: C Experts Panel – Ask us anything about C by orenht on Wednesday April 15, 2020 @10:34AM (#22878176)
should be

  typedef struct {uint8_t *data, *dataend} 
if I'm not mistaken :)
Re:Tell HN: C Experts Panel – Ask us anything about C by gkfasdfasdf on Wednesday April 15, 2020 @11:07AM (#22878570)
What are the advantages of saving the end as a pointer? Genuinely curious. Seems like a length allows the end pointer to be quickly calculated (data + len), while being more useful for comparisons, etc.
Re:Tell HN: C Experts Panel – Ask us anything about C by benibela on Wednesday April 15, 2020 @03:32PM (#22882007)
You can remove the first k elements of a view with data += k.

With the length you would need to do data += k; length -= k

Especially if you want to use it as safe iterator, you can do data++ in a loop

Re:Tell HN: C Experts Panel – Ask us anything about C by zoomablemind on Thursday April 16, 2020 @05:33PM (#22893804)
> ...You can remove the first k elements of a view with data += k.

How would you safely free(data) afterwards? You'd need to keep an alloc'ed pointer somehow.

Re:Tell HN: C Experts Panel – Ask us anything about C by gkfasdfasdf on Thursday April 16, 2020 @09:09AM (#22888954)
Got it. That is really neat, going to add to my bag of tricks...
Re:Tell HN: C Experts Panel – Ask us anything about C by benibela on Wednesday April 15, 2020 @06:03PM (#22883630)
Right. I always think the pointer declaration is part of the type. (that is why I do not use C. Is there really a good reason for this C syntax?)
Re:Tell HN: C Experts Panel – Ask us anything about C by enriquto on Tuesday April 14, 2020 @04:10PM (#22870854)
That's a really bizarre layout for your struct. Why don't you put the length first?
Re:Tell HN: C Experts Panel – Ask us anything about C by twic on Tuesday April 14, 2020 @04:21PM (#22870982)
Why would it matter? The bytes aren't inline, this is just a struct with two word-sized fields.

A possible tiny advantage for this layout is that a pointer to this struct can be used as a pointer to a pointer-to-bytes, without having to adjust it. Although i'm not sure that's not undefined behaviour.

Re:Tell HN: C Experts Panel – Ask us anything about C by epr on Wednesday April 15, 2020 @09:30AM (#22877447)
I don't think that's undefined behavior. That's how C's limited form of polymorphism is utilized. For example, many data structures behind dynamic languages are implemented in this way. A concrete example would be Python's PyObject which share PyObject_HEAD.

https://github.com/python/cpython/blob/master/Include/object...

Re:Tell HN: C Experts Panel – Ask us anything about C by ATsch on Tuesday April 14, 2020 @04:17PM (#22870919)
I'm not sure if it matters. It might be better for some technical reason, such as speeding up double dereferences, because you don't need to add anything to get to the pointer. But to be honest I just copied it out of existing code.
Re:Tell HN: C Experts Panel – Ask us anything about C by saagarjha on Tuesday April 14, 2020 @05:49PM (#22871917)
Most platforms have instructions for dereferencing with a displacement.