is this defined behaviour in C++?

Discussion in 'C++' started by Gianni Mariani, Jul 29, 2004.

  1. Note that F() takes the address of a 0 size vector. Should the code
    below be considered "bad" ? STLPort complains about it but gcc and
    VC++7.1 eat it fine.


    #include <string>
    #include <iostream>
    #include <vector>

    struct foo
    {
    foo( char * ix, size_t is )
    : x( ix ), s( is )
    {
    }

    char * x;
    size_t s;
    };

    std::vector<char> v;

    foo F()
    {
    return foo( & v[0], v.size() );
    }


    int main()
    {
    std::cout << F().s << std::endl;
    }
    Gianni Mariani, Jul 29, 2004
    #1
    1. Advertising

  2. Gianni Mariani

    Andre Kostur Guest

    Gianni Mariani <> wrote in news:cea0v5$j35
    @dispatch.concentric.net:

    >
    > Note that F() takes the address of a 0 size vector. Should the code
    > below be considered "bad" ? STLPort complains about it but gcc and
    > VC++7.1 eat it fine.
    >
    >
    > #include <string>
    > #include <iostream>
    > #include <vector>
    >
    > struct foo
    > {
    > foo( char * ix, size_t is )
    > : x( ix ), s( is )
    > {
    > }
    >
    > char * x;
    > size_t s;
    > };
    >
    > std::vector<char> v;
    >
    > foo F()
    > {
    > return foo( & v[0], v.size() );
    > }
    >
    >
    > int main()
    > {
    > std::cout << F().s << std::endl;
    > }
    >


    Offhand I think you've invoked undefined behaviour at the point of you
    attempting to index element 0 of an empty vector. I don't think that a
    vector is required to have created _any_ storage for data elements yet...
    it could be deferring that operation until the first element creation...
    Andre Kostur, Jul 29, 2004
    #2
    1. Advertising

  3. Gianni Mariani wrote:
    >
    > Note that F() takes the address of a 0 size vector. Should the code
    > below be considered "bad" ? STLPort complains about it but gcc and
    > VC++7.1 eat it fine.
    >
    >
    > #include <string>
    > #include <iostream>
    > #include <vector>
    >
    > struct foo
    > {
    > foo( char * ix, size_t is )
    > : x( ix ), s( is )
    > {
    > }
    >
    > char * x;
    > size_t s;
    > };
    >
    > std::vector<char> v;
    >
    > foo F()
    > {
    > return foo( & v[0], v.size() );




    Mistake. There is no v[0].






    Regards,

    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Jul 30, 2004
    #3
  4. Gianni Mariani

    Guest

    Ioannis Vranos <> wrote in message news:<cedj1m$1nta$>...
    > Gianni Mariani wrote:
    > >
    > > Note that F() takes the address of a 0 size vector. Should the code
    > > below be considered "bad" ? STLPort complains about it but gcc and
    > > VC++7.1 eat it fine.
    > >
    > >
    > > #include <string>
    > > #include <iostream>
    > > #include <vector>
    > >
    > > struct foo
    > > {
    > > foo( char * ix, size_t is )
    > > : x( ix ), s( is )
    > > {
    > > }
    > >
    > > char * x;
    > > size_t s;
    > > };
    > >
    > > std::vector<char> v;
    > >
    > > foo F()
    > > {
    > > return foo( & v[0], v.size() );

    >
    >
    >
    > Mistake. There is no v[0].


    The value it returns is certainly undefined. Is the action of,
    say,

    someVariable = v[0];

    undefined? That is, someVariable here would certainly not get
    anything meaningful in it. But is that "as bad" as this gets?
    In other words, is it worse than the following?

    char *someVariable;
    char *otherVariable;
    someVariable = otherVariable;

    After this, someVariable still contains junk. But it won't
    cause the program to do anything other than put junk in
    someVariable. (At least, I don't think it will. If I'm wrong,
    please do enclue me.) Is the v[0] thing similar? Or is there
    the possibility of worse things?
    Socks
    , Jul 30, 2004
    #4
  5. wrote:

    >>>std::vector<char> v;
    >>>
    >>>foo F()
    >>>{
    >>> return foo( & v[0], v.size() );

    >>
    >>
    >>
    >>Mistake. There is no v[0].

    >
    >
    > The value it returns is certainly undefined. Is the action of,
    > say,
    >
    > someVariable = v[0];
    >
    > undefined?



    Yes. There is no element v[0].


    > That is, someVariable here would certainly not get
    > anything meaningful in it. But is that "as bad" as this gets?
    > In other words, is it worse than the following?
    >
    > char *someVariable;
    > char *otherVariable;
    > someVariable = otherVariable;




    They are also undefined behaviour. Worse or not, it depends on context.



    > After this, someVariable still contains junk. But it won't
    > cause the program to do anything other than put junk in
    > someVariable. (At least, I don't think it will. If I'm wrong,
    > please do enclue me.) Is the v[0] thing similar? Or is there
    > the possibility of worse things?




    It is undefined behaviour. In may happen nothing at this point and
    happen afterwards, the program may crash, anything. In Windows in
    particular, usually the program crashes.






    Regards,

    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Jul 31, 2004
    #5
  6. Gianni Mariani

    Old Wolf Guest

    Andre Kostur <> wrote:
    > Gianni Mariani <> wrote:
    >
    > > std::vector<char> v;
    > > return foo( & v[0], v.size() );
    > >

    >
    > Offhand I think you've invoked undefined behaviour at the point of you
    > attempting to index element 0 of an empty vector. I don't think that a
    > vector is required to have created _any_ storage for data elements yet...


    Annoying. What's the simplest way to obtain a pointer to the start of
    a vector then? It would be silly to require some sort of macro or function
    for this seemingly obvious task.
    Old Wolf, Aug 1, 2004
    #6
  7. Gianni Mariani

    Andre Kostur Guest

    (Old Wolf) wrote in
    news::

    > Andre Kostur <> wrote:
    >> Gianni Mariani <> wrote:
    >>
    >> > std::vector<char> v;
    >> > return foo( & v[0], v.size() );
    >> >

    >>
    >> Offhand I think you've invoked undefined behaviour at the point of
    >> you attempting to index element 0 of an empty vector. I don't think
    >> that a vector is required to have created _any_ storage for data
    >> elements yet...

    >
    > Annoying. What's the simplest way to obtain a pointer to the start of
    > a vector then? It would be silly to require some sort of macro or
    > function for this seemingly obvious task.


    Note that at this point in time, the vector has _no_ members. When you
    actually have members in the vector, then &v[0] makes sense. (As long as
    you keep in mind as to when that pointer is no longer valid....)
    Andre Kostur, Aug 1, 2004
    #7
  8. (Old Wolf) wrote in message news:<>...
    > Andre Kostur <> wrote:
    > > Gianni Mariani <> wrote:
    > >
    > > > std::vector<char> v;
    > > > return foo( & v[0], v.size() );
    > > >

    > >
    > > Offhand I think you've invoked undefined behaviour at the point of you
    > > attempting to index element 0 of an empty vector. I don't think that a
    > > vector is required to have created _any_ storage for data elements yet...

    >
    > Annoying. What's the simplest way to obtain a pointer to the start of
    > a vector then? It would be silly to require some sort of macro or function
    > for this seemingly obvious task.


    v.begin() (of type std::vector<char>::iterator) is a pointer to the
    first element of the vector. *(v.begin()) is the first element,
    though of course in your example this dereference would still be
    invalid.

    I see your frustration -- you want an array-style pointer to the
    beginning of the vector. v.begin() I think is as close as you're
    going to get, and it's probably not of the type you want, char*.

    http://www.sgi.com/tech/stl/Vector.html is relevant, and SGI's
    documentation in general is invaluable.

    Best of luck to you.

    -Russell Silva
    Russell Silva, Aug 2, 2004
    #8
  9. Russell Silva wrote:
    > (Old Wolf) wrote in message news:<>...
    >
    >>Andre Kostur <> wrote:
    >>
    >>>Gianni Mariani <> wrote:
    >>>
    >>>
    >>>>std::vector<char> v;
    >>>> return foo( & v[0], v.size() );

    >
    > v.begin() (of type std::vector<char>::iterator) is a pointer to the
    > first element of the vector.



    Actually it is a vector<char>::iterator which is probably implemented as
    a just char *pointer. But the right way to work with containers is to
    use iterators and not pointers.






    Regards,

    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Aug 2, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oodini
    Replies:
    1
    Views:
    1,751
    Keith Thompson
    Sep 27, 2005
  2. Angel Tsankov
    Replies:
    1
    Views:
    855
    Victor Bazarov
    Apr 5, 2006
  3. Replies:
    1
    Views:
    483
    Sion Arrowsmith
    Jul 10, 2008
  4. Keith Thompson

    Re: __STDC_IEC_559__ (defined or !defined ?)

    Keith Thompson, Aug 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    430
    Keith Thompson
    Aug 17, 2010
  5. Alok
    Replies:
    3
    Views:
    239
Loading...

Share This Page