run time error

G

Gary Wessle

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;
}
 
P

p-Brane

Gary Wessle said:
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;
 
S

Salt_Peter

Gary said:
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.
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.
 
G

Gianni Mariani

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.
 
B

BobR

Gary Wessle wrote in message ...
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;
string a = "jack";
// > string f = a.substr(0,1) + "/" + typ->nam;
string f = a.substr(0,1) + "/" + typ.nam;
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'!
 
S

Salt_Peter

Gianni said:
Salt_Peter wrote:
...

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
*/
 
J

Jim Langston

Gary Wessle said:
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;

Okay, typ is a pointer to Typ. So where is it pointing? Right now,
anywhere. It hasn't been initialized, so it's not pointing to any memory
that's been asside to hold a Typ class. Also, the construction hasn't been
called.
string a = "jack";
string f = a.substr(0,1) + "/" + typ->nam;

Okay, typ isn't pointing to any valid memory, so typ->nam attempts to get a
string from some location. Which causes your segmentation fault (basically,
attempting to read memory your program doesn't "own").
cout << f << endl;
}

So, how to fix this? A few ways, depending on what you are actually trying
to do.

1. allocate memory for your typ instance and allow the constructor to be
called.
Typ* typ = new Typ();
2. Point typ to some previously allocated Typ isntance.
Typ MyType;
Typ* typ = &MyType;

In other words, you have defined a naken pointer, but now you have to point
it to a valid instance.
 
X

Xian

p-Brane said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

I need help 1
First time question 1
Character operations in C++ 2
std::bad_alloc 5
Remove Space, Stuck on lab 1
Crossword 2
How can I fix my pattern coding error in c++ 0
string close causes SIGSEGV 3

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top