Although the example in std::normal_distribution reference compiled fine, I got a warning on uninitialized value in a similar code below.
#include <random>
void foo(double);
int main()
{
std::default_random_engine generator;
std::normal_distribution<double> norm_dist(0,1);
for(int i=0; i<3; i++)
foo(norm_dist(generator));
return 0;
}
When compiled with "g++ -std=c++11 -Wall -ffast-math -O -c foo.cpp" using gcc5.3.1 on my CentOS7.2, I got:
foo.cpp:8:30: warning:
‘norm_dist.std::normal_distribution::_M_saved’ may be used
uninitialized in this function [-Wmaybe-uninitialized]
foo(norm_dist(generator));
I thought STL had been well tested, but why does the compiler warn? Is the problem in g++, STL, or my code?
Related
I run an up to date debian testing (with kernel 4.19).
Helpers are not found on my system (but they exist in the header, Qt jumps to them)
#include "bpf/bpf.h"
int main (){
int r = bpf_create_map(BPF_MAP_TYPE_ARRAY,1,1,1,0);
return 0;
}
Compilation results in
undefined reference to `bpf_create_map(bpf_map_type, int, int, int, unsigned int)'
compiled with
g++ -c -pipe -g -std=gnu++1z -Wall -W -fPIC -DQT_QML_DEBUG -I. -I../../Qt/5.13.0/gcc_64/mkspecs/linux-g++ -o main.o main.cpp
g++ -lbpf -o server main.o
Same result with
g++ main.cpp -lbpf -o out
I have the libbpf-dev installed as well and i have the associated libraries (a and so).
What is wrong?
Update
even the following code won't work
#include <linux/bpf.h>
int main (){
//int r = bpf_create_map(BPF_MAP_TYPE_ARRAY,1,1,1,0);
bpf_attr attr = {};
attr.map_type = BPF_MAP_TYPE_ARRAY;
attr.key_size = 1;
attr.value_size = 1;
attr.max_entries = 1;
bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
return 0;
}
results in
error: 'bpf' was not declared in this scope
Update2:
BTW, key size is mandated to be 4 and not 1; but it is a point aside, that was unrelated to my problem here.
Namespace issue due to compiling in C++, you probably want:
extern "C" {
#include "bpf/bpf.h"
}
int main()...
Regarding your second error (error: 'bpf' was not declared in this scope), this is not directly related to libbpf, this is because there is no function simply called bpf() to actually perform the syscall. Instead you have to use the syscall number. For example, libbpf defines the following:
static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
unsigned int size)
{
return syscall(__NR_bpf, cmd, attr, size);
}
... and uses sys_bpf() after that, the same way you try to call bpf() in your sample.
For the record, “BPF helpers” often designates BPF functions that you call from within a BPF program, which is not the case here. Hence some confusion in the comments, I believe.
#include <ios>
#include <iostream>
#include <map>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
map<int, int> v;
int i;
int t;
while (cin >> i) {
v[i] = t++;
}
auto mi = i;
auto mt = t;
for (const auto p : v) {
if (p.second < mt) {
mi = p.first;
mt = p.second;
}
}
cout << mi << '\n';
return 0;
}
The abovementioned program makes heavy use of an uninitialized variable t, but GCC does not report it with -Wall or -Wuninitialized. Why is it so?
It is worth noting that Clang catches it:
main.cpp:13:12: warning: variable 't' is uninitialized when used here [-Wuninitialized]
v[i] = t++;
^
Used g++ (GCC) 7.2.1 20170915 (Red Hat 7.2.1-2).
Used clang version 4.0.1 (tags/RELEASE_401/final).
As you can see in https://godbolt.org/g/kmYMC1 GCC 7.2 does not report it even when it should. I will create a ticket in GCC's issue tracker.
g++'s warning flag is not called -Wuninitialized: it is called -Wmaybe-uninitialized.
Also, as Jonathan Wakely noted in his answer, g++ is able to detect usage of uninitialized variables only when optimizations are enabled.
Enabling both -Wmaybe-initalized and optimizations produces the expected warning: https://godbolt.org/g/3CZ6kT
Note that -Wmaybe-initalized is enabled by default with both -Wall and -Wextra.
GCC can only detect uninitialized variables when optimization is enabled, because the logic for tracking the values of variables is part of the optimization machinery.
If you compile with -O -Wall you get a warning:
<source>: In function 'int main()':
12 : <source>:12:13: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
v[i] = t++;
~^~
Compiler exited with result code 0
https://godbolt.org/g/327bsi
I have a basic C++ file like so:
#include <iostream>
using namespace std;
int main() {
float x = rand();
cout << x << endl;
return 0;
}
When I run this through g++ on Ubuntu with g++ test.cpp -o test -std=c++11, I get no errors, and the program runs just fine. But when I run it through g++ on MinGW with the same command, I get the following error:
test.cpp: In function 'int main()':
test.cpp:6:17: error: 'rand' was not declared in this scope
float x = rand();
^
I have GCC version 5.3.0. Attempting to compile with g++ test.cpp -o test.exe -std=gnu++11 or g++ test.cpp -o test.exe -std=c++0x yield the same result.
You must include library for the random function first
i-e
#include < cstdlib >
After that your code will work perfectly
Here is the correct code
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
float x = rand();
cout << x << endl;
return 0;
}
Some compilers allow you to use random function without including library but standard C++ compilers doesn't allow you.
Hope this will help you
According to this question, the use of threadprivate with openmp is
problematic. Here is a minimum (non-)working example of the problem:
#include"omp.h"
#include<iostream>
extern const int a;
#pragma omp threadprivate(a)
const int a=2;
void my_call(){
std::cout<<a<<std::endl;
};
int main(){
#pragma omp parallel for
for(unsigned int i=0;i<8;i++){
my_call();
}
}
This codes compiles with intel 15.0.2.164 but not with gcc 4.9.2-10.
gcc says:
g++ -std=c++11 -O3 -fopenmp -O3 -fopenmp test.cpp -o test
test.cpp:5:29: error: ‘a’ declared ‘threadprivate’ after first use
#pragma omp threadprivate(a)
I would be very happy to find a way to compile it with gcc.
Note: I know that global variables are a nightmare, but this example is the
coming from a code I haven't written and that I need to use... It's >11000
lines and I don't want to rewrite everything.
Well, I'm stumped. I've always had a bit of a hard time with static member variables and functions, so if the answer to this is really obvious, I apologize. I can't figure out what's wrong, though.
In WSGrid.h:
#include <functional>
class WSGrid
{
public:
//constructors...
static const std::function< char( void ) > _randomChar;
//private data...
};
In WSGrid.cpp:
#include <random>
std::default_random_engine generator;
std::uniform_int_distribution< char > distribution;
const WSGrid::_randomChar = std::bind( distribution, generator );
In my main.cpp:
#include "WSGrid.h"
#include <iostream>
int main( int argc, char* argv[] )
{
std::cout << WSGrid::_randomChar() << std::endl;
return 0;
}
And when I try to compile (g++ -std=c++11 -Wall -pedantic main.cpp), I get "undefined reference to WSGrid::_randomChar".
So... it looks, to me, like I'm doing everything right. I'm following the syntax found here, at least as far as I'm aware. But apparently there's something wrong.
You need to define _randomChar correctly.
update :
const WSGrid::_randomChar = std::bind( distribution, generator );
to:
const std::function<char(void)> WSGrid::_randomChar = std::bind(distribution, generator);
Also you need to link WSGrid.cpp
g++ -std=c++11 -Wall -pedantic -c WSGrid.cpp -o WSGrid.o
g++ -std=c++11 -Wall -pedantic main.cpp WSGrid.o