473,326 Members | 2,588 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,326 software developers and data experts.

Anything illegal in this code?

Hi all,

Is this code doing any UB by any chance --

#include <map>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

map<string, vector<long sv;

void foo() {
vector<longv;
v.push_back(5);
v.push_back(7);
sv.insert(std::make_pair(string("five"), v));
}
int main() {

foo();
cout << sv["five"][0]; // Print 5
cout << sv["five"][1]; // Print 7
}

Thanks ..
Dec 13 '07 #1
4 1354
Kelvin Moss wrote:
Is this code doing any UB by any chance --

#include <map>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

map<string, vector<long sv;

void foo() {
vector<longv;
v.push_back(5);
v.push_back(7);
sv.insert(std::make_pair(string("five"), v));
}
int main() {

foo();
cout << sv["five"][0]; // Print 5
cout << sv["five"][1]; // Print 7
}
No UB. Insertion in a map _copies_ the object that you
insert. So, even though the 'v' goes away when 'foo' returns,
the copy made by 'sv.insert' stays in the global map, so to
speak (the actual location is determined by the allocator the
map uses for its "values").

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 13 '07 #2
On Dec 13, 2:15 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>
No UB. Insertion in a map _copies_ the object that you
insert. So, even though the 'v' goes away when 'foo' returns,
the copy made by 'sv.insert' stays in the global map, so to
speak (the actual location is determined by the allocator the
map uses for its "values").
Thanks Victor. I also thought it was no UB, but wanted to confirm. So
even something like this would be very legal --

map<string, vector<long m1;
map<string, vector<long::iterator itr;
...
itr = m1.find("xyz");
if (itr != m1.end()) {
vector<longv1 = itr->second;
v1.push_back(8);
}

The above sequence would indeed add 8 in the vector associated with
string "xyz" in map, correct?

Another unrelated question --

stringstream strm;

....
Are strm.str().length() and strlen(strm.c_str()) going to be equal?
Any case in which they might differ?

Thanks again ..

Dec 13 '07 #3
Kelvin Moss wrote:
On Dec 13, 2:15 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>>
No UB. Insertion in a map _copies_ the object that you
insert. So, even though the 'v' goes away when 'foo' returns,
the copy made by 'sv.insert' stays in the global map, so to
speak (the actual location is determined by the allocator the
map uses for its "values").

Thanks Victor. I also thought it was no UB, but wanted to confirm. So
even something like this would be very legal --

map<string, vector<long m1;
map<string, vector<long::iterator itr;
..
itr = m1.find("xyz");
if (itr != m1.end()) {
vector<longv1 = itr->second;
v1.push_back(8);
}

The above sequence would indeed add 8 in the vector associated with
string "xyz" in map, correct?
No. It would add '8' to the local vector (named 'v1') in the body
of the 'if' statement. If you wanted to change the stored vector,
you need to declare 'v1' a _reference_.
Another unrelated question --

stringstream strm;

...
Are strm.str().length() and strlen(strm.c_str()) going to be equal?
Depends.
Any case in which they might differ?
If you output (or stuff into 'strm.str()') the null char ('\0').

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 13 '07 #4
On Dec 13, 2:41 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
The above sequence would indeed add 8 in the vector associated with
string "xyz" in map, correct?

No. It would add '8' to the local vector (named 'v1') in the body
of the 'if' statement. If you wanted to change the stored vector,
you need to declare 'v1' a _reference_.
Ahh..not been in touch with the language for quite some time. Thanks!

Another unrelated question --
stringstream strm;
...
Are strm.str().length() and strlen(strm.c_str()) going to be equal?

Depends.
Any case in which they might differ?

If you output (or stuff into 'strm.str()') the null char ('\0').
Ok..thanks for your inputs!
Dec 14 '07 #5

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

Similar topics

14
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal =...
5
by: Dave | last post by:
I need to cut out illegal characters in a string submitted from a mobile phone to a web form. I need a way to check for the illegal characters in a textbox. I intend to loop through the text and...
2
by: Gabrielle A. Grün | last post by:
Hi All, Does anyone know a way around the illegal reference of a non-static member? Thanks. iNHERITANCE HIERACY
6
by: BlueTrin | last post by:
Hello I was adapting a C version of SolvOpt in C++ to use it within a virtual class. However I am stuck with the overriding of evaluation and gradiant functions. cStepCurveEvaluator.cpp...
8
by: LuB | last post by:
Is this somehow illegal? template<typename T> class A { public: private:
1
by: Manish | last post by:
The code is as ... $folderlistfile = $path."xmlfile.xml"; /* <list> <details id="2"> <name>Books</name>
16
by: DBC User | last post by:
Hi, I have a small XML file, I uploaded to a web page. I have the following code to convert the content I downloaded from web to xml and is giving "Illegal characters in path", but when I try to...
28
by: hijkl | last post by:
hey guys anything wrong with this code?? if it is then what? int *array(int n){ return new int(n); } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) {
3
by: Tarik Monem | last post by:
Hi Everyone, Still a newbie with FLEX, and I've passed arrays using AJAX to FLEX before, but I've never passed links to FLEX. Basically, this is the OUTPUT, which I wanted, but I'm given an...
2
by: Kamaria | last post by:
I wrote this program to calculate the income tax of a user depending on whether or not they were married: import javax.swing.JOptionPane; public class Income_Tax { public static void main...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.