is this defined behaviour in C++?

G

Gianni Mariani

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;
}
 
A

Andre Kostur

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...
 
I

Ioannis Vranos

Gianni said:
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
 
P

puppet_sock

Ioannis Vranos said:
Gianni said:
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
 
I

Ioannis Vranos

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
 
O

Old Wolf

Andre Kostur said:
Gianni Mariani said:
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.
 
A

Andre Kostur

(e-mail address removed) (Old Wolf) wrote in
Andre Kostur said:
Gianni Mariani said:
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....)
 
R

Russell Silva

Andre Kostur said:
Gianni Mariani said:
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
 
I

Ioannis Vranos

Russell said:
Andre Kostur said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top