program crash

This is a discussion on program crash within the C++ Programming forums, part of the General Programming Boards category; Hello.. Why would that make my program crash? Code: class session { public: session() { } string get_setting(string name); }; ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    program crash

    Hello..

    Why would that make my program crash?

    Code:
    class session {
    	public:
    		session() { }
    		string get_setting(string name);
    };
    
    string session::get_setting(string name) {
    	//sth
    	return 0;
    }
    
    void somefunc() {
    	session *s = new session();
    	
    	string name = "some name";
    	s->get_setting(name);
    }

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    What is that: "session() { }" doing inside public?

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    The crash is not because of the class for sure.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Does your program segfault? Is that what you mean by crash? Or does the console window just flash so quickly that you can't even see it?

    FAQ > How do I... (Level 1) > Stop my Windows Console from disappearing everytime I run my program?

    Or does your program not compile, possibly because you don't include <string>?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Can't you define "s" just like this?:
    Code:
    session *s;
    It seems strange to use "new" in a pointer or an array...

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    string session::get_setting(string name) {
    	//sth
    	return 0;
    }
    I'm not sure 0 is a valid string . . . try returning "".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by dwks
    Code:
    string session::get_setting(string name) {
    	//sth
    	return 0;
    }
    I'm not sure 0 is a valid string . . . try returning "".
    Yeah, you're right.. Seems like string cant return 0.. What would be the right approach to do that kind of function? Im used to return pointer to char string and 0 on error.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    As I already said, return an empty string:
    Code:
    return "";
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Even better, return a default constructed string:
    Code:
    return string();
    >> Can't you define "s" just like this?: session *s;
    No, that would be just an uninitialized pointer. You would probably just want to define s like this (maybe what you meant):
    Code:
    session s;

  10. #10
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Is it okay to return string or would you pro guys prefer to set a variable instead?

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Returning an empty string would be the way I'd do it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> Is it okay to return string or would you pro guys prefer to set a variable instead?

    I'm not sure what you mean. If you don't have any data to return and you want to return an empty string, I would probably do it the way I showed.

    It doesn't really matter, but some people prefer to have a single local variable for these types of things, and that variable is always returned. If there is no data to return and you want to return an empty string, then just don't set the variable to any value. If there is data, assign it to that variable. This can help make the return value optimization work. But I really don't think you should worry about that kind of optimization in this case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char vs int - program crash!!
    By Goldrak in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2006, 08:17 PM
  2. My program causes my compiler to crash
    By carolsue2 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2006, 04:06 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 11:26 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21