Segmentation Fault when getting user input?

Up vote 3 down vote favorite share g+ share fb share tw.

I have this: // Get database access parameters const char* db = "codes", *server = "localhost", *user = "root", *pass = "pass"; // Connect to the sample database. Mysqlpp::Connection conn(false); if (conn. Connect(db, server, user, pass)) { for (int i=0; i> d; cout > count; for (int a=0; a> r; cout > n1 >> n2; cout > name; mysqlpp::Query query = conn.query(); for (int j=n2-n1+1; j Disconnect (); return 0; } else { cerr And how to solve it?

C++ segmentation-fault link|improve this question edited Nov 28 '11 at 15:47DevSolar8,6051249 asked Nov 28 '11 at 13:35?162.

2 You should make your next quest learning to use a debugger, for example gdb. A debugger would have told you that you're crashing in the cin >> name; line and that name is not a sane pointer. – derobert Nov 28 '11 at 13:56 If you are on linux, try valgrind – Dadam Nov 29 '11 at 7:45.

You should allocate space for char * name; before writing to it. If you know that name isn't going to be longer then MAXNAME then just define name to be char nameMAXNAME+1 That should solve your problem.

Thanx. And, one more thing, I can't into syntax highlighting, whats the matter? –?

Nov 28 '11 at 13:49 @? You managed it by yourself! :) If you find the answer to be satisfying, please consider accepting it.

– Beginner Nov 28 '11 at 13:50 If you actually recommend char arrays in C++, please do show how to avoid buffer overflows. Char nameMAXNAME+1 does not solve problems, it just hides them until the assumption that there are no longer names doesn't hold anymore. – DevSolar Nov 28 '11 at 14:02.

I see that you are using C++. In this case, you should use std::string name; so you don't have to allocate space for it. Rule of thumb: If you're using C++, use instead of char; use instead of arrays; use member variables initialized by constructors instead of either malloc() or new().

It will make your coding life easier, believe me. (Every rule has its exceptions, but that's the starting point...).

Yeah, I've changed it to string, char seems to be hardcore. –? Nov 28 '11 at 14:07 @?

Keep in mind that string goodies come not for free, but with performance penalty. If you can neglect it, of course use string. – Beginner Nov 28 '11 at 14:25 @RomanB.

: An urban legend that has hurt many a C++ project, because people believed it and wrote half-baked, bug-ridden C-style code instead. If you are afraid of the resizing, add name. Reserve(MAXNAME+1);.

When Knuth uttered his words about premature optimization, he meant exactly this case. Prove that C++ strings are the performance bottleneck before coding a char. Everything else is just shoddy workmanship.

I've seen many projects that were hurt by char buffer overflows, but not a single one that was hurt by proper use of std::string. – DevSolar Nov 28 '11 at 15:44 @DevSolar Here is the proof: d-programming-language.org/cppstrings.html See tables in the bottom. – Beginner Nov 28 '11 at 15:47 @RomanB.

: Does apply to the OP's question... how? Neither does he code in D, nor is the type of name critical to the performance of his program. – DevSolar Nov 28 '11 at 15:50.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions