-1

I'm not sure if this is this right place to ask this question, so please migrate this post to a correct place if it suits better.

I was really wondering what is a correct name for this.

char buf[SIZE + XX]

buf will contain meaningful data up to SIZE but some routines will write over SIZE so I should provide a buffer XX for the overflow.

What do you think is a good name for XX?

xiver77
  • 2,162
  • 1
  • 2
  • 12
  • Any understandable name would fit: `safetySpan`, `overflowProofPadding`, ... Although I suspect there might be something off in your design if you need to account for overflow and this should be the concern rather than that (e.g. using `snprintf` instead of `sprintf`) – joH1 Jan 27 '22 at 16:05
  • @joH1 I'm asking for the name for `XX`. The buffer is aligned to cache (64 bytes), but some routines write in a multiple of 7, so an overflow is unavoidable if I don't put a check within the write, which is unacceptable in this case. – xiver77 Jan 27 '22 at 16:07
  • @ikegami Please see the comment above. – xiver77 Jan 27 '22 at 16:07
  • Generally, when some data has a fixed part and a variable part after it, they are often named "header" and "data" or some flavour of that. – Lundin Jan 27 '22 at 16:13
  • This is not "opinion based". An answer can be backed up with concrete examples. – ikegami Jan 27 '22 at 16:51

1 Answers1

2

It's called padding.


Take for example

struct Struct {
   int i;
   char ch;
}

On a particular system with sizeof( int ) of 4, sizeof( struct Struct ) might be 8. That's because 3 bytes of padding are added (on that system) at the end to ensure that each element of arrays of this struct are properly aligned.


Take for example encryption protocols. It's common for them to work with fixed-size blocks. The modern standard for symmetric encryption is such a protocol. AES can only encode and decode 128 bit blocks. So what if the data you want to send isn't a multiple of 128 bits? Padding must be used.


In the first example, padding refers to extra space required to meet meta/sizing constraints rather than for storing data.

In the second example, padding refers to extra data required to meet meta/sizing constraints rather than for carrying information.

In both examples, padding refers to something extra added solely to meet meta/sizing constraints. And this is what you are adding.


Finally, things it's not. "Overflow" refers to insufficient space. "Safety" implies guesswork or uncertainty. Neither term is appropriate here.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • I think you misunderstood what the OP is asking for. There's no structs and so there's no case for padding either. Flexible array members might be the solution they are looking for though, I suppose. – Lundin Jan 27 '22 at 16:15
  • 1
    @Lundin, Their code is doing the is doing exactly the same thing the C compiler is doing for this struct: Adding space because of alignment considerations. – ikegami Jan 27 '22 at 16:15
  • The term _padding_ should be used exclusively when compensating for alignment requirements though. Swap the order of your members and the padding will change place - no longer at the end. – Lundin Jan 27 '22 at 16:16
  • 1
    @Lundin, Re "*The term padding should be used exclusively when compensating for alignment requirements though.*", That's the case here. See the comments on the question. – ikegami Jan 27 '22 at 16:17
  • Hmm okay I didn't see that, fair enough. – Lundin Jan 27 '22 at 16:18
  • @Lundin, ok, it's not exactly alignment, but fixed word/read size. But padding would be used for that too – ikegami Jan 27 '22 at 16:19
  • I do like the name *padding*, but I'm still thinking because *padding* somehow feels like I'm filling stuff to match a certain point, while in this case I'm just providing a safety zone.. maybe I should use safety_zone? – xiver77 Jan 27 '22 at 16:21
  • No, safety zone implies you're writing really bad code. There should be no guessing involved, so adding extra "just to be safe" in inherently wrong. – ikegami Jan 27 '22 at 16:21
  • The *padding* is certainly planned and calculated, I'm optimizing tightly so I cannot provide more than necessary anyway. – xiver77 Jan 27 '22 at 16:26