Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > declaring functions moving input pointer

Reply
Thread Tools

declaring functions moving input pointer

 
 
Ilya N. Golubev
Guest
Posts: n/a
 
      08-24-2006
There is already one function processing pointer values this way. And
there may easily appear more, including wrappers for this function.

For purposes of further discussion, consider simplified function,
`consume'. It has one arg, INBUF, that points to a variable that
points to the first character in the input buffer. The function never
modifies characters pointed to by `*INBUF'. It, however, updates
the `*INBUF' value to point to the byte following the last byte
successfully consumed.

We can declare this function

void consume (const char **);

or

void consume (char **);

Which one is appropriate, wrt semantics of `const' as designed in
current C language standards? In any case, applications may have both
`char *' and `const char *' variables, which must be passed and
updated as described above. In any case, the conversions are be
needed to do such a passing, like this.

void consume (const char **);
/* ... */
char *buf;
consume ((const char **) &buf);

Or

void consume (char **);
/* ... */
const char *buf;
consume ((char **) &buf);

Currently, if T is type, any `T*' value may be assigned to `const T*'
one. This is not the case for assigning `T**' values to `const T**'
ones. Why would allowing this be incorrect / dangerous?

Or should we completely avoid declaring functions with `**' args like
these, and specify them like `write' instead? That is, to take `const
char *' arg and return or otherwise pass to caller a `ssize_t' value
saying how much should be added to said `const char *' value before
passing it to the next call.


The function needs to update `size_t' value in addition to `char *'
one anyway, and this should be also the case for its wrappers. So one
might declare `consume' like this.

typedef struct {
const char *p;
size_t s;
} ciov_t;
void consume (ciov_t *IR);

So that is updates both `IR->p' and `IR->s'.

Never seen such a declarations, however. `writev', in particular,
does not use input structures with `const void *' for data to write.
This will even cause trouble for application writers needing to pass
such a data pointers to `writev'. What would be dangerous / incorrect
in declaring functions this way?

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      08-24-2006
Ilya N. Golubev schrieb:
> There is already one function processing pointer values this way. And
> there may easily appear more, including wrappers for this function.
>
> For purposes of further discussion, consider simplified function,
> `consume'. It has one arg, INBUF, that points to a variable that
> points to the first character in the input buffer. The function never
> modifies characters pointed to by `*INBUF'. It, however, updates
> the `*INBUF' value to point to the byte following the last byte
> successfully consumed.
>
> We can declare this function
>
> void consume (const char **);
>
> or
>
> void consume (char **);
>
> Which one is appropriate, wrt semantics of `const' as designed in
> current C language standards? In any case, applications may have both
> `char *' and `const char *' variables, which must be passed and
> updated as described above. In any case, the conversions are be
> needed to do such a passing, like this.
>
> void consume (const char **);
> /* ... */
> char *buf;
> consume ((const char **) &buf);
>
> Or
>
> void consume (char **);
> /* ... */
> const char *buf;
> consume ((char **) &buf);
>
> Currently, if T is type, any `T*' value may be assigned to `const T*'
> one. This is not the case for assigning `T**' values to `const T**'
> ones. Why would allowing this be incorrect / dangerous?


Have a look at the comp.lang.c FAQ, especially
http://c-faq.com/ansi/constmismatch.html

BTW: I'd rather use the "secure" form of "const char **" and have
no cast, i.e.
char *buf;
const char *cbuf;
....
cbuf = buf;
consume(&cbuf);
Avoiding the cast can help you find errors like
consume(cbuf);
which is not possible for
consume((const char **)buf);

> Or should we completely avoid declaring functions with `**' args like
> these, and specify them like `write' instead? That is, to take `const
> char *' arg and return or otherwise pass to caller a `ssize_t' value
> saying how much should be added to said `const char *' value before
> passing it to the next call.


Note that in the context of standard C, there is no "ssize_t" but
there is in POSIX. From the following, I guess that you really mean
size_t.

> The function needs to update `size_t' value in addition to `char *'
> one anyway, and this should be also the case for its wrappers. So one
> might declare `consume' like this.
>
> typedef struct {
> const char *p;
> size_t s;
> } ciov_t;
> void consume (ciov_t *IR);
>
> So that is updates both `IR->p' and `IR->s'.
>
> Never seen such a declarations, however. `writev', in particular,
> does not use input structures with `const void *' for data to write.
> This will even cause trouble for application writers needing to pass
> such a data pointers to `writev'. What would be dangerous / incorrect
> in declaring functions this way?


I am not sure whether I understood you correctly.
Do you want to modify data pointed to by a pointer to const type?
This is most of the time a bad idea; if you can guarantee that
you had a pointer to unqualified type to start with, then
"removing the const" may work. If the data was const qualified
from the start, it may well reside in some sort of non-writable
memory.
Or do you want to read data pointed to by IR->p?

The "updates" throws me off, to be honest.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Proto-Functions: Declaring in one file, using in another? Hal Vaughan C++ 2 03-14-2008 12:34 PM
Free Moving Estimate, Local Movers, Long Distance Moving, PackingSupplies, Storage Rental, Home Moving, Apartment Moving, Office Moving,Commercial Moving linkswanted ASP .Net 0 01-06-2008 04:45 AM
i want to use functions that don't return int, without declaring them G Patel C Programming 16 02-17-2005 11:50 PM
Errors got when declaring member functions to be inline Peng Yu C++ 3 10-25-2004 01:38 AM
declaring strings inside vs. outside of functions darrel ASP .Net 3 08-13-2004 03:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57