473,385 Members | 1,838 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,385 software developers and data experts.

String - substr query

sks
Hi,

Here is a small program that is wrtten to simply use substr.
When the second parameter in substr (length of the string to be
extracted) is lesser than 0, the output is the entire string (I'm using
gcc 4.1.0)
My questions are :
1. Is this behaviour correct ? ( Or should this case be an exception?)
2. If it is correct, what is the idea behind this behaviour ?

using namespace std;
#include <iostream>
#include <string>
int main()
{
string s("This is a test string");
cout << s.substr(0,-1) << endl;
return 0;
}
Thanks,
Sumedha Swamy

Jul 12 '06 #1
6 6004
In article <11**********************@p79g2000cwp.googlegroups .com>,
"sks" <su**********@gmail.comwrote:
Hi,

Here is a small program that is wrtten to simply use substr.
When the second parameter in substr (length of the string to be
extracted) is lesser than 0, the output is the entire string (I'm using
gcc 4.1.0)
My questions are :
1. Is this behaviour correct ? ( Or should this case be an exception?)
Yes.
2. If it is correct, what is the idea behind this behaviour ?

using namespace std;
#include <iostream>
#include <string>
int main()
{
string s("This is a test string");
cout << s.substr(0,-1) << endl;
return 0;
}
basic_string basic_string::substr(size_type pos = 0, size_type n = npos)
const

Constructs a string from a substring of s. The substring begins at
character position pos and terminates at character position pos + n or
at the end of s, whichever comes first.

Note that n is of type size_type which is unsigned, so passing -1
probably makes for a very big number.
Jul 12 '06 #2

sks wrote:
Hi,

Here is a small program that is wrtten to simply use substr.
When the second parameter in substr (length of the string to be
extracted) is lesser than 0, the output is the entire string (I'm using
gcc 4.1.0)
There is no such thing as a substr parameter lesser than 0 as the param
type is unsigned.
My questions are :
1. Is this behaviour correct ? ( Or should this case be an exception?)
2. If it is correct, what is the idea behind this behaviour ?

using namespace std;
#include <iostream>
#include <string>
int main()
{
string s("This is a test string");
cout << s.substr(0,-1) << endl;
Passed parameter is not -1 but a very large positive number on most
architectures.
return 0;
}
Thanks,
Sumedha Swamy
Jul 12 '06 #3
sks wrote:
Hi,

Here is a small program that is wrtten to simply use substr.
When the second parameter in substr (length of the string to be
extracted) is lesser than 0, the output is the entire string (I'm using
gcc 4.1.0)
My questions are :
1. Is this behaviour correct ? ( Or should this case be an exception?)
Somewhat.
2. If it is correct, what is the idea behind this behaviour ?
The idea is that substr() should return the whole string if the second
argument is larger than the length of the string.
using namespace std;
#include <iostream>
#include <string>
int main()
{
string s("This is a test string");
cout << s.substr(0,-1) << endl;
return 0;
}
The signature of substr() is:

basic_string<charT,traits,Allocator>
substr(size_type pos = 0, size_type n = npos) const;

As you can see, the length argument is of type size_type, which is inherited
from the Allocator. You are using std::allocator<charT>, whose size_type is
std::size_t and therefore unsigned. You are passing -1, which wraps around
and yields in fact the maximum number that can be represented.
Best

Kai-Uwe Bux
Jul 12 '06 #4
"sks" <su**********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
Hi,

Here is a small program that is wrtten to simply use substr.
When the second parameter in substr (length of the string to be
extracted) is lesser than 0, the output is the entire string (I'm using
gcc 4.1.0)
My questions are :
1. Is this behaviour correct ? ( Or should this case be an exception?)
2. If it is correct, what is the idea behind this behaviour ?

using namespace std;
#include <iostream>
#include <string>
int main()
{
string s("This is a test string");
cout << s.substr(0,-1) << endl;
return 0;
}
Thanks,
Sumedha Swamy
It is correct by the fact that substr's second paramter is unsigned. size_t
which (on my implementation) is a 4 byte unsigned int.

Passing a negative number as an unsigned parameter actually makes a very BIG
number. The high bit is set in signed numbers, and in unsigned the high bit
is treated as a high number. 2147483648 in a 32 bit unsigned int. If your
system uses 2's compliment (most do) -1 as a signed is the largest value the
number can hold, because all bits are set for -1 (google up on 2's
compliment to find out why).

