473,734 Members | 2,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::vector<str ing*> crash...

Hello,
I am new to STL and I've written the following code
which crash.
Could anyone tell me why this code crash?
Thanks by advance.
--
J-F
#include <iostream>
#include <vector>

using namespace std;

static char data[] =
"Because the original quantities and instructions \
that make up Linux have been published, \
any programmer can see what it is doing,\
how it does it and, possibly, how it cou\
ld do it better. Torvalds did not invent\
the concept of open programming but Linu\
x is its first success story. Indeed, it\
probably could not have succeeded befor\
e the Internet had linked the disparate\
world of computing experts. In making \
Linux an open language, Torvalds gave \
up the opportunity of growing rich fro\
m his work. This too is part of nerd c\
ulture, which thrives on the satisfact\
ion of authorship and the respect of o\
ne’s peers.";

static const int line_length = 10;

int
main(int argc, char** argv)
{
string* str = NULL;
vector<string*> v;
vector<string*> ::iterator it;

for( int i = 0; i < sizeof(data) / sizeof(data[0]); i++)
{
if( 0 == ((i+1) % line_length))
{
// Every "line_lengt h" char create a new
// string which will hold place for the line.

str = new string;
str->insert(str->end(), 'a');
v.insert(v.end( ), str);
}

it = v.end();
// CRASH HERE.
(*it)->insert((*it)->end(), data[i]);
}

for(it = v.begin(); it != v.end(); it++)
{
cout << **it << endl;
delete(*it);
}
}
Jul 22 '05 #1
10 3095

