Segmentation fault when returning string from function in c++

jfhfhf839

I have an error in my class. I'm suspect it's something simple, maybe me failing to understand how strings works in C++. My function tries to returns local std::string to another std::string, but instead of getting the string, I'm getting segmentation fault after the function returns. This is my code:

    TestClass::TestClass(std::string a, std::string b)
    {
        m_a = a;
        m_b = b;
    }

    std::string TestClass::stringHandler()
    {
        const char myTemplate[] = "a=%s,b=%s";
        char formattedString[strlen(myTemplate) + m_a.length() + m_b.length()];
        sprintf( formattedString, myTemplate, m_a.c_str(), m_b.c_str());
        std::cout << "formattedString= " << formattedString << "\n";
        std::string returnStr(formattedString);
        std::cout << "returnStr=" << returnStr << "\n";
        return returnStr;
    }

    int main(...)
    {
        /* getting a and b from argv */
        TestClass MyClassObj(a, b);
        std::string strRet = MyClassObj.stringHandler();
        std::cout << "Back after stringHandler\n";
        return 0
    }

In stringHandler, when I'm printing returnStr, it displayes properly. But right after that, I'm getting Segmentation fault, and "Back after stringHandler" is not being printed. Do any of you masters, have any idea what am I doing wrong?

M.M

Several issues:

  • %b is not a valid format specifier.
  • You have 3 format specifiers (if %b is fixed!) but you only passed 2 arguments.
  • You didn't allocate enough memory for formattedString.
  • Arrays with runtime size are illegal in C++

(update:) after the edits to OP, the first three of these were fixed, so if your compiler also has an extension for runtime array size then this code would not cause a segfault.

The simplest fix is just to write:

std::string returnStr = "a=" + m_a + ",b=" + m_b;

But supposing you want to stick with printf formatting, here is one way to do it. Although it is possible to calculate and work out the exact amount of space, that is fragile. It'd be easy to cause a buffer overflow if you made a change to myTemplate.

One plan would be to allocate a large amount of space. A more robust way is to use snprintf to determine the buffer size first:

char const myTemplate[] = "a=%s,b=%s";
size_t expect_len = std::snprintf(NULL, 0, myTemplate, m_a.c_str(), m_b.c_str());

if ( expect_len == -1 )
    throw std::runtime_error("snprintf failed");

std::vector<char> buffer(expect_len + 1);
std::snprintf(&buffer[0], buffer.size(), myTemplate, m_a.c_str(), m_b.c_str());

std::string returnStr(buffer);

In C++11 you could actually snprintf directly into returnStr.

To be clear, the whole printf idea is not very safe as you can cause undefined behaviour if myTemplate contains something you didn't expect. (I presume you did it this way to allow yourself to specify a different format string at runtime). So use it with caution.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Segmentation fault when returning string from function in c++

From Dev

What causes segmentation fault when returning a list from a member function?

From Dev

Segmentation fault on macOS when returning from dylib

From Dev

Segmentation Fault when accessing string array modified by a function in C

From Dev

C++ segmentation fault when calling function from a new location

From Dev

Segmentation fault when reading C-style string from scanf

From Dev

Segmentation fault when calling c-function

From Dev

Segmentation fault when trying to clone a function (in C)

From Dev

segmentation fault when calling c function on ruby

From Dev

Segmentation fault when trying to clone a function (in C)

From Dev

C Segmentation fault when spitting a string

From Dev

Segmentation fault: 11 when returning stuct via function pointer in struct

From Dev

Segmentation fault on reverse string function

From Dev

Segmentation Fault When Calling Function

From Dev

Segmentation fault when function returns

From Dev

Segmentation fault when calling C function with Python C API twice

From Dev

Segmentation Fault when retrieving value from pointer in another function

From Dev

Segmentation fault in C++ program using getline and string function

From Dev

string segmentation fault when reading lines from text file

From Dev

C : Segmentation Fault when move main function to new file

From Dev

Segmentation fault in C code function

From Dev

Running Into Segmentation Fault When Attempting To Convert String To Double in C++

From Dev

segmentation Fault in C String conversion

From Dev

C Segmentation Fault from function that returns the maximum of an array

From Dev

C++ Segmentation fault when erasing from vector

From Dev

Returning string from C function

From Dev

Function that returns string is not working- segmentation fault

From Dev

Segmentation fault when assigning values to pointers in function

From Dev

Segmentation fault when calling bound member function

Related Related

  1. 1

    Segmentation fault when returning string from function in c++

  2. 2

    What causes segmentation fault when returning a list from a member function?

  3. 3

    Segmentation fault on macOS when returning from dylib

  4. 4

    Segmentation Fault when accessing string array modified by a function in C

  5. 5

    C++ segmentation fault when calling function from a new location

  6. 6

    Segmentation fault when reading C-style string from scanf

  7. 7

    Segmentation fault when calling c-function

  8. 8

    Segmentation fault when trying to clone a function (in C)

  9. 9

    segmentation fault when calling c function on ruby

  10. 10

    Segmentation fault when trying to clone a function (in C)

  11. 11

    C Segmentation fault when spitting a string

  12. 12

    Segmentation fault: 11 when returning stuct via function pointer in struct

  13. 13

    Segmentation fault on reverse string function

  14. 14

    Segmentation Fault When Calling Function

  15. 15

    Segmentation fault when function returns

  16. 16

    Segmentation fault when calling C function with Python C API twice

  17. 17

    Segmentation Fault when retrieving value from pointer in another function

  18. 18

    Segmentation fault in C++ program using getline and string function

  19. 19

    string segmentation fault when reading lines from text file

  20. 20

    C : Segmentation Fault when move main function to new file

  21. 21

    Segmentation fault in C code function

  22. 22

    Running Into Segmentation Fault When Attempting To Convert String To Double in C++

  23. 23

    segmentation Fault in C String conversion

  24. 24

    C Segmentation Fault from function that returns the maximum of an array

  25. 25

    C++ Segmentation fault when erasing from vector

  26. 26

    Returning string from C function

  27. 27

    Function that returns string is not working- segmentation fault

  28. 28

    Segmentation fault when assigning values to pointers in function

  29. 29

    Segmentation fault when calling bound member function

HotTag

Archive