Memory fault during function return

-2

I'm facing with a "Memory fault" (QNX) when calling to:

std::string set_name( std::string name )
{
    this->name = name;
    // return this->string; // commented
}
  • or heap corruption (error 0xC0000374) (Windows MinGW)

source code: test.cc

#include <iostream>
#include <string>

class Base{
    public:
        Base()
        {
            std::cout << multiply( 100, 200 ) << std::endl;
            std::cout << set_name( "set_name" ) << std::endl;
        }
        std::string set_name( std::string name )
        {
            this->name = name;
            // return this->string; // commented
        }
        int multiply( int x, int y )
        {
            ret = x * y;
            // return ret; // commented
        }
    private:
        std::string name;
        int ret;
};

Base base;

int main(int argc, char** argv)
{
    std::cout << "exit" << std::endl;
}

Compiled: Makefile

all:
    g++ test.cc -std=c++11 -o test

Result:

// 20000
// ╚Z  шГь HН
  • Why string="╚Z шГь HН" is printed, and "exit" isn't printed?
  • It is "undefined behavior", but what really happened?
c++
string
undefined-behavior
nullptr
asked on Stack Overflow Mar 14, 2020 by nck

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0