473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

validity of begin() on empty string or vector

Is this undefined behaviour:

#include <string>
#include <vector>
#include <algorithm>

int main()
{
std::string s;
char buf[20];

std::copy(s.begin(), s.end(), buf);
}

Same question again for std::vector<char> instead of std::string.

Do I have to go s.empty() before every time I go s.begin(), in
case begin() is undefined?

Jul 23 '05 #1
3 4394
Old Wolf wrote:
Is this undefined behaviour:
std::string s;
char buf[20];

std::copy(s.begin(), s.end(), buf);


No, why should it? It is an empty sequence. This applies to all
container types which can be empty as well. Of course, you cannot
dereference the the past the end iterator which is in this case
identical to 'begin()' but this is not done by 'std::copy()' or
other standard algorithms taking an input sequence.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #2
Old Wolf wrote:
Is this undefined behaviour:

#include <string>
#include <vector>
#include <algorithm>

int main()
{
std::string s;
char buf[20];

std::copy(s.begin(), s.end(), buf);
}


It's only undefined if s.size() is greater than 20, in which case you'd
overflow the buffer. Also note that buf will not be usable as a C-style
string after the call to std::copy(). To use it as such you'll have to
append a null character yourself.

Kristo
Jul 23 '05 #3
On 2005-03-03 00:13:23 -0500, "Old Wolf" <ol*****@inspire.net.nz> said:
Is this undefined behaviour:

#include <string>
#include <vector>
#include <algorithm>

int main()
{
std::string s;
char buf[20];

std::copy(s.begin(), s.end(), buf);
}

Same question again for std::vector<char> instead of std::string.

Do I have to go s.empty() before every time I go s.begin(), in
case begin() is undefined?


No. If the container is empty, then begin() and end() return iterators
to the same location. Which means that they are valid in the sense that
they are non-singular, they simply cannot be dereferenced.

There is nothing undefined about the code you posted (of course, it
doesn't really do anything, but that's beside the point).

--
Clark S. Cox, III
cl*******@gmail.com

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dr. J.K. Becker | last post by:
Hi all, I have vectors that holds pointers to other vectors, like so: vector<whatever> x; vector<whatever*> z; z=&x; Now I add something to x
2
by: diadia | last post by:
string s = "hello"; const char *p = s.begin(); cout << p << endl; // print hello s = ""; char *p2= s.begin(); cout << p2 << endl; // print hello why?????
16
by: jacob navia | last post by:
Valid pointers have two states. Either empty (NULL), or filled with an address that must be at a valid address. Valid addresses are: 1) The current global context. The first byte of the data...
1
by: Col | last post by:
Begin() method applied on empty Vector returns NULL or non null value???? Plz help!!! Thanks&regards, COL
3
by: linq936 | last post by:
Hi, I have some code like this, vector<MyNode*getNodes() { return theNodes; } int func() { vector<MyNode*nodes; nodes.insert(nodes.end(), getNodes().begin(), getNodes().end());
2
by: frame | last post by:
Hi, The other day, I was experimenting with Predicates and STL and came across a gotcha with the following program. The objective of the program is to remove those strings, from a vector of...
11
by: food4uk | last post by:
Dear all : I am not good at programming, please give a hand. My data structure is very similar as an array. I actually can use the std::vector as container to organize my data objects. However,...
2
by: barcaroller | last post by:
If an STL container is empty, what will its begin() member function return? Would it be a valid value that an iterator could use, like the end() member function? In particular, can I assume that...
3
by: subramanian100in | last post by:
Suppose I have vector<intv; vector<int>::const_iterator citer = v.begin( ); Here which version of begin( ) is called ? const or non-const version ? Kindly explain
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.