run time error

Discussion in 'C++' started by Gary Wessle, Dec 24, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    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;
    }
    Gary Wessle, Dec 24, 2006
    #1
    1. Advertising

  2. Gary Wessle

    p-Brane Guest

    "Gary Wessle" <> wrote in message
    news:...
    >
    > 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;
    p-Brane, Dec 24, 2006
    #2
    1. Advertising

  3. Gary Wessle

    Salt_Peter Guest

    Gary Wessle wrote:
    > 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.
    Salt_Peter, Dec 24, 2006
    #3
  4. 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.
    Gianni Mariani, Dec 24, 2006
    #4
  5. Gary Wessle

    BobR Guest

    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'!

    --
    Bob R
    POVrookie
    BobR, Dec 24, 2006
    #5
  6. Gary Wessle

    Salt_Peter Guest

    Gianni Mariani wrote:
    > 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
    */
    Salt_Peter, Dec 24, 2006
    #6
  7. Gary Wessle

    Jim Langston Guest

    "Gary Wessle" <> wrote in message
    news:...
    >
    > 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.
    Jim Langston, Dec 24, 2006
    #7
  8. Gary Wessle

    Xian Guest

    p-Brane wrote:

    > "Gary Wessle" <> wrote in message
    > news:...
    >>
    >> 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
    Xian, Dec 24, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Cook
    Replies:
    0
    Views:
    433
    Paul Cook
    Nov 6, 2003
  2. Tzury Bar Yochay
    Replies:
    3
    Views:
    473
    Jacob Yang [MSFT]
    Nov 29, 2003
  3. flamesrock
    Replies:
    8
    Views:
    430
    Hendrik van Rooyen
    Nov 24, 2006
  4. iskeletor
    Replies:
    3
    Views:
    350
    Barry Schwarz
    Dec 26, 2006
  5. Pierre Yves
    Replies:
    2
    Views:
    468
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page