D
dalbosco
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_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);
}
for(it = v.begin(); it != v.end(); it++)
{
cout << **it << endl;
delete(*it);
}
}
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_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);
}
for(it = v.begin(); it != v.end(); it++)
{
cout << **it << endl;
delete(*it);
}
}