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

std::vector<string*> 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\
nes 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_length" 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 3054

"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_length" 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_length" 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**********@Home.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\
nes 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_length" 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::append() 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::append() takes a char* and not a char.
Thank you anyway.


Just to let you know. There are five variations of std::string::append
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
dalbosco <da******@free.fr> wrote:
dalbosco wrote:
vector<string*> v;
vector<string*>::iterator it;

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


BTW I'm not scared by pointer. They avoid useless in-memory
copies while passing parameters.


Actually they use more memory here. Instead of N strings you
have N strings and N pointers. What's worse though, instead of
one block of memory with N strings, you have N blocks of memory.
If your allocator has a minimum allocation size (most do AFAIK)
then you have wasted some bytes. Also you have increased
fragmentation of the memory pool (thus degrading OS performance).

Also your posted code has no instances of a 'passing parameter'
that would have saved time or memory by using pointers. For
example, passing by reference is either the same or better
than passing by pointer.
Jul 22 '05 #11

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

Similar topics

2
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 ? //...
5
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
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!...
6
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 *...
6
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",...
42
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...
9
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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
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,...
0
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...

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.