Incidently, -1 is actually teh value used for npos (no position) which is
the largest value for size_t (all bits set).

So, saying
s.substr(0,-1)
is actually the same as saying
s.substr(0, 4294967295)

You can also achieve the exact same thing by leaving off the second parm.
s.substr(0)
If the second parm is left off, it defaults to npos which is 4294967295 on
32 bit systems.
Jul 12 '06 #5
sks wrote:
Hi,

Here is a small program that is wrtten to simply use substr.
When the second parameter in substr (length of the string to be
extracted) is lesser than 0, the output is the entire string (I'm using
gcc 4.1.0)
As others pointed out, the lengt parameter of substr is size_type,
unsigned.

But being often enough enoyed by the signed/unsigned warnings of
g++/vc++, I wondered why gcc didn't warn you.

Having installed a few versions, I tried:

$ g++-2.95.4 -Wall -O2 ref.cc -o ref && ./ref
This is a test string

$ g++-3.3.6 -Wall -O2 ref.cc -o ref && ./ref
ref.cc: In function `int main()':
ref.cc:8: warning: passing negative value `-1' for converting 2 of `
std::basic_string<_CharT, _Traits, _Allocstd::basic_string<_CharT,
_Traits, _Alloc>::substr(typename _Alloc::size_type, typename
_Alloc::size_type) const [with _CharT = char, _Traits =
std::char_traits<char>, _Alloc = std::allocator<char>]'
This is a test string

$ g++-4.0.3 -Wall -O2 ref.cc -o ref && ./ref
ref.cc: In function 'int main()':
ref.cc:8: warning: passing negative value '-0x00000000000000001' for
argument 2 to 'std::basic_string<_CharT, _Traits, _Alloc>
std::basic_string<_CharT, _Traits, _Alloc>::substr(typename
_Alloc::size_type, typename _Alloc::size_type) const [with _CharT =
char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'
This is a test string

$ g++-4.1.1 -Wall -O2 ref.cc -o ref && ./ref
This is a test string

So the various g++ versions somtimes warn, sometimes don't. You just
happened to use a version that doesn't. Wonder why 4.1 stopped warning
again. (It's not in the .h files, as the cpp (-E) output generated by
4.0.3 doesn't generate a warning with 4.1.1, the reverse does warn)

Jul 12 '06 #6
sks
Hi,
Thanks all of you. I'm now convinced that the behaviour is correct.
But as joosteto has pointed out, gcc should have atleast given me a
warning.

Thanks once again.

Sumedha Swamy

Jul 13 '06 #7

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

Similar topics

10
by: RH | last post by:
<SELECT name="txtServices" size="3" multiple> <OPTION value="Massage">Massage</OPTION> <OPTION value="Facials">Facials</OPTION> <OPTION value="Wet & Dry Body Treatments">Wet &amp; Dry Body...
3
by: deko | last post by:
I'm trying to create an email mailing list for Outlook. Here's the part of the sub that puts the email address in the Bcc (or To) field: With objNewMail .BCC = strEmail .Display End With
1
by: paulwsarahr | last post by:
Hi group I have a database that contains questions and answers. I need to create a query that will produce results for all the questions and answers fields by looking for a single word that will...
2
by: Jarek Bednarz | last post by:
Hi all, Following code causes strAddress to contain garbage data. When I decrease number of characters that appear before '\\' then everything is ok. 12 chars before '\\' seems to be a magic...
0
by: Java25 | last post by:
I am trying to insert a date to my access database and i get the error below. please help: Syntax error in string in query expression ''Fri Jun 08 10:49:00 CAT 2007)'. I only want in the...
3
by: Boltar | last post by:
How persistant is the string created by substr()? Is it deleted immediately the context exits or is it stored internally by the parent string? I ask because I'm wondering if its safe to keep a copy...
4
by: srinathvs | last post by:
Hi, I have an access db that I am trying to query from a vb6 program. I've the following code: Dim sSQLQuery As String sSQLQuery = "SELECT * FROM TblData WHERE ID = " & Chr(39) & ID &...
5
by: rolltide | last post by:
I've seen many similar threads, but despite repeated efforts I cannot figure out my problem. I am running Access 2003, VB 6.5, Office XP Pro. Code excerpt is below (you can see where I've tried...
4
by: bullfrog83 | last post by:
I wrote a function that takes a comma-separated string, parses out the individual values, places quotations around each value and a comma in-between each value so that they can be processed in a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.