0

Why in a class constructor method the std::string param is not quite like it in other place?

For example:

class MyIssue{

public:
MyIssue(std::string str){
return;       // 3.
}
}

void test(std::string str){
    return; // 2.
}


auto str = std::string("123");  // 1.
test("123");
MyIssue my_issue("123");

When I debug it, the 1., 2. lines show the str is "123".

But the 3. line shows the str is "\336\223\000..."(it's a HUGE size value. I CANT EVEN EVALUATE IT USING GDB) .

So could you guys please tell me why , it's my mistake or a feature? Is there any attention I should pay when I using it?

--------Add some info--------

I am using Clion(Windows) + MinGW to build the project. And I did not ever change the settings of cmake files instead of adding a gtest library and creating a new target to run.

And now I create a whole new empty project also by Clion(Windows) + MinGW. And I copy the above code to it.

The 3. line debug result becomes ""(empty). No matter what methods I use like "erase" 、 "cout" ect in the constructor. The debug val still shows "". Like it shows "/xxx/yyy" style in the previous project.

I am not quite sure about how CLion set the compile settings. I assume they are here in CMakeCCompiler.cmake and CMakeCCXXompiler.cmake. These 2 is the two of my previous project.

set(CMAKE_C_COMPILER "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.exe")
set(CMAKE_C_COMPILER_ARG1 "")
set(CMAKE_C_COMPILER_ID "GNU")
set(CMAKE_C_COMPILER_VERSION "8.1.0")
set(CMAKE_C_COMPILER_VERSION_INTERNAL "")
set(CMAKE_C_COMPILER_WRAPPER "")
set(CMAKE_C_STANDARD_COMPUTED_DEFAULT "11")
set(CMAKE_C_COMPILE_FEATURES "c_std_90;c_function_prototypes;c_std_99;c_restrict;c_variadic_macros;c_std_11;c_static_assert")
set(CMAKE_C90_COMPILE_FEATURES "c_std_90;c_function_prototypes")
set(CMAKE_C99_COMPILE_FEATURES "c_std_99;c_restrict;c_variadic_macros")
set(CMAKE_C11_COMPILE_FEATURES "c_std_11;c_static_assert")

set(CMAKE_C_PLATFORM_ID "MinGW")
set(CMAKE_C_SIMULATE_ID "")
set(CMAKE_C_SIMULATE_VERSION "")



set(CMAKE_AR "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/ar.exe")
set(CMAKE_C_COMPILER_AR "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc-ar.exe")
set(CMAKE_RANLIB "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/ranlib.exe")
set(CMAKE_C_COMPILER_RANLIB "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc-ranlib.exe")
set(CMAKE_LINKER "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/ld.exe")
set(CMAKE_COMPILER_IS_GNUCC 1)
set(CMAKE_C_COMPILER_LOADED 1)
set(CMAKE_C_COMPILER_WORKS TRUE)
set(CMAKE_C_ABI_COMPILED TRUE)
set(CMAKE_COMPILER_IS_MINGW 1)
set(CMAKE_COMPILER_IS_CYGWIN )
if(CMAKE_COMPILER_IS_CYGWIN)
  set(CYGWIN 1)
  set(UNIX 1)
endif()

set(CMAKE_C_COMPILER_ENV_VAR "CC")

if(CMAKE_COMPILER_IS_MINGW)
  set(MINGW 1)
endif()
set(CMAKE_C_COMPILER_ID_RUN 1)
set(CMAKE_C_SOURCE_FILE_EXTENSIONS c;m)
set(CMAKE_C_IGNORE_EXTENSIONS h;H;o;O;obj;OBJ;def;DEF;rc;RC)
set(CMAKE_C_LINKER_PREFERENCE 10)

# Save compiler ABI information.
set(CMAKE_C_SIZEOF_DATA_PTR "8")
set(CMAKE_C_COMPILER_ABI "")
set(CMAKE_C_LIBRARY_ARCHITECTURE "")

if(CMAKE_C_SIZEOF_DATA_PTR)
  set(CMAKE_SIZEOF_VOID_P "${CMAKE_C_SIZEOF_DATA_PTR}")
endif()

if(CMAKE_C_COMPILER_ABI)
  set(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_C_COMPILER_ABI}")
endif()

if(CMAKE_C_LIBRARY_ARCHITECTURE)
  set(CMAKE_LIBRARY_ARCHITECTURE "")
endif()

set(CMAKE_C_CL_SHOWINCLUDES_PREFIX "")
if(CMAKE_C_CL_SHOWINCLUDES_PREFIX)
  set(CMAKE_CL_SHOWINCLUDES_PREFIX "${CMAKE_C_CL_SHOWINCLUDES_PREFIX}")
endif()





set(CMAKE_C_IMPLICIT_LINK_LIBRARIES "mingw32;gcc;moldname;mingwex;pthread;advapi32;shell32;user32;kernel32;iconv;mingw32;gcc;moldname;mingwex")
set(CMAKE_C_IMPLICIT_LINK_DIRECTORIES "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0;C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc;C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/x86_64-w64-mingw32/lib;C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib")
set(CMAKE_C_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "")

set(CMAKE_CXX_COMPILER "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe")
set(CMAKE_CXX_COMPILER_ARG1 "")
set(CMAKE_CXX_COMPILER_ID "GNU")
set(CMAKE_CXX_COMPILER_VERSION "8.1.0")
set(CMAKE_CXX_COMPILER_VERSION_INTERNAL "")
set(CMAKE_CXX_COMPILER_WRAPPER "")
set(CMAKE_CXX_STANDARD_COMPUTED_DEFAULT "14")
set(CMAKE_CXX_COMPILE_FEATURES "cxx_std_98;cxx_template_template_parameters;cxx_std_11;cxx_alias_templates;cxx_alignas;cxx_alignof;cxx_attributes;cxx_auto_type;cxx_constexpr;cxx_decltype;cxx_decltype_incomplete_return_types;cxx_default_function_template_args;cxx_defaulted_functions;cxx_defaulted_move_initializers;cxx_delegating_constructors;cxx_deleted_functions;cxx_enum_forward_declarations;cxx_explicit_conversions;cxx_extended_friend_declarations;cxx_extern_templates;cxx_final;cxx_func_identifier;cxx_generalized_initializers;cxx_inheriting_constructors;cxx_inline_namespaces;cxx_lambdas;cxx_local_type_template_args;cxx_long_long_type;cxx_noexcept;cxx_nonstatic_member_init;cxx_nullptr;cxx_override;cxx_range_for;cxx_raw_string_literals;cxx_reference_qualified_functions;cxx_right_angle_brackets;cxx_rvalue_references;cxx_sizeof_member;cxx_static_assert;cxx_strong_enums;cxx_thread_local;cxx_trailing_return_types;cxx_unicode_literals;cxx_uniform_initialization;cxx_unrestricted_unions;cxx_user_literals;cxx_variadic_macros;cxx_variadic_templates;cxx_std_14;cxx_aggregate_default_initializers;cxx_attribute_deprecated;cxx_binary_literals;cxx_contextual_conversions;cxx_decltype_auto;cxx_digit_separators;cxx_generic_lambdas;cxx_lambda_init_captures;cxx_relaxed_constexpr;cxx_return_type_deduction;cxx_variable_templates;cxx_std_17;cxx_std_20")
set(CMAKE_CXX98_COMPILE_FEATURES "cxx_std_98;cxx_template_template_parameters")
set(CMAKE_CXX11_COMPILE_FEATURES "cxx_std_11;cxx_alias_templates;cxx_alignas;cxx_alignof;cxx_attributes;cxx_auto_type;cxx_constexpr;cxx_decltype;cxx_decltype_incomplete_return_types;cxx_default_function_template_args;cxx_defaulted_functions;cxx_defaulted_move_initializers;cxx_delegating_constructors;cxx_deleted_functions;cxx_enum_forward_declarations;cxx_explicit_conversions;cxx_extended_friend_declarations;cxx_extern_templates;cxx_final;cxx_func_identifier;cxx_generalized_initializers;cxx_inheriting_constructors;cxx_inline_namespaces;cxx_lambdas;cxx_local_type_template_args;cxx_long_long_type;cxx_noexcept;cxx_nonstatic_member_init;cxx_nullptr;cxx_override;cxx_range_for;cxx_raw_string_literals;cxx_reference_qualified_functions;cxx_right_angle_brackets;cxx_rvalue_references;cxx_sizeof_member;cxx_static_assert;cxx_strong_enums;cxx_thread_local;cxx_trailing_return_types;cxx_unicode_literals;cxx_uniform_initialization;cxx_unrestricted_unions;cxx_user_literals;cxx_variadic_macros;cxx_variadic_templates")
set(CMAKE_CXX14_COMPILE_FEATURES "cxx_std_14;cxx_aggregate_default_initializers;cxx_attribute_deprecated;cxx_binary_literals;cxx_contextual_conversions;cxx_decltype_auto;cxx_digit_separators;cxx_generic_lambdas;cxx_lambda_init_captures;cxx_relaxed_constexpr;cxx_return_type_deduction;cxx_variable_templates")
set(CMAKE_CXX17_COMPILE_FEATURES "cxx_std_17")
set(CMAKE_CXX20_COMPILE_FEATURES "cxx_std_20")

set(CMAKE_CXX_PLATFORM_ID "MinGW")
set(CMAKE_CXX_SIMULATE_ID "")
set(CMAKE_CXX_SIMULATE_VERSION "")



set(CMAKE_AR "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/ar.exe")
set(CMAKE_CXX_COMPILER_AR "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc-ar.exe")
set(CMAKE_RANLIB "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/ranlib.exe")
set(CMAKE_CXX_COMPILER_RANLIB "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc-ranlib.exe")
set(CMAKE_LINKER "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/ld.exe")
set(CMAKE_COMPILER_IS_GNUCXX 1)
set(CMAKE_CXX_COMPILER_LOADED 1)
set(CMAKE_CXX_COMPILER_WORKS TRUE)
set(CMAKE_CXX_ABI_COMPILED TRUE)
set(CMAKE_COMPILER_IS_MINGW 1)
set(CMAKE_COMPILER_IS_CYGWIN )
if(CMAKE_COMPILER_IS_CYGWIN)
  set(CYGWIN 1)
  set(UNIX 1)
endif()

set(CMAKE_CXX_COMPILER_ENV_VAR "CXX")

if(CMAKE_COMPILER_IS_MINGW)
  set(MINGW 1)
endif()
set(CMAKE_CXX_COMPILER_ID_RUN 1)
set(CMAKE_CXX_IGNORE_EXTENSIONS inl;h;hpp;HPP;H;o;O;obj;OBJ;def;DEF;rc;RC)
set(CMAKE_CXX_SOURCE_FILE_EXTENSIONS C;M;c++;cc;cpp;cxx;mm;CPP)
set(CMAKE_CXX_LINKER_PREFERENCE 30)
set(CMAKE_CXX_LINKER_PREFERENCE_PROPAGATES 1)

# Save compiler ABI information.
set(CMAKE_CXX_SIZEOF_DATA_PTR "8")
set(CMAKE_CXX_COMPILER_ABI "")
set(CMAKE_CXX_LIBRARY_ARCHITECTURE "")

if(CMAKE_CXX_SIZEOF_DATA_PTR)
  set(CMAKE_SIZEOF_VOID_P "${CMAKE_CXX_SIZEOF_DATA_PTR}")
endif()

if(CMAKE_CXX_COMPILER_ABI)
  set(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_CXX_COMPILER_ABI}")
endif()

if(CMAKE_CXX_LIBRARY_ARCHITECTURE)
  set(CMAKE_LIBRARY_ARCHITECTURE "")
endif()

set(CMAKE_CXX_CL_SHOWINCLUDES_PREFIX "")
if(CMAKE_CXX_CL_SHOWINCLUDES_PREFIX)
  set(CMAKE_CL_SHOWINCLUDES_PREFIX "${CMAKE_CXX_CL_SHOWINCLUDES_PREFIX}")
endif()





set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "stdc++;mingw32;gcc_s;gcc;moldname;mingwex;pthread;advapi32;shell32;user32;kernel32;iconv;mingw32;gcc_s;gcc;moldname;mingwex")
set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0;C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc;C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/x86_64-w64-mingw32/lib;C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib")
set(CMAKE_CXX_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "")