"dalbosco" <da******@free. fr> wrote in message
news:41******** *************** @news.free.fr.. .
Hello,
I am new to STL and I've written the following code
which crash.
Could anyone tell me why this code crash? int
main(int argc, char** argv)
{
string* str = NULL;
vector<string*> v;
vector<string*> ::iterator it;

for( int i = 0; i < sizeof(data) / sizeof(data[0]); i++)
{
if( 0 == ((i+1) % line_length))
{
// Every "line_lengt h" char create a new
// string which will hold place for the line.

str = new string;
str->insert(str->end(), 'a');
v.insert(v.end( ), str);
}

it = v.end();
Now 'it' is pointing outside the vector. It does not
refer to a valid object.
// CRASH HERE.
(*it)->insert((*it)->end(), data[i]);


Here you dereference 'it'. Undefined behavior.

-Mike
Jul 22 '05 #2
dalbosco wrote:
Hello,
I am new to STL and I've written the following code
which crash.
Could anyone tell me why this code crash?
Thanks by advance.
--
J-F
#include <iostream>
#include <vector>

using namespace std;

static char data[] =
"[...]";

static const int line_length = 10;

int
main(int argc, char** argv)
{
string* str = NULL;
vector<string*> v;
Why in the world do you need a vector of _pointers_? Why not just
a vector of strings?
vector<string*> ::iterator it;

for( int i = 0; i < sizeof(data) / sizeof(data[0]); i++)
{
if( 0 == ((i+1) % line_length))
{
// Every "line_lengt h" char create a new
// string which will hold place for the line.
str = new string;
str->insert(str->end(), 'a');
Why do you need this instead of, say,

str->append('a');

?
v.insert(v.end( ), str);
This is much better written

v.push_back(str );

it's much easier to understand that.
}

it = v.end();
// CRASH HERE.
(*it)->insert((*it)->end(), data[i]);
What are you trying to achieve here? Perhaps you wanted to use
'v.rbegin()' instead of 'v.end()' if you need the iterator to the
_last_ element of the vector. 'v.end()' returns the "one-past-the-
end" iterator.

RTFM
}

for(it = v.begin(); it != v.end(); it++)
{
cout << **it << endl;
delete(*it);
}
}

Jul 22 '05 #3
On Fri, 22 Oct 2004 19:39:04 +0200, dalbosco <da******@free. fr> wrote:
Hello,
I am new to STL and I've written the following code
which crash.
Could anyone tell me why this code crash?
Thanks by advance.


You don't use line continuations (backslash) when writing actual code,
only when writing macros (#define ...)

Try this instead:

static char data[] =
"Because the original quantities and instructions "
"that make up Linux have been published, "
"any programmer can see what it is doing,"

etc.

The compiler just concatenates the lines automatically. If you want
real line breaks, you need to embed '\n' in the data.

--
Bob Hairgrove
No**********@Ho me.com
Jul 22 '05 #4
it = v.end();
v.end() is the "one past the end" value. // CRASH HERE.
(*it)->insert((*it)->end(), data[i]);
}

There is no object *it.

Further I believe in your code you have NOTHING in the
vector the first time through. When i is initially
0, the if statement condition is false.

I'm not clear what you are trying to do...but the fact
that you have all these pointers to strings pretty much
indicates that you're going about it the wrong way.
Jul 22 '05 #5
dalbosco wrote:
Hello,
I am new to STL and I've written the following code
which crash.
Could anyone tell me why this code crash?
Thanks by advance.
--
J-F
#include <iostream>
#include <vector>

using namespace std;

static char data[] =
"Because the original quantities and instructions \
that make up Linux have been published, \
any programmer can see what it is doing,\
how it does it and, possibly, how it cou\
ld do it better. Torvalds did not invent\
the concept of open programming but Linu\
x is its first success story. Indeed, it\
probably could not have succeeded befor\
e the Internet had linked the disparate\
world of computing experts. In making \
Linux an open language, Torvalds gave \
up the opportunity of growing rich fro\
m his work. This too is part of nerd c\
ulture, which thrives on the satisfact\
ion of authorship and the respect of o\
ne’s peers.";

static const int line_length = 10;

int
main(int argc, char** argv)
{
string* str = NULL;
vector<string*> v;
vector<string*> ::iterator it;

for( int i = 0; i < sizeof(data) / sizeof(data[0]); i++)
{
if( 0 == ((i+1) % line_length))
{
// Every "line_lengt h" char create a new
// string which will hold place for the line.
str = new string;
str->insert(str->end(), 'a');
v.insert(v.end( ), str);
}

it = v.end();
// CRASH HERE.
(*it)->insert((*it)->end(), data[i]);
}

for(it = v.begin(); it != v.end(); it++)
{
cout << **it << endl;
delete(*it);
}
}


OK i finally wen't through and v.rbegin() was the solution.
BTW I'm not scared by pointer. They avoid useless in-memory
copies while passing parameters.
--
JF

Victor :
RTFM too. std::string::ap pend() takes a char* and not a char.
Thank you anyway.
Jul 22 '05 #6
dalbosco wrote:

OK i finally wen't through and v.rbegin() was the solution.
BTW I'm not scared by pointer. They avoid useless in-memory
copies while passing parameters


What useless in-memory copy? If you resize the vector and then
insert characters into the string that's IN the vector (operator[],
front() and back() all return references)...
Jul 22 '05 #7
dalbosco wrote:
[...]
Victor :
RTFM too. std::string::ap pend() takes a char* and not a char.
Thank you anyway.


Just to let you know. There are five variations of std::string::ap pend
and if you RTFM long enough and hard enough you will find one that takes
'char' as one of its arguments. I'll leave it to you to find.

V
Jul 22 '05 #8
There is no point in using "new string(...)" use then plain new
char[x] instead if you have desire to manage memory (and write the
code for that) yourself.
a string object itself is only ~ 30-40 bytes of which most of the part
is a static buffer. it stores pointer (or whatever) to the data that
is automatically (de)allocated. So, why then would you want to new
them if this process happens inside them... Just read some book and
try to do it the way it's explained in the book.
Jul 22 '05 #9
__PPS__ wrote:
There is no point in using "new string(...)" use then plain new
char[x] instead if you have desire to manage memory (and write the
code for that) yourself.
Well, that's not totally true. The point of using a string instead of
a plain char* would be to use string's ability to grow if necessary, and
not having to reallocate the memory. Going "all the way" with dynamic
memory allocation is often problematic for Java programmers crossing
over to C++ because they are used to doing 'new Thing' all the time and
rarily do 'new Thing[blah]'. I'm generalising, of course, and it's
probably a wrong thing to do :*)
a string object itself is only ~ 30-40 bytes of which most of the part
is a static buffer. it stores pointer (or whatever) to the data that
is automatically (de)allocated. So, why then would you want to new
them if this process happens inside them... Just read some book and
try to do it the way it's explained in the book.


Reading a good book would be preferrable to reading "some book". I am
sure you meant that.

V
Jul 22 '05 #10

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

Similar topics

2
2634
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? // test.cpp #include <iostream> #include <fstream> #include <vector> #include <string>
5
3551
by: Gary Wessle | last post by:
whats an efficient way to copy a string to a vector<string>? how about this? #include <iostream> #include <string> #include <vector> Using namespace std;
10
2495
by: Shafik | last post by:
Hello, I am new to C++. I know the reason is probably template instantiation problems ... but what's the *real* reason I cannot declare a: vector<stringv = vector<string>(4); Thanks! --Shafik
6
5716
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
6
7380
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1", new vector<string>)); MapVector.insert(make_pair("string2", new vector<string>)); MapVector.insert(make_pair("string3", new vector<string>));
42
4532
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector of string, in one line. I could
9
3813
by: barcaroller | last post by:
1. If I pass pointers (char*) as iterators to an STL algorithm and the return value is an iterator, can I convert that iterator to a pointer? If yes, how? 2. What is the internal representation of vector<string>? Will the vector contain the string objects or will it contain pointers/references to the string objects? The reason I ask is that it is not clear to me how the v.reserve() and &v operations would work for a vector<string>.
0
8946
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
8777
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,...
0
9452
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
9310
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
9184
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...
1
6737
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
4551
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...
0
4813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.