 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gary Wessle Guest
|
Posted: Sun Dec 24, 2006 9:13 am Post subject: run time error |
|
|
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
|
Posted: Sun Dec 24, 2006 9:30 am Post subject: Re: run time error |
|
|
"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
|
Posted: Sun Dec 24, 2006 10:07 am Post subject: Re: run time error |
|
|
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
|
Posted: Sun Dec 24, 2006 10:10 am Post subject: Re: run time error |
|
|
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
|
Posted: Sun Dec 24, 2006 10:10 am Post subject: Re: run time error |
|
|
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
|
Posted: Sun Dec 24, 2006 10:10 am Post subject: Re: run time error |
|
|
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
|
Posted: Mon Dec 25, 2006 4:05 am Post subject: Re: run time error |
|
|
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 |
|
 |
|
|
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
|
|