skip to Main Content

I am trying to run a simple program in C++ which basically counts the amount of symbols using the length() function from the <string> library of C++.

Here’s my custom function:

#include <iostream>
#include <string>
using namespace std;

string TrimTextFunction() {
    string Text = "Hello I am your program!";

    cout << "String size is: " << Text.length();

    return 0; 
} 

Then, I simply run this custom function inside main() through the debugger in Visual Studio:

int main()
{
    cout << TrimTextFunction(); 
}

Eventually, I get this error code:

Exception thrown at 0x00007FFE67837A41 (ucrtbased.dll)

Then, it redirects me to the inner file called xstring showing the error itself.

Without the debugger, the code returns the value I need.

But when I run the same code inside main() using the same debugger (or without), it has no error whatsoever.

#include <iostream>
#include <string>
using namespace std; 


int main()
{
    string Text = "Hello I am your program!";

    cout << "String size is: " << Text.length();
}

It seems like I am missing something about custom functions, particularly when I debug something.

Why does the main() function allow me to get a result that I need without error, and a custom function doesn’t?

I have used a custom function and I need to expect a result of a string that returns the length of a string variable, which is 24.

2

Answers


  1. Your function is defined as

    string TrimTextFunction() {
        string Text = "Hello I am your program!";
    
        cout << "String size is: " << Text.length();
    
        return 0; 
    } 
    

    In this case using 0 to construct a std::string is invoking the constructor

    std::string(const char* s, const Allocator& alloc = Allocator())
    

    Constructs the string with the contents initialized with a copy of the null-terminated character string pointed to by s. The length of the string is determined by the first null character. The behavior is undefined if [s, s + Traits::length(s)) is not a valid range (for example, if s is a null pointer). This constructor is not used for class template argument deduction if the Allocator type that would be deduced does not qualify as an allocator.

    Note that as of C++23 this constructor is declared deleted so this wouldn’t even compile

    std::string(std::nullptr_t) = delete;
    

    If you just want your helper function to output to stdout then it does not need to return anything, and therefore the return type would be void and you can omit a return statement

    void TrimTextFunction() {
      string Text = "Hello I am your program!";
    
      cout << "String size is: " << Text.length();
    } 
    
    int main() {
      TrimTextFunction(); 
    }
    
    Login or Signup to reply.
  2. Lets make the example simpler. The version with all code in main is

    #include <iostream>
    
    int main() {
        std::cout << 42;
    }
    

    You want to place the 42 inside a function. The function should return that value. That would be:

     #include <iostream>
    
     int foo() { return 42; }
    
     int main() {
         std::cout << foo();
     }
    

    But what you did instead is something along the line of this:

     #include <iostream>
    
     int foo() { 
         std::cout << 42;
         return 0;
     }
    
     int main() {
         std::cout << foo();
     }
    

    You have to decide: Is the function printing the value on the screen. Or is the function returning the value so that main can print it. You could also make both, the function print it and let main print it. However, your code is somewhere in between.

    Here

    string TrimTextFunction() {
        string Text = "Hello I am your program!";
    
        cout << "String size is: " << Text.length();
    
        return 0; 
    } 
    
    
    int main()
    {
        cout << TrimTextFunction(); 
    }
    

    You let TimeTextFunction print the result and return some unrelated 0. Unfortunately std::string has a constructor that can be called with a literal 0, but it invokes undefined behavior when you do so, because it expects a pointer to nullterminated string, while 0 converts to a nullptr.

    main on the other hand looks like the function returns the restul to be printed, it uses the returned result to print it.

    The solution is, as mentioned above, to not confuse printing on the screen with returning from a function. Decide whether the function or main or both should print it and when you declare the function to return a std::string make sure it returns a proper std::string (I suppose you actually want to return a size_t, the length of the string).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search