When I try to compile this file by issuing the command, "g++ qr.cpp -o qr" The system hangs. I haven't seen this kind of an error anywhere else.
#include<iostream>
using namespace std;
bool win[1000000001];
bool know[1000000001];
int sixes[] = {6, 36, 216, 1296, 7776, 46656, 279936, 1679616, 10077696, 60466176, 362797056};
bool check(int n){
cout << n << endl;
if(!know[n]){
bool b = check(n-1);
for(int i=0; i<11; i++){
if(n > sixes[i]){
b = b & check(n-sixes[i]);
}
}
win[n] = !b;
}
return win[n];
}
int main(){
win[1] = know[1] = true;
for(int j=0; j<11; j++){
win[sixes[j]] = know[sixes[j]] = true;
}
int n = 1;
cin >> n;
int i = 0;
while(n != 0){
i++;
win[n] = check(n);
cout << i << (win[n]?"-Heckle":"-Jeckle");
cin >> n;
if(n!=0) cout << endl;
}
return 0;
}
My compiler version information is given below.
yasith#vostro:~/Dropbox/Shared$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --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.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --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.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)
Do you realize how big these are?
bool win[1000000001];
bool know[1000000001];
Those are at least 1GB each!!! You're gonna want to allocate them dynamically...
It compiles fine with g++ 4.6.1 on my Debian system, which only has 1GB of memory.
I tried looking at the memory used by the various passes of the compiler and the linker when changing the size of the arrays, and the memory use didn't change much, indicating the compiler wasn't trying to allocate any data-structures proportional to the array size.
However, I have the new GNU linker "gold" installed.
I then tried it again, using the older ("BFD-based") GNU linker, which is still the default on many systems, for the link step—and then my system started thrashing like crazy (I had to kill the linker process)!
So it seems that the new gold linker is smarter about big arrays than the older linker.
On Debian, gold can be installed as the system linker by just installing the "binutils-gold" package. [I don't know if Ubuntu has the same package, but as Ubuntu is based on Debian, it seems likely.]
You're allocating 2GB of static space. Try changing the size and recompiling?
bool win[1000000001];
bool know[1000000001];
The code, while a bit insane, should not hang the compiler. If it's a true hang (i.e. more than a few minutes), report it as a GCC bug.
Related
I'm testing the following piece of code
#include <string>
int main(int argc, const char *argv[])
{
const size_t size = strtoull(argv[1], nullptr, 10);
for (int i = 0; i < 100000000; ++i)
{
std::string str;
str.reserve(size);
for (size_t j = 0; j < size; ++j)
{
str += 'x';
}
}
return 0;
}
I compile it with -O3
g++ string-append.cpp -O3 -o string-append
Now when running with parameter 15 the program appears slower then when running with parameter 16.
$ time ./string-append 15
real 0m4.342s
user 0m4.342s
sys 0m0.000s
$ time ./string-append 16
real 0m3.112s
user 0m3.112s
sys 0m0.000s
I have verified with valgrind that:
15: no allocations for the string
16: 100M allocations and 100M deallocations
Using printouts I verified that:
15: str.c_str() returns always the same address
16: str.c_str() returns different address every time
Using perf I verified that 16 gives more:
L1 cache loads
L1 cache stores
L1 cache misses
branches
branches misses
instructions (almost twice more)
What is the reason that the code works faster for size 16?
Compiler is gcc version 9.4.0.
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.4.0-1ubuntu1~20.04.1' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-9-Av3uEd/gcc-9-9.4.0/debian/tmp-nvptx/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
Checked on Clang13 - it does not seem to have this problem.
I try to make minimal buffer for std::snprintf to convert uint64_t to std::string and found error in converted string for big values
#include <cstdio>
#include <inttypes.h>
#include <limits>
#include <string>
#include <cstdint>
// assume this is maximum string size to represent any unsigned integer
// with 1 extra byte for string terminating null character
const std::size_t MAX_UINT64_WIDTH =
std::numeric_limits<std::uint64_t>::digits10 + 1;
int main(int, char**)
{
uint64_t value = 10244450920698242790ULL;
const std::string svalue = "10244450920698242790";
std::string str;
char buf[MAX_UINT64_WIDTH];
auto l = std::snprintf(buf, MAX_UINT64_WIDTH, "%" PRIu64, value);
if (l > 0) {
str.assign(buf, buf + l);
}
assert(svalue == str); //assertion failed, the last character '0' (0x30) is replaced with '\0' (0x00)
return 0;
}
Then I found that assert(21 == MAX_UINT64_WIDTH); also fails. And finally minimal program shows up error is
#include <limits>
#include <iostream>
#include <string>
#include <cstdint>
int main(int, char**) {
std::cout << std::numeric_limits<std::uint64_t>::max() << std::endl;
std::cout << std::numeric_limits<std::uint64_t>::digits10 << std::endl;
std::cout << std::to_string(std::numeric_limits<std::uint64_t>::max()).size() << std::endl;
return 0;
}
user#host:~$ g++ --std=c++11 -o check check.cpp
user#host:~$ ./check
18446744073709551615
19
20
user#host:~$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.5' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --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 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)
Update
I misuse library digits10 so I rename my question.
digits10 is the number of digits of a number that you can "safely" store in that type. If for example std::numeric_limits<std::uint64_t>::max() is 18446744073709551615 then you cannot store a number with same number of digits and all digits equal to 9 in an uint64_t.
Lets say, if the max has N digits, then digits10 is typically N-1 (unless max is 99999...9999).
I noted that a regular expression, containing two patterns with OR condition, do not match a sample string if the first pattern is a beginning part of the second pattern (tested on clang 3.5 and clang 3.8):
std::regex_match("ab", std::regex("(ab|a)")) == true
but
std::regex_match("ab", std::regex("(a|ab)")) == false
I think true is logically correct in both cases.
Clang & OSX:
$ cat > test.cpp
#include <string>
#include <regex>
#include <iostream>
int main() {
std::cout << std::regex_match("ab", std::regex("(a|ab)")) << std::endl;
std::cout << std::regex_match("ab", std::regex("(ab|a)")) << std::endl;
return 0;
}
^C
$ clang++ -v
Apple LLVM version 8.1.0 (clang-802.0.41)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ clang++ ./test.cpp -o test
$ ./test
0
1
Clang & FreeBSD:
$ cat > test.cpp
#include <string>
#include <regex>
#include <iostream>
int main() {
std::cout << std::regex_match("ab", std::regex("(a|ab)")) << std::endl;
std::cout << std::regex_match("ab", std::regex("(ab|a)")) << std::endl;
return 0;
}
^C
$ clang++ -v
FreeBSD clang version 3.8.0 (tags/RELEASE_380/final 262564) (based on LLVM 3.8.0)
Target: x86_64-unknown-freebsd11.0
Thread model: posix
InstalledDir: /usr/bin
$ clang++ ./test.cpp -o test
$ ./test
0
1
Linux & GCC:
$ cat > test.cpp
#include <string>
#include <regex>
#include <iostream>
int main() {
std::cout << std::regex_match("ab", std::regex("(a|ab)")) << std::endl;
std::cout << std::regex_match("ab", std::regex("(ab|a)")) << std::endl;
return 0;
}
^C
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.1-2ubuntu1~16.04' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --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 5.4.1 20160904 (Ubuntu 5.4.1-2ubuntu1~16.04)
$ g++ -std=gnu++11 ./test.cpp -o test
$ ./test
1
1
ECMAScript (the default regex syntax) attempts to match the alternatives in order, stopping at the first success, which means that in ordinary searching (a la regex_search) the regex a|ab never matches the whole ab; it always matches just the a part.
The standard was ambiguous about what regex_match was supposed to do in this case, leading to implementation divergence. See LWG issue 2273 for the competing interpretations. Eventually the standard was amended (see the resolution of that issue) to make clear that regex_match only considers potential matches that match the entire input sequence, as the example added to the standard makes clear:
std::regex re("Get|GetValue");
std::cmatch m;
regex_search("GetValue", m, re); // returns true, and m[0] contains "Get"
regex_match ("GetValue", m, re); // returns true, and m[0] contains "GetValue"
The original <regex> implementation in libc++ used the other interpretation, however, and it simply wasn't updated to match the resolution until recently. Clang 4.0 now prints 1 1.
#include <iostream>
#include <cilk/cilk.h>
int fib(int n)
{
if (n < 2)
return n;
int x = cilk_spawn fib(n-1);
int y = fib(n-2);
cilk_sync;
return x + y;
}
int main(int argc, char ** argv)
{
for (int i = 0; i != 20; ++i)
{
std::cout << "fib(i)=" << fib(i) << std::endl;
}
}
compiled like so can't find the library
$ g++ -Wall -O3 -fcilkplus fib.cpp -o fib
/opt/rh/devtoolset-4/root/usr/libexec/gcc/x86_64-redhat-linux/5.2.1/ld: cannot find -lcilkrts
collect2: error: ld returned 1 exit status
So I try find and adding it with the -L switch
$ find /opt/rh/devtoolset-4 -name "cilkrts"
/opt/rh/devtoolset-4/root/usr/lib/gcc/x86_64-redhat-linux/5.2.1/32/libcilkrts.a
/opt/rh/devtoolset-4/root/usr/lib/gcc/x86_64-redhat-linux/5.2.1/32/libcilkrts.so
/opt/rh/devtoolset-4/root/usr/lib/gcc/x86_64-redhat-linux/5.2.1/libcilkrts.spec
$ g++ -Wall -O3 -fcilkplus fib.cpp -o fib -L/opt/rh/devtoolset-4/root/usr/lib/gcc/x86_64-redhat-linux/5.2.1/32
/opt/rh/devtoolset-4/root/usr/libexec/gcc/x86_64-redhat-linux/5.2.1/ld: skipping incompatible /opt/rh/devtoolset-4/root/usr/lib/gcc/x86_64-redhat-linux/5.2.1/32/libstdc++.so when searching for -lstdc++
/opt/rh/devtoolset-4/root/usr/libexec/gcc/x86_64-redhat-linux/5.2.1/ld: skipping incompatible /opt/rh/devtoolset-4/root/usr/lib/gcc/x86_64-redhat-linux/5.2.1/32/libcilkrts.so when searching for -lcilkrts
/opt/rh/devtoolset-4/root/usr/libexec/gcc/x86_64-redhat-linux/5.2.1/ld: cannot find -lcilkrts
/opt/rh/devtoolset-4/root/usr/libexec/gcc/x86_64-redhat-linux/5.2.1/ld: skipping incompatible /opt/rh/devtoolset-4/root/usr/lib/gcc/x86_64-redhat-linux/5.2.1/32/libgcc_s.so when searching for -lgcc_s
/opt/rh/devtoolset-4/root/usr/libexec/gcc/x86_64-redhat-linux/5.2.1/ld: skipping incompatible /opt/rh/devtoolset-4/root/usr/lib/gcc/x86_64-redhat-linux/5.2.1/32/libgcc.a when searching for libgcc.a
collect2: error: ld returned 1 exit status
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-4/root/usr/libexec/gcc/x86_64-redhat-linux/5.2.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-4/root/usr --mandir=/opt/rh/devtoolset-4/root/usr/share/man --infodir=/opt/rh/devtoolset-4/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --disable-libgcj --with-default-libstdcxx-abi=gcc4-compatible --with-isl=/builddir/build/BUILD/gcc-5.2.1-20150902/obj-x86_64-redhat-linux/isl-install --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 5.2.1 20150902 (Red Hat 5.2.1-2) (GCC)
Pointers appreciated
If I asked it I guess I should post the answer when I work it out:
sudo yum install devtoolset-4-libcilkrts-devel
is the missing piece.
I have been having this problem for weeks and have not been able to find a solution.
When trying to use Eclipse or trying to compile with plain GCC or G++ through a terminal, there are a number of standard functions (plus the "NULL" variable) that are not recognized, including the to_string method.
I have attempted to add many different headers to the program in an attempt to find the correct files, but to no avail. I have tried #including <string>, <stdio.h>, <stddef.h>, <cstdlib>, and pretty much any other standard header I could find in forum posts that might contain the to_String function. I even tried all of these #includes with AND without the ".h" extension. Still, not matter what I tried, to_string and NULL were not recognized (among other methods).
I've been through many forums and many forum posts and tried many solutions, including this one, this one, this one, this one, this one, and more. Still, I have found no solution.
I have tried uninstalling and reinstalling Eclipse C.D.T., Eclipse as a whole, GCC, and G++. I have tried adding -std=c++11 and/or -std=c++99 flags to the GCC command or g++ command. I have tried building in Eclipse with Linux GCC, Cross GCC, and other versions of GCC.
I am running Eclipse 3.8 with both J.D.T. and C.D.T. packages installed on 64-bit Linux Mint 16 Petra.
If anyone could help me resolve this issue, "grateful" would not properly express my gratitude toward you.
EDIT:
Here is the output of gcc -v:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.8.1-10ubuntu9' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --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.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu9)
Here's a sample of the code the code that is having errors. Please keep in mind that I have attempted to add other #includes not currently shown and I have reason to believe that a missing #include is not the problem.
#include <string>
#include <stdio.h>
#include <iostream>
using namespace std;
int Month[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
string x;
class Date_Time {
private:
int minute, hour, day, month, year;
public:
Date_Time(int minute, int hour, int day, int month, int year) {
this->minute = minute;
this->hour = hour;
this->day = day;
this->month = month;
this->year = year;
}
string toString() {
string min = to_string(minute);
if (min.length() == 1)
min = '0' + min;
return to_string(month) + "/" + to_string(day) + "/" + to_string(year)
+ " " + to_string(hour) + ":" + min;
}
void addMinutes(int min) {
minute = min + minute;
if (minute > 59) {
minute = minute - 60;
hour = hour + 1;
}
if (hour > 24) {
hour = hour - 24;
day = day + 1;
}
if (day > Month[month])
day = day - Month[month];
month = month + 1;
if (month > 12) {
month = 1;
year = year + 1;
}
}
int getYear() {
return year;
}
int getMonth() {
return month;
}
int getDay() {
return day;
}
int getHour() {
return hour;
}
int getMinutes() {
return minute;
}
};
EDIT: The summary of the comments below is that we have determined that the cause is that Eclipse is not recognizing C++11 standard functions and keywords. I installed NetBeans on my system with their C/C++ plugin and the exact same errors are occurring. I can't seem to get NetBeans or Eclipse to recognize C++11 features despite adding the -std=c++11 flag in multiple places in the project configurations.
As of right now, Eclipse can finally compile without giving C++11 errors; however, in the code window, C++11 functions and features are still marked as errors with a red underline, so the issue is still not completely resolved. All I need is for someone to tell me how to get Eclispe and/or NetBeans to recognize C++11 features in its error parsers.
Thanks again in advance for any help you may be able to provide.
Try to compile your source code with
g++ -std=c++11 ...
According to to_string(), it is a C++ 2011 feature.
In Eclipse Kepler, you could set this by selecting
Project >> Properties >> C/C++ Build >> Settings >> Tool Settings >> GCC C++ Compiler >> Dialect
and choosing ISO C++11 (-std=c++0x) for Language standard.
And you should choose [All configurations] for Configuration at C/C++ Build.