Stl map simple insert memory issue - c++

Not clear what is going on: I have the 2 following lines that works in other parts of the code:
map<short,string> params;
params.insert( std::pair<short, string>(5, string("ee")) );
when I put it into my routine, I get this valgring error right at the beginning, and the program crashes. Not totally clear why it ends up in the string destructor. I may have something wrong with my binary but I am a little confused why it happens right at the beginning and why is it in the string destructor ? Does what is herebelow MEANS that I must look for the error somewhere else ?
==2052== Conditional jump or move depends on uninitialised value(s)
==2052== at 0x6073EC2: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/libstdc++.so.6.0.13)
==2052== by 0x42428B: std::pair<short const, std::string>::~pair() (stl_pair.h:68)
==2052== by 0x4B99FB: NLGSearch::Search::Calc() (Search.cpp:409)
Note that it goes through if I use map<short,short>
Thank you.
--------------------------------------------------- EDIT
This is used right at the beginning of a method. I guess I have the answer to my question, I have to look at somewhere else in my code...
lef#dxml:~$ more /etc/issue
Debian GNU/Linux 6.0 \n \l
lef#dxml:~$ g++ -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 (Debian 4.4.5-8)

It works for me. How does this program differ from the one you are testing?
#include <map>
#include <string>
int main () {
using std::map;
using std::string;
map<short,string> params;
params.insert( std::pair<short, string>(5, string("ee")) );
}
gcc 4.4.3 on Ubuntu 10.04.3 LTS. No complaints from valgrind.

Related

GCC v12.1 warning about serial compilation

I have upgraded my whole arch linux system today (12th May, 2022). gcc was also upgraded from v11.2 to v12.1. I tried compiling some of my programs with g++ (part of gcc compiler collection) by the following command:
g++ -O3 -DNDEBUG -Os -Ofast -Og -s -march=native -flto -funroll-all-loops -std=c++20 main.cc -o ./main
The program compiled perfectly and ran as excepted without any errors, but I got a warning:
lto-wrapper: warning: using serial compilation of 2 LTRANS jobs
But, when the same program was compiled using v11.2 it produces zero number of errors and warnings.
My Questions:
What is the meaning of this warning?
How can I fix this?
Is this warning occurred due to upgrading gcc version to v12.1
Here's the g++ configuration on my machine:
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror --with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.1.0 (GCC)
Apparently that is a recent change in the -flto option. With a little bit of variation in the google search I was able to find this mail conversation:
Likewise if people just use -flto and auto-detection
finds nothing:
warning: using serial compilation of N LTRANS jobs
note: refer to http://.... for how to use parallel compile
[...]
That is, teach users rather than second-guessing and eventually
blowing things up. IMHO only the jobserver mode is safe to
automatically use.
So this is about using the -flto options correctly. I could not manage to easily get a GCC 12 on my system and thus could not try it myself, but you can try -flto=1 or -flto=auto to get rid of the warning.
Anyway it seems that this warning is rather harmless. It just tells you that GCC uses 2 threads in parallel to do the link time optimization.
The exact semantics and effects of the -flto is (together with the other optimization options) described in detail in the GCC manual. By the way you should not spam optimization options like you do in your command line. For example specifying multiple -O... options will only have the effect of the last one of them. Unless you know exactly what you are doing and have carefully read the manual, just stick to use -O3 and you will be fine.

Compiling with arm-fsl-linux-gnueabi-gcc: int64_t is 32 bit long

I'm cross compiling for imx28 cpu with arm-fsl-linux-gnueabi:
[vladimir#vladimir src]$ arm-fsl-linux-gnueabi-g++ -v
Using built-in specs.
Target: arm-fsl-linux-gnueabi
Configured with: /work/arm-toolchains/tmp/src/gcc-4.4.4/configure --build=i686-build_pc-linux-gnu --host=i686-build_pc-linux-gnu --target=arm-fsl-linux-gnueabi --prefix=/work/arm_fsl_gcc_4.4.4_multilib --with-sysroot=/work/arm_fsl_gcc_4.4.4_multilib/arm-fsl-linux-gnueabi/multi-libs --enable-languages=c,c++ --with-pkgversion=4.4.4_09.06.2010 --enable-__cxa_atexit --disable-libmudflap --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --with-gmp=/work/arm-toolchains/tmp/arm-fsl-linux-gnueabi/build/static --with-mpfr=/work/arm-toolchains/tmp/arm-fsl-linux-gnueabi/build/static --with-ppl=/work/arm-toolchains/tmp/arm-fsl-linux-gnueabi/build/static --with-cloog=/work/arm-toolchains/tmp/arm-fsl-linux-gnueabi/build/static --enable-threads=posix --enable-target-optspace --with-local-prefix=/work/arm_fsl_gcc_4.4.4_multilib/arm-fsl-linux-gnueabi/multi-libs --disable-nls --enable-symvers=gnu --enable-c99 --enable-long-long --enable-multilib --with-system-zlib --enable-lto
Thread model: posix
gcc version 4.4.4 (4.4.4_09.06.2010)
The following code line:
info->mask = 0xffffffffffffffff;
gives me following error:
warning: integer constant is too large for 'long' type
I tried close to everything, made sure int64_t is defined to long long int, even tried to typedef it manually, still, it gives me the error.
I also tried different -mcpu options.
Is there even a way to use int64_t on imx28, or should I just give up?
Based on your comment.
Try 0xFFFFFFFFFFFFFFFFLL or -1

C++ multithreading errors

I have been starting to try and learn how to utilize multithreading in c++, but the #include<thread> causes issues with the thread type being not declared in the scope. error: 'thread' was not declared in this scope. I have been doing research, and I have come across a lot of answers regarding how to solve this issue. Im presently at the understanding that my compiler, MinGW, doesn't support thread effectively, but I am not sure what to do with that information.
Any guidance on this matter is appreciated.
Also, I think this may be helpful. If I run gcc -v on my command line, I get this output:
Using built-in specs.
COLLECT_GCC=c:\MinGW\bin\gcc.exe
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --with-gmp=/mingw --with-mpfr --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
edit: I've seen this webpage, https://github.com/meganz/mingw-std-threads, as a potential solution, but I don't think this works for me. Unless somehow I am putting the mingw-thread.h in the wrong folder.
I think the problem here is you did not tell the compiler using c++11 features. 'thread' is belonged to c++11 features, let try adding -std=c++11 into CXXFLAGS or CPPFLAGS and see if it resolves your problem
My psychic powers suggest your forgot the std:: namespace attribute. That would explain why thread is undefined, even with #include <thread>. The other answer about -std=c++11 is also steering you into the right direction. And don't forget the -pthread compiler/linker option.
$ cat foo.cpp
#include <thread>
#include <iostream>
void threadfunc()
{
std::cout << "Hello from the worker thread" << std::endl;
}
int main()
{
std::thread t(threadfunc);
t.join();
return 0;
}
$ g++ foo.cpp -std=c++11 -o foo -pthread
$ ./foo
Hello from the worker thread

Boost 1.53 and 1.58 difference

Following is a small and simple piece of code I wrote :
#include<iostream>
#include<boost/filesystem.hpp>
using namespace std;
namespace fs = boost::filesystem;
int main(int argc, char** argv)
{
fs::directory_iterator it((fs::path(argv[1])));
fs::directory_iterator endit;
while(it != endit)
{
cout<<it->path().string()<<endl;
++it;
}
}
I then wrote a very simple CMakeLists.txt file as follows :
cmake_minimum_required(VERSION 2.8)
project(test)
find_package(Boost COMPONENTS filesystem system REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
add_executable(test test.cpp)
target_link_libraries(test ${Boost_LIBRARIES})
When I compile this code on OS X Yosemite, it runs perfectly.
The compiler details are as follows :
$ g++ -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix
I then ran it on a Fedora 19 system and it did produce errors during compilation. The error message is very long. My project involves a number of source files, but for this problem, a part of the error message is sufficient to understand the problem :
/user/uujjwal/home/code/pom-final/test/abc.cpp: In function 'int main(int, char**)':
/user/uujjwal/home/code/pom-final/test/abc.cpp:13:10: error: no match for 'operator!=' (operand types are 'boost::filesystem::directory_iterator(boost::filesystem::path*)' and 'boost::filesystem::directory_iterator')
while(it!=endit)
Thus it shows that the operator != is invalid.
The compiler details on Fedora system are :
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.2/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-isl=/builddir/build/BUILD/gcc-4.8.2-20131017/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.2-20131017/obj-x86_64-redhat-linux/cloog-install --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.2 20131017 (Red Hat 4.8.2-1) (GCC)
Boost version on OS X - 1.58
Boost version on Fedora System - 1.53
I want to know that is this owing to a fundamental difference between the two versions ? Is the reason something else ? If I have to make some changes in the code, to make it portable, what is the right way ?
****PS :-I also noticed out of experimentation that on Fedora if , do the following**
fs::path root_path(argv[1]);
fs::directory_iterator it(root_path);
it then compiles. So where is the difference coming and why ?**
The LHS operand type in your error message is boost::filesystem::directory_iterator(boost::filesystem::path*). This indicates that you're experiencing a most vexing parse error; the declaration of it is being parsed as a function declaration rather than a local variable declaration-instantiation:
fs::directory_iterator it(fs::path(argv[1]));
This is parsed as the declaration of an extern function named it, taking one argument (named argv) of type fs::path[1] (fs::path* after decay) and returning fs::directory_iterator.
What's odd is that your extra parentheses should have prevented the MVP error. Either your version of gcc is buggy and ignored them, or something changed between the code you're presenting and what was passed to the compiler.
fs::directory_iterator it((fs::path(argv[1])));
// extra parentheses ^ ^ to prevent MVP

Can't compile C++ file.cpp. C++98 mode

I'm new to C++. When I write
for (char* c : v)
{
cout << c;
}
I get
"range-based ‘for’ loops are not allowed in C++98 mode"
As far as I understand, I have to change my GCC version (or just mode?). My g++ -v:
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.7/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.2-2ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1) `
Can't compile from vim (with c.vim plugin), sublime text3 and from terminal using gcc program.cpp and gcc -pedantic -std=c99 program.cpp.
I've downloaded gcc 4.8.1 but It's not deb package so can't install it properly. Also heard about C++11, C++14, C++98, C++99. Where/how can I get/install the latest version?
Solution for vim plugin c.vim:
Edit "c.vim/plugin/c.vim". Change this line
let s:C_CplusCFlags = '-Wall -g -O0 -c -std=c++11' " C++ compiler flags: compile, don't optimize
I've added "-std=c++11" and it works.
The range based for loop is part of C++11, so you will need to use -std=c++11 with G++ to enable the C++11 features.
As mentioned in the previous answer, you are attempting to use a feature of the latest C++ standard (called C++11) while compiling for older standard.
C++11 is the latest C++ standard and the only one supporting range based for.
Now you need to distinguish between the C++ standard and the compiler support for that standard.
Along the past few years, support for C++11 features was gradually added to the gcc compiler.
The following link shows which C++11 feature is supported by which gcc version:
C++0x/C++11 Support in GCC
As you can see, range based for was added in gcc 4.6, so you do not need gcc 4.8 in order to use this feature - gcc 4.6 or later will suffice.
When compiling, you will also need to tell the compiler which standard to compile against. The -std=whatever tells the compiler which standard to use.
You are currently using "-std=c99", telling the compiler to compile using an old C++ standard. Instead you need to set this flag to c++11. If this doesn't work on the gcc version you are using, try using "-std=c++0x" instead (C++0x is an old name of the C++11 standard.)