Anon
  • 214
  • 1
  • 11
  • 5
    Have you tried lowering your compiler's optimization level? Especially since the strings are never being interacted with after initialization, it could cause the inconsistent behavior you're seeing when debugging. You could also add a test method to print the strings in each respective case to confirm nothing weird is happening. – Bobby Tables Apr 16 '19 at 15:15
  • 1
    Unused variables with no side-effect might be subject of optimization. With side-effect, I couldn't reproduce (but I didn't try in debugger): [**Live Demo on coliru**](http://coliru.stacked-crooked.com/a/452f6fc775b05870) – Scheff's Cat Apr 16 '19 at 15:17
  • @Scheff I mean using `(gdb) p` instead of printing its val. Is it a compiler behavior? Can I turn it off? – Anon Apr 16 '19 at 15:21
  • I know. But adding `std::cout` I made a _side-effect_ which prevents optimization (if this was the reason). You may try my code on your side and look whether it helps. – Scheff's Cat Apr 16 '19 at 15:22
  • It's a while ago I last used gdb. In VS2013 (which has a nice and powerful visual debugger built-in), I recognized that I have to go a certain number of steps into the constructor until the arguments show reasonable values in debugger. There is not much to step in in your code. (Just another idea...) – Scheff's Cat Apr 16 '19 at 15:25
  • @BobbyTables In the constructor method I erase some parts of the string after some codes. But the `(gdb) p` result still reveals a val like "\xxx\yyy"(which is different from the first val). How can I turn off the optimization behavior? I don't think printing the val every time is a good solution. – Anon Apr 16 '19 at 15:26
  • 1
    Please, post your command to compile/link this sample. _Is it a compiler behavior? Can I turn it off?_ If it's caused by optimization then don't use `-O` and add `-g` instead. ;-) – Scheff's Cat Apr 16 '19 at 15:28
  • `MyIssue(std::string str){ return; // 3. }` - you don't actually use `str`, so I'd assume the compiler optimizes it out and there's therefore nothing to actually inspect where you expect `str` to be. – Jesper Juhl Apr 16 '19 at 15:36
  • @Anon Of course you wouldn't print the values as a permanent solution to being able to debug them later. In a normal procedure, the values will be used later and wouldn't *necessarily* be optimized away. To customize your optimization levels, you need to add flags to your compiler command. Here is the link for gcc but if you're not using gcc then you'd need to look up the options for your specific compiler. https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options In your case, you'd want to try `-Og` to see if it makes a difference. – Bobby Tables Apr 16 '19 at 15:43
  • 1
    1.) My apologize. I could reproduce what you described (with `-std=c++11 -g`, g++ 7.3 in cygwin64). 2.) After googling for a reason I found this possible duplicate [SO: Why does the print command in gdb return \035 for C++ std::strings?](https://stackoverflow.com/a/4830782/7478597). (And, trying the described workaround `p *(char**)str._M_dataplus._M_p` worked on my side.) – Scheff's Cat Apr 16 '19 at 15:58
  • 1
    May be interesting as well: [SO: How to pretty-print STL containers in GDB?](https://stackoverflow.com/q/11606048/7478597). – Scheff's Cat Apr 16 '19 at 16:03
  • @Scheff Thanks it works. Hope it's only a bug of my gdb. – Anon Apr 16 '19 at 16:14
  • 1
    Did you try the recommendation of [Fei's answer](https://stackoverflow.com/a/13132091/7478597)? To me, it sounds like it isn't a bug in gdb but just an additional configuration which has to be done. (In VS2013, we had to add specific visualizers as well to get some of our own classes printed properly.) – Scheff's Cat Apr 16 '19 at 16:18

1 Answers1

0

With a long time testing I finally figure out that :

This is a gcc compiler issue instead of the gdb.

I have tested the project with following configure.

  1. Clang-1001.0.46.3(LLVM 10.0.1) & lldb(1001.0.12.1) & XCode【OK】
  2. MSVC Tool set(v141) & Visual Studio 2017 【OK】
  3. MinGW-w64 & gcc8.1 & gdb8.1 & CLion 【wrong debug result】
  4. Cygwin & g++7.4 / gcc7.4 & gdb 8.1.1 & CLion【wrong debug result】
  5. Cygwin & g++7.3 / gcc7.3 & gdb 8.1.1 & CLion【wrong debug result】
  6. Cygwin & g++8.3 / gcc8.3 & gdb 8.1.1 & CLion【OK】

So I assume that it's only a gcc bug and has been fixed in the latest version.

Anon
  • 214
  • 1
  • 11