473,804 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

accessing an empty string

#include <iostream>
#include <limits>
int main()
{
std::string s1;
std::cout << s1[10] << std::endl;

return 0;
}
[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra test.cpp
[arnuld@arch cpp ]% ./a.out

[arnuld@arch cpp ]%
this programme compiles and runs without any trouble. why i do not get any
error (because the string is empty and i am trying to access 9th
character).

is it a valid C++ programme ?
--
-- http://arnuld.blogspot.com

Jul 18 '07 #1
3 1703
arnuld wrote:
#include <iostream>
#include <limits>
int main()
{
std::string s1;
std::cout << s1[10] << std::endl;

return 0;
}
[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra test.cpp
[arnuld@arch cpp ]% ./a.out

[arnuld@arch cpp ]%
this programme compiles and runs without any trouble. why i do not get any
error (because the string is empty and i am trying to access 9th
character).
Because it's undefined behaviour. If you don't _know_ that your index is
inside the bounds, use at:

s1.at(10);

And: You are trying to access the eleventh character.
is it a valid C++ programme ?
No: You haven't included <stringand <ostream(techni cally, you need
<ostream>, but it seems that most, if not all, implementations of
<iostreaminclud es <ostream>), but if you do that, it's still undefined
behaviour.

--
rbh
Jul 18 '07 #2
On 2007-07-18 11:15, arnuld wrote:
#include <iostream>
#include <limits>
int main()
{
std::string s1;
std::cout << s1[10] << std::endl;

return 0;
}
[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra test.cpp
[arnuld@arch cpp ]% ./a.out

[arnuld@arch cpp ]%
this programme compiles and runs without any trouble. why i do not get any
error (because the string is empty and i am trying to access 9th
character).
Fist of it's the 11th character you are trying to access. The []
operator on standard containers (string can be considered as such) is
not checked*, the at() method provides the same service except it will
throw an exception if the index is out of range so you might want to use
that instead.

* Notice that it behaves a bit different on std::map, where a new
element will be created instead.

--
Erik Wikström
Jul 18 '07 #3
arnuld wrote:
#include <iostream>
#include <limits>
int main()
{
std::string s1;
std::cout << s1[10] << std::endl;

return 0;
}
[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra test.cpp
[arnuld@arch cpp ]% ./a.out

[arnuld@arch cpp ]%
this programme compiles and runs without any trouble. why i do not get any
error (because the string is empty and i am trying to access 9th
character).

is it a valid C++ programme ?

No it's not a valid C++ program. Sometimes invalid C++ programs do not
produce errors.

john
Jul 18 '07 #4

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

Similar topics

0
1518
by: Tetet B via .NET 247 | last post by:
I'm using VB.Net and trying to retrieve the data in the textboxcell of a datagrid after a successful display (used Databind).User can either display data or enter data in the textbox. I canalways get the textbox object using either the Controls orFindControl method but it's always returning an empty *.Textinstead of the value displayed on the screen. Below are snippetsof my code - anybody there who can help me PLEASE? =====*.vb <edit mode...
6
13268
by: wASP | last post by:
Hi again, I'm having a problem accessing the ViewState object. I'm using the following two functions - as copied from the MS docs - and state.count is zero in the first - and the while loop in the second function never iterates: /* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */ public string GetViewStateList () {
0
3381
by: msnews.microsoft.com | last post by:
Hi, IN ASP.NET PAGE I HAVE DEFINED THE DIV TAG AS FOLLOWS:I want to access the value in the code behind(c#) . <div align="left" autopostback = true class="clsTitle" style="VISIBILITY:hidden" id="HiddenFlag" runat="server"></div> I am accessing the div tag in the code behind as follows:
3
1456
by: spmm# | last post by:
Hi! Could someone please help me with the following; I have a WebControl that basically looks like this: public class LeftMenu : System.Web.UI.WebControls.WebControl { private string m_CurrentNodeId = string.Empty; public string CurrentNodeId
6
3437
by: Steve Barnett | last post by:
I have created a user control that includes the following field: protected TreeColumn columns = new TreeColumn; This is accessed via an accessor as follows: pubilc TreeColumn Columns { get { return columns; } set { columns = value; } }
0
1327
by: tahsin_sayeeda | last post by:
Aas Salamu Aalaikum I’m facing a problem while using C#.NET class. I think my problem is very silly and most of the time I stuck in my programs for silly problems. (Most silly problems…!) I create a class using C# and my problem is set the value in the class and getting the value from the class..!!! (I know its heard silly…!!) Whatsoever, my code isn’t working. Here’s my class using System; using System.Collections;
3
1195
by: CCLeasing | last post by:
I have a function that generates an error if the file it is operating on is empty. I would like to add some error protection in. The modification i tried to make to the function i've included in the commented section in the code. However the ide underlines the lines.Length in red indicating that it isn't accesible why is this? how else can i code this? private bool CheckDuplicates(string filepath, string linetocheck) {
26
2802
by: anonieko | last post by:
In the past I always used "" everywhere for empty string in my code without a problem. Now, do you think I should use String.Empty instead of "" (at all times) ? Let me know your thoughts.
1
1437
by: chetuashish | last post by:
I am importing a excel sheet data into a data set using C#.net in a web application i m using following connection string "DSN=Excel Files;DBQ=C:\\Inetpub\\wwwroot\\uninumberusa\\CDR\\Intellicenter Jan 2007 Bill 2000000157.xls;DefaultDir=C:\\Inetpub\\wwwroot\\uninumberusa\\CDR;DriverId=790;MaxBufferSize=2048;PageTimeout=5;HDR=YES;IMEX=1;ImportMixedTypes=Text;" now the problem is that, in the sheet some cells are getting empty value. the...
0
10603
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10353
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10099
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
9176
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
7643
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
5536
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
4314
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
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.