4
\$\begingroup\$

struct iovec is defined in <uio.h> by the following way:

/* Structure for scatter/gather I/O. */
struct iovec
 {
 void *iov_base; /* Pointer to data. */
 size_t iov_len; /* Length of data. */
 };

Now I do ugly casting from const void* to void*:

int send_frame_once( int fd, const struct iovec* iov, size_t iovlen );
int send_frame( int fd, const void* buffer, size_t len )
{
 struct iovec iov;
 iov.iov_base = (void*)buffer; /* const iov cast */
 iov.iov_len = len;
 return send_frame_once( fd, &iov, 1 );
}

Is that necessary kludge? Or should I remove const keyword from declaration of send_frame function. Like this:

int send_frame( int fd, void* buffer, size_t len )
asked Feb 29, 2012 at 10:09
\$\endgroup\$

1 Answer 1

7
\$\begingroup\$

I think you should keep it. const is not only meant for the compiler, but also for anybody reading your code. Even with the void* cast, you're still saying to readers "Trust me, I'm not touching your buffer!" and this is a valuable information that should not be discarded.

answered Feb 29, 2012 at 10:16
\$\endgroup\$
3
  • 2
    \$\begingroup\$ assuming send_frame does not alter the content of buffer in any way. \$\endgroup\$ Commented Feb 29, 2012 at 14:34
  • \$\begingroup\$ Indeed, this is what I am assuming here. \$\endgroup\$ Commented Feb 29, 2012 at 14:35
  • 1
    \$\begingroup\$ Yep, buffer is not modified. \$\endgroup\$ Commented Mar 1, 2012 at 8:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.