1

This is a basic question. But, I think understanding this would be helpful to play with buffers in 'C'. Thanks.

Scenario

Just a sample snippet

char *test_buff = (char *) malloc(512); //allocate 512 bytes in heap memory.
bzero(test_buff, 512); //Reset buffer instead of Junk/garbage.
/* random code here to fill-in the buffer called "test_buff"...*/
...
/* Send data to another host/system [this doesn't matter though]
 * using buffer "test_buff".
 */
 sendto(sock_descriptor, test_buff /*buffer to send data*/, sizeof(*test_buff) ,...,...);  // option-1
/* OR */
 sendto(sock_descriptor, test_buff /*buffer to send data*/, 512 /* actual malloced size*/ ,...,...); // option-2

Question

  1. Which one of the two is better [between option-1/option-2] way of using buffer? I hope option-1 makes sense, as it has length/size of valid data bytes of send buffer or "test_buff" in this case.

Thanks for your time and input.

4

sizeof(*test_buff) is computed at compile time and will evaluate to the size of a single element of test_buff. As test_buff is a buffer of char's, that element size is guaranteed to be 1. If I have to choose between the two options that you give, then only option 2 has useful behaviour.

On the other hand, if there comes a time that you need to change the size of your buffer, then you have to make that change in 3 (or 4 or 5) places and you have to look out for the places where the number 512 is used with a different meaning. For this reason, my preferred way of writing the code is like this:

#define BUFFER_SIZE 512
char *test_buff = (char *) malloc(BUFFER_SIZE); //allocate 512 bytes in heap memory.
bzero(test_buff, BUFFER_SIZE); //Reset buffer instead of Junk/garbage.
/* random code here to fill-in the buffer called "test_buff"...*/
...
/* Send data to another host/system [this doesn't matter though]
 * using buffer "test_buff".
 */
 sendto(sock_descriptor, test_buff /*buffer to send data*/, BUFFER_SIZE /* actual malloced size*/ ,...,...); // option-2a

If you want to send only the used portion of the buffer, you either need to keep explicitly track of that, or if your buffer only holds a single string, you can use strlen(test_buff)+1 (+1 to ensure the terminating '\0' is also transmitted).

  • I missed it while typing this snippet :). I agree, using macro "#define BUFFER_SIZE 512" is a neat way of doing things. Thanks van Ingen Schenau, I appreciate it. – smaven Dec 13 '13 at 18:26
3

In as much as sizeof(*test_buff) will be sizeof(char), i.e. 1, I'd go with the 512.

  • Thanks for your answer, Keith. Just to clarify, I thought sizeof(*test_buff) should point to number of bytes till null termination is reached. I guess, that happens if I had used strlen(test_buff), is it? – smaven Dec 13 '13 at 5:21
  • Yes, assuming that the buffer contains a text string with a null terminator, you could use strlen(test_buff) + 1. I really recommend the 1 so that you send the null as well, although if you precede the send with a length field that is not mandatory. – Keith Dec 13 '13 at 5:29
  • In case it isn't obvious, strlen(test_buff) is very unlikely to return 512. Whoever is receiving the message will need to know what to expect, so someone needs to decide whether you're sending a variable length string or a fixed length buffer. – Simon B Dec 13 '13 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.