I noticed that the following code is extremely slow to compile: (It can't even finish on my computer)
#include <complex>
struct some_big_struct {
std::complex <double> a[1000000][2];
};
some_big_struct a;
int main () {
return 0;
}
Out of curiosity, I've also tried other alternatives of the code. However, these codes seem to compile just fine on my computer :
#include <complex>
struct some_big_struct {
double a[1000000][2];
};
some_big_struct a;
int main () {
return 0;
}
and
#include <complex>
std::complex <double> a[1000000][2];
int main () {
return 0;
}
I wonder if anyone can share some insight on why such is the case. Thanks!
The compiler is probably running the default std::complex constructor while compiling, so that it can put the initialized values of all the array members into the executable, rather than generate code that performs this loop when the program starts. So it's calling the constructor 2 million times while compiling.
Related
Shortened code I wrote for learning purposes. Still the way it is, it doesn't work properly.
Removing last line (&&) it works OK, but with it, code doesn't reach line noted as ##, expressing
segmentation error, after the "BazOnly" text displayed.
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
namespace nek{
class Baz{
int num;
std::string str;
public:
Baz(){cout<<"BazOnly"<<endl;}
string setstr(string& val){
//str.assign(val);
num= 7;
}
};
}
namespace drg{
class Deriv: nek::Baz{
public:
Deriv(char ch){cout<<"DerivChar"<<endl;}
};
};
int main(){
nek::Baz b;
string priv{"zik"};
b.setstr(priv);
cout<<"Passed here"<<endl; //##
drg::Deriv dc{'A'}; // &&
}
Compiled with g++ (option std=gnu++11), Ubuntu 16 on Virtual Box.
Question is what could be the reason for such behavior ?
string setstr(string& val){
//str.assign(val);
num= 7;
}
This method is declared as returning a std::string, but nothing actually gets returned from it. This is undefined behavior.
Most modern C++ compilers will warn you about this. If yours' did, this is an example of why you cannot ignore warning messages from your compiler, even if it did successfully compile your code.
I have an array of threads and in for loop I should create a thread to all of them.
The problem is that one of the parameters is std::move(promise_var) and another one is structure. When I try to compile it compile gives me an error:
error: no matching function for call to ‘std::thread::thread(void (&)(Function), Structure [nNumThreads], std::remove_reference<std::promise<const char*>&>::type)’
So, here is the simplified version of code...
func(struct Structure, std::promise<const char *> && v_Promise){
//doing work
}
main(){
std::thread a_Threads[5];
for(int8_t i = 0; i < 5; i++){
a_Threads[i] = std::threads(func, Structure, std::move(v_promise[i]));
}
}
(Not putting in a comment so that I can append the code properly) I completed the code you submitted so that it compiles and I can confirm that the code below compiles and runs fine with the command g++ test.cpp -std=c++11 -pthread:
#include <iostream>
#include <thread>
#include <future>
using namespace std;
struct Structure{
int el1;
int el2;
};
void func(struct Structure, std::promise<const char *> && v_Promise){
cout<<"Hello"<<endl;
//doing work
}
int main(){
std::thread a_Threads[5];
Structure my_struct;
std::promise<const char*> v_promise;
for(int8_t i = 0; i < 5; i++){
a_Threads[i] = std::thread(func, my_struct, std::move(v_promise));
}
}
Please cross-reference this against any typos or information that you might have omitted.
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main ()
{
struct process
{
int burst;
int ar;
};
vector<process> a;
// insert two processes
a.push_back({21, 42});
a.push_back({10, 20});
queue <process> names;
names.push(a[1]);
names.push(a[2]);
cout<<names.front().ar;
return 0;
}
The above code is working fine in eclipse but when i compile it on linux it give many errors. invalid argument and many more. i'm executing it with command:
g++ -o file_name file_name.cpp
does anyone know the reason behind these errors? any solution?
You need to move the definition of struct process out of the main function:
struct process
{
int burst;
int ar;
};
int main ()
{
...
}
Additionally, since you use initializer lists you must enable C++11 suport in GCC:
g++ -std=c++11 -o file_name file_name.cpp
You can not define a struct within a function. In your case, the struct process is defined within the main function.
Also, you need to declare the type of struct you wish to append to the vector.
the '{21, 42}' does not declare a 'process' type (Unless you use C++11).
Using local classes as template arguments and passing braced-init-list to functions are only possible as of C++11. Pass -std=c++11 or -std=c++14 option to g++ and all the errors will disappear. Note that all the letters in the option are in lower case.
It is said that arrays are allocated at compile time, then the size must be const and available at compile time.
But the following example also works, Why?
#include <iostream>
#include <vector>
using namespace::std;
int main()
{
vector<int> ivec;
int k;
while(cin>>k)
ivec.push_back(k);
int iarr[ivec.size()];
for (size_t k=0;k<ivec.size();k++)
{
iarr[k]=ivec[k];
cout<<iarr[k]<<endl;
}
return 0;
}
Compile your code with -pedantic.
Most compilers support variable length arrays through compiler extensions.
The code works due to the compiler extensions, However as you noted the code is non standard conforming and hence non portable.
So, when compiled with the basic icc bob.cpp -o bob and run, the following code segfaults:
#include <string>
int foo () {
return 6;
}
int main() {
std::string t[foo()];
}
The following two similar programs, however, seem to run fine.
#include <string>
int foo () {
return 6;
}
int main() {
int f = foo();
std::string t[f];
}
and
#include <string>
int foo () {
return 6;
}
int main() {
std::string t[6];
}
I'm a bit confused about what's going on. Apparently, variable length arrays are non-standard, and this was a surprise to me since I've always used g++ which supports it. However, if it's not supported by ICC, why would it compile? Also, why would example 2 "work"?
What is correct code here, and, if the first snippet is incorrect, why does it compile, and then why does it segfault?
I'm using icc (ICC) 12.0.2 20110112 on 2011 x86_64 Intel(R) Core(TM) i5.
Thanks
Well, while it is true that C++ has no variable-length arrays (C99 does though), apparently ICC does support them as an extension, since your code actually compiles (and since your second snippet actually runs without crashing).
If the first version crashes, then it must be a bug in ICC's implementation of that non-standard extension.