C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

run time error

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Gary Wessle
Guest





PostPosted: Sun Dec 24, 2006 9:13 am    Post subject: run time error Reply with quote



Hi

I am getting the following output then trying to run the code below.
string does not like what I am doing, why and whats the fix?
thanks



Program received signal SIGSEGV, Segmentation fault.
0x4e34ad7f in std::string::append () from /usr/lib/libstdc++.so.6

#include <string>
#include <iostream>
using namespace std;

class Typ{
public:
string nam;
Typ():nam("mary"){}
};

int main (){
Typ* typ;
string a = "jack";
string f = a.substr(0,1) + "/" + typ->nam;
cout << f << endl;
}
Back to top
p-Brane
Guest





PostPosted: Sun Dec 24, 2006 9:30 am    Post subject: Re: run time error Reply with quote



"Gary Wessle" <phddas (AT) yahoo (DOT) com> wrote in message
news:m3irg2gf53.fsf (AT) localhost (DOT) localdomain...
Quote:

Hi

I am getting the following output then trying to run the code below.
string does not like what I am doing, why and whats the fix?
thanks



Program received signal SIGSEGV, Segmentation fault.
0x4e34ad7f in std::string::append () from /usr/lib/libstdc++.so.6

#include <string
#include <iostream
using namespace std;

class Typ{
public:
string nam;
Typ():nam("mary"){}
};

int main (){
Typ* typ;
string a = "jack";
string f = a.substr(0,1) + "/" + typ->nam;
cout << f << endl;
}

typ is uninitialized. Try this...

Typ* typ = new Typ;
Back to top
Salt_Peter
Guest





PostPosted: Sun Dec 24, 2006 10:07 am    Post subject: Re: run time error Reply with quote



Gary Wessle wrote:
Quote:
Hi

I am getting the following output then trying to run the code below.
string does not like what I am doing, why and whats the fix?
thanks



Program received signal SIGSEGV, Segmentation fault.
0x4e34ad7f in std::string::append () from /usr/lib/libstdc++.so.6

#include <string
#include <iostream
using namespace std;

class Typ{
public:
string nam;
Typ():nam("mary"){}
};

int main (){
Typ* typ;

typ is not an instance. Its just a dumb pointer.
Until proven otherwise, a pointer holds an address to nothing.
Here is a suggestion: when you declare a pointer, always initialize it.
Typ* typ = 0; // black hole
That way you won't forget that its a pointer to nothing until its given
a valid address.

Quote:
string a = "jack";
string f = a.substr(0,1) + "/" + typ->nam;
cout << f << endl;
}

My fav analogy:

If you mail an envelope to a fictitiuos address (a pointer) half-way
around the world, a postman will not construct a house just to receive
the envelope.
Back to top
BobR
Guest





PostPosted: Sun Dec 24, 2006 10:10 am    Post subject: Re: run time error Reply with quote

Gary Wessle wrote in message ...
Quote:

Hi
I am getting the following output then trying to run the code below.
string does not like what I am doing, why and whats the fix?
thanks
Program received signal SIGSEGV, Segmentation fault.
0x4e34ad7f in std::string::append () from /usr/lib/libstdc++.so.6

#include <string
#include <iostream
using namespace std;

class Typ{ public:
string nam;
Typ() : nam( "mary" ){}
};

int main (){
// > Typ* typ; // <-- you forgot to initialise this!!

Typ typ;

Quote:
string a = "jack";
// > string f = a.substr(0,1) + "/" + typ->nam;

string f = a.substr(0,1) + "/" + typ.nam;

Quote:
cout << f << endl;
}
// out: j/mary


// or

int main (){
Typ typ1;
Typ *typ( &typ1 );
string a( "jack" );
string f = a.substr(0,1) + "/" + typ->nam;
cout << f << endl;
}
// out: j/mary

Or, what p-Brane showed you, but wirh a 'delete'!

--
Bob R
POVrookie
Back to top
Gianni Mariani
Guest





PostPosted: Sun Dec 24, 2006 10:10 am    Post subject: Re: run time error Reply with quote

Salt_Peter wrote:
....
Quote:

My fav analogy:

If you mail an envelope to a fictitiuos address (a pointer) half-way
around the world, a postman will not construct a house just to receive
the envelope.


Totally off-topic.

Funny you should say that, but there is use for a smart pointer type
that does exactly that - i.e. new on dereference.
Back to top
Salt_Peter
Guest





PostPosted: Sun Dec 24, 2006 10:10 am    Post subject: Re: run time error Reply with quote

Gianni Mariani wrote:
Quote:
Salt_Peter wrote:
...

My fav analogy:

If you mail an envelope to a fictitiuos address (a pointer) half-way
around the world, a postman will not construct a house just to receive
the envelope.


Totally off-topic.

Funny you should say that, but there is use for a smart pointer type
that does exactly that - i.e. new on dereference.

a PO box?

While you are on the subject of smart pointers...
It might be of interest for the OP that most smart pointers are default
initialized to nullptr. These will also usually put out an assertion if
accessed before being properly initialized.

#include <iostream>
#include <ostream>
#include <boost/shared_ptr.hpp>

template< typename S >
class MyType {
S s;
public:
MyType() : s() { }
MyType(const S& r_s) : s(r_s) { }
const S& get() const { return s; }
};

int main()
{
typedef MyType< std::string > MyString;
typedef boost::shared_ptr< MyString > SPtrStr;

SPtrStr sp_s( new MyString("string 0") );
std::cout << sp_s->get() << std::endl; // ok

SPtrStr sp_test;
std::cout << sp_test->get() << std::endl; // assertion
}

/*
string 0
....Assertion `px != 0' failed.
....Aborted
*/
Back to top
Xian
Guest





PostPosted: Mon Dec 25, 2006 4:05 am    Post subject: Re: run time error Reply with quote

p-Brane wrote:

Quote:
"Gary Wessle" <phddas (AT) yahoo (DOT) com> wrote in message
news:m3irg2gf53.fsf (AT) localhost (DOT) localdomain...

Hi

I am getting the following output then trying to run the code below.
string does not like what I am doing, why and whats the fix?
thanks



Program received signal SIGSEGV, Segmentation fault.
0x4e34ad7f in std::string::append () from /usr/lib/libstdc++.so.6

#include <string
#include <iostream
using namespace std;

class Typ{
public:
string nam;
Typ():nam("mary"){}
};

int main (){
Typ* typ;
string a = "jack";
string f = a.substr(0,1) + "/" + typ->nam;
cout << f << endl;
}

typ is uninitialized. Try this...

Typ* typ = new Typ;
Remember that if you create a new object you should delete it after you've

finished with it.
delete typ;

Also if you don't want to create a new object when you declare a pointer,
you can set it to NULL, because dereferenceing a null pointer is safe, it
just breaks you program. Whereas dereferencing an arbitrary pointer has
unpredictable results (sometimes worse than a SIGSEGV).
--
/Xian
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.