Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > is this defined behaviour in C++?

Reply
Thread Tools

is this defined behaviour in C++?

 
 
Gianni Mariani
Guest
Posts: n/a
 
      07-29-2004

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;
}
 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      07-29-2004
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...
 
Reply With Quote
 
 
 
 
Ioannis Vranos
Guest
Posts: n/a
 
      07-30-2004
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
 
Reply With Quote
 
puppet_sock@hotmail.com
Guest
Posts: n/a
 
      07-30-2004
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
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      07-30-2004
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
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      08-01-2004
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.
 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      08-01-2004
(Old Wolf) wrote in
news: om:

> 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....)
 
Reply With Quote
 
Russell Silva
Guest
Posts: n/a
 
      08-02-2004
(Old Wolf) wrote in message news:<. com>...
> 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
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      08-02-2004
Russell Silva wrote:
> (Old Wolf) wrote in message news:<. com>...
>
>>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
 
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
Re: __STDC_IEC_559__ (defined or !defined ?) Keith Thompson C Programming 0 08-17-2010 04:36 PM
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
defined? for recursive function call v/s defined? for function call stack Alok Ruby 3 04-13-2006 11:53 AM
Using parenthesis with defined (#if defined(...)) Angel Tsankov C++ 1 04-05-2006 10:00 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments