3

I am a newbie to C++ and I've tried to write a simple string reverse program. When I compile it, everything is OK, but when I run it, I get the following error:

terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid Aborted (core dumped) 

What I am doing wrong? Here is my code.

#include <iostream> using namespace std; string reverse_string(char* argv[], int i); int main(int argc, char* argv[]) { for (int i = 0; i < argc; i++) { cout << reverse_string(argv, i) << endl; } return 0; } string reverse_string(char* argv[], int i) { string arg = argv[i + 1]; string output; int length = arg.length(); for (int index = 1; index <= length; index++) { output += arg[length-index]; } return output; } 
3
  • 1
    You are assigning a nullptr to string which is invalid, most likely it is argv[i+1]. See it live. Commented Feb 15, 2014 at 14:42
  • The error is because you are attempting to construct an std::string from a null char* at some point. Commented Feb 15, 2014 at 14:42
  • argv[argc], which happens on your last iteration, is a null pointer. Commented Feb 15, 2014 at 14:43

3 Answers 3

4

This: argv[i + 1] in the construction of your reversed string should be argv[i] and the main loop should be for (i=1; i<argc; ++i)

And there are simpler ways to reverse a string:

std::string reverse_string(char* argv[], int i) { std::string arg = argv[i]; return std::string(arg.rbegin(), arg.rend()); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I've noticed this already. And thanks for the simpler way :).
2

This error message

terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid

means that you are trying to call constructor of std::string passing as argument pointer NULL. The problem is that *(argv + argc) is NULL pointer.

Also take into account that you must include header <string>

As for the reverse function then it can be written much simpler then that of you. First of all it could have only one parameter of type const char *

For example

#include <iostream> #include <string> std::string reverse_string( const char* s ); int main(int argc, char* argv[]) { for (int i = 1; i < argc; i++) { std::cout << argv[i] << " -> " << reverse_string( argv[i] ) << std::endl; } return 0; } std::string reverse_string( const char* s ) { std::string arg( s ); return std::string( arg.rbegin(), arg.rend() ); } 

3 Comments

One note: argv + argc cannot be a null pointer because argv[argc] is guaranteed to be a null pointer.
@chris It is a typo. Shall be *(argv + argc )
Yep, I understood that already. And about const char* s, I am still learning :). So thats why I din't use const char*
1

Ok thanks for help everyone! I just didn't want to reverse argument 0, which is program's name. But I found another way around it. If anyone is interested, here is the fixed code:

#include <iostream> using namespace std; string reverse_string(char* argv[], int i); int main(int argc, char* argv[]) { for (int i = 1; i < argc; i++) { cout << argv[i] << " -> " << reverse_string(argv, i) << endl; } return 0; } string reverse_string(char* argv[], int i) { string arg = argv[i]; string output; int length = arg.length(); for (int index = 1; index <= length; index++) { output += arg[length-index]; } return output; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.