Topic: A conformance question


Author: allan_w@my-dejanews.com (Allan W)
Date: Wed, 30 Oct 2002 10:17:41 +0000 (UTC)
Raw View
velco@fadata.bg (Momchil Velikov) wrote
> Is ``do_stuff'' a conformant function ?
>
> void *get_buffer (unsigned int &len);
> void process_buffer (void *buf, const unsigned int &len);
>
> void
> do_stuff ()
> {
>   unsigned int len;
>
>   process_buffer (get_buffer (len), len);
> }

Depends what the procs do.

    void* get_buffer(unsigned int &len) {
        static char privatebuff[100];
        len = sizeof(privatebuff);
        return privatebuff;
    }
    void process_buffer(void*buf, const unsigned int &len) {
        if (len < sizeof(myClass)) throw myClass::Exception();
        myClass *m = new(buf)myClass;
        // ...
        m->~myClass();
    }

This probably doesn't tell you what you wanted to know.
Re-post your question with *A LOT* more specifics (but not
your whole program!)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: velco@fadata.bg (Momchil Velikov)
Date: Thu, 24 Oct 2002 21:52:24 +0000 (UTC)
Raw View
Is ``do_stuff'' a conformant function ?

void *get_buffer (unsigned int &len);
void process_buffer (void *buf, const unsigned int &len);

void
do_stuff ()
{
  unsigned int len;

  process_buffer (get_buffer (len), len);
}

~velco

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: wolof@freemail.hu ("White Wolf")
Date: Fri, 25 Oct 2002 02:15:15 +0000 (UTC)
Raw View
"Momchil Velikov":
> Is ``do_stuff'' a conformant function ?
[SNIP]
>   process_buffer (get_buffer (len), len);

It is not possible to tell from the information you have given.  If
get_buffer _always_ sets len to a valid unsigned integer value then it is.
If there may be a situation when len is untouched and process_buffer tries
to read it: it formats the harddisk on DeathStation 9000++.

What is possible to tell that the design is rather - khm - weak.  There is
an entity called buffer which is not represented properly by any level of
data abstraction.  That and the hack of passing the size by reference to
avoid the effects of undefined argument evaluation order sounds dangerous.
If someone follows the usual rules of interface design in a code review 2
years later... well, I think undefined behavior is on the way.  And I
cannot even easily blame the poor guy: who would think about this hack if
he did not know it is there?

WW


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Nicola.Musatti@ObjectWay.it (Nicola Musatti)
Date: Sat, 26 Oct 2002 22:58:14 +0000 (UTC)
Raw View
Momchil Velikov wrote:

> Is ``do_stuff'' a conformant function ?
>
> void *get_buffer (unsigned int &len);
> void process_buffer (void *buf, const unsigned int &len);
>
> void
> do_stuff ()
> {
>   unsigned int len;
>
>   process_buffer (get_buffer (len), len);
> }

If by conformant you mean is it written in standard C++ the answer is
yes, as far as I can tell; if you mean is it's behaviour defined, the
answer is no, as len has an undefined value. If you change its
definition to, say,

unsigned int len = 256;

then the behaviour is defined, provided the behaviour of get_buffer and
process_buffer is.

Cheers,
Nicola Musatti

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]