473,788 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.beg in(), s.end(), buf);
}

Same question again for std::vector<cha r> 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 4435
Old Wolf wrote:
Is this undefined behaviour:
std::string s;
char buf[20];

std::copy(s.beg in(), 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.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - 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.beg in(), 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*****@inspir e.net.nz> said:
Is this undefined behaviour:

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

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

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

Same question again for std::vector<cha r> 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
2024
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
3667
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
3387
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 of the program till the last byte. Here we find static tables, global context pointers, etc. This are the global variables of the program.
1
3958
by: Col | last post by:
Begin() method applied on empty Vector returns NULL or non null value???? Plz help!!! Thanks&regards, COL
3
2028
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
1557
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 strings, whose size is of atleast a specified length (here 5) and contains atleast a specified number of punctuation characters, excluding '@' (here 2). There are 3 classes: Length and NumberOfSpecialChars in 'Parameters' namespace (which are just...
11
1689
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, the behaviours of std::vector::iterator can not meet my requirements. I need to redefine the vector::iterator's behavious such as ++. It means "next position in the object sequence" in STL but now I redefine it to mean "next position within a 2-d...
2
4859
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 begin() == end()?
3
2427
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
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10118
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.