This question already has answers here:
Segmentation fault on large array sizes
(7 answers)
Closed 1 year ago.
I have some data of Complex Values of size N, and would like to compute the FFT of this data using the Intel OneAPI. Here is my code:
# Connectivity
#include <bits/stdc++.h>
#include "mkl_dfti.h"
#include <complex.h>
using namespace std;
float pi = 2*acos(0.0);
int main(){
long long int N; cin >> N
float _Complex c2c_data[N];
DFTI_DESCRIPTOR_HANDLE my_desc1_handle = NULL;
DFTI_DESCRIPTOR_HANDLE my_desc2_handle = NULL;
MKL_LONG status;
// data is inserted here
status = DftiCreateDescriptor(&my_desc1_handle, DFTI_SINGLE, DFTI_COMPLEX, 1, N);
status = DftiCommitDescriptor(my_desc1_handle);
status = DftiComputeForward(my_desc1_handle, c2c_data);
status = DftiFreeDescriptor(&my_desc1_handle);
cout << round(cabs(c2c_data[s]) / N) << "\n";
return 0;
}
This works for smaller cases of N, but for larger cases (around 2^21), I get a segmentation fault and for even larger cases, I get a Bus error. I have checked that this happens at the DftiComputeForward function. The length of the data is specified in DftiCreateDescriptor which is indeed N in this case, so I am not sure why I am getting this error.
Here is here how I compile my code:
dpcpp test.cpp -lmkl_intel_lp64 -lmkl_core -lmkl_intel_thread -liomp5 -ldl -lpthread -o test
It would be great if someone could help. Thank you!
you might be exhausting your stack memory space.
Please consider using a vector instead.
Related
I was implemeting a graph theory question in Cpp using STL. While compiling the following code snippet in my local machine, I am not getting any compile time error, even with compiling using the following flags:
-Wall -Wextra -pedantic -std=c++11 -O2 -Wshadow -Wformat=2 -Wfloat-equal -Wconversion -Wlogical-op -Wshift-overflow=2 -Wduplicated-cond -Wcast-qual -Wcast-align -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2 -fsanitize=address -fsanitize=undefined -fno-sanitize-recover -fstack-protector
But when I am submitting the code to the following question: https://practice.geeksforgeeks.org/problems/count-the-paths/0 I am getting segmentation fault. Below is the code snippet, if you find any error, please point out.
#include <bits/stdc++.h>
#define int long long
#define endl "\n"
#define MAX 10e4
using namespace std;
vector<vector<int>> v(MAX);
vector<bool> check(MAX);
int path;
void dfs(int s, int d) {
for (int i: v[s]) {
if (i == d) {
path++;
continue;
}
if (!check[i]) {
dfs(i, d);
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, e;
cin >> n >> e;
int a, b;
for (int i=0; i<e; i++) {
cin >> a >> b;
v[a].push_back(b);
}
int source, destination;
cin >> source >> destination;
check[source] = true;
dfs(source, destination);
cout << path << endl;
}
return 0;
}
Your code has several errors, I've removed them all and run the code. It compiled fine and work properly on provided input. But there are some problems with your implementation style and also with problem. To understand what is wrong with your code(actually, its not only your code's fault, the problem is also wrong), let me explain first why the problem is wrong:
Statement:
Given a directed graph, a source vertex ‘s’ and a destination vertex ‘d’, print the count of all paths from given ‘s’ to ‘d’.
Input:
First line consists of T test cases. First line of every test case consists of V and E, denoting the vertices and edges. Second line of every test case consists of 2*E spaced integers denoting the edge between vertices. Third line of every test case consists of S and D.
Output:
Single line output, print the count of all paths from 's' to 'd'.
It seems ok at first, but see, there's not talk about cycle. If there's any cycle, then, this problem could have infinite answer.
Now, examine the input:
1
4 6
0 1
0 2 // note this
0 3
2 0 // note this
2 1
1 3
2 3
The above (note this) lines shows two edges, those create a cycle. but, your code still works, why? cause, your code makes check[source] = true. But what if the cycle does not include source but other vertices...then, this code will lead to stackoverflow and sometimes stackoverflow raises SIGSEGV (segmentation violation) signal.
This problem can still be solved with some work around, but it'll be solving a wrong problem with wrong rules and wrong understanding(this happens sometime in problem solving and competitive programming). So, I didn't try that.
[P.S.]: competitive programming or sport programming is not bad at all. its actually quite effective to learn ds and algo. but choose better sites like topcoder, codeforces, codechef, hackerrank, leetcode, atcoder, uva, lightoj and the list goes on...but those i've noted are actually quite good.
I am new to c++ programming and StackOverflow, but I have some experience with core Java. I wanted to participate in programming Olympiads and I choose c++ because c++ codes are generally faster than that of an equivalent Java code.
I was solving some problems involving recursion and DP at zonal level and I came across this question called Sequence game
But unfortunately my code doesn't seem to work. It exits with exit code 3221225477, but I can't make anything out of it. I remember Java did a much better job of pointing out my mistakes, but here in c++ I don't have a clue of what's happening. Here's the code btw,
#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
int N, minimum, maximum;
set <unsigned int> result;
vector <unsigned int> integers;
bool status = true;
void score(unsigned int b, unsigned int step)
{
if(step < N)
{
unsigned int subtracted;
unsigned int added = b + integers[step];
bool add_gate = (added <= maximum);
bool subtract_gate = (b <= integers[step]);
if (subtract_gate)
subtracted = b - integers[step];
subtract_gate = subtract_gate && (subtracted >= minimum);
if(add_gate && subtract_gate)
{
result.insert(added);
result.insert(subtracted);
score(added, step++);
score(subtracted, step++);
}
else if(!(add_gate) && !(subtract_gate))
{
status = false;
return;
}
else if(add_gate)
{
result.insert(added);
score(added, step++);
}
else if(subtract_gate)
{
result.insert(subtracted);
score(subtracted, step++);
}
}
else return;
}
int main()
{
ios_base::sync_with_stdio(false);
ifstream input("input.txt"); // attach to input file
streambuf *cinbuf = cin.rdbuf(); // save old cin buffer
cin.rdbuf(input.rdbuf()); // redirect cin to input.txt
ofstream output("output.txt"); // attach to output file
streambuf *coutbuf = cout.rdbuf(); // save old cout buffer
cout.rdbuf(output.rdbuf()); // redirect cout to output.txt
unsigned int b;
cin>>N>>b>>minimum>>maximum;
for(unsigned int i = 0; i < N; ++i)
cin>>integers[i];
score(b, 0);
set<unsigned int>::iterator iter = result.begin();
if(status)
cout<<*iter<<endl;
else
cout<<-1<<endl;
cin.rdbuf(cinbuf);
cout.rdbuf(coutbuf);
return 0;
}
(Note: I intentionally did not use typedef).
I compiled this code with mingw-w64 in a windows machine and here is the Output:
[Finished in 19.8s with exit code 3221225477] ...
Although I have an intel i5-8600, it took so much time to compile, much of the time was taken by the antivirus to scan my exe file, and even sometimes it keeps on compiling for long without any intervention from the anti-virus.
(Note: I did not use command line, instead I used used sublime text to compile it).
I even tried tdm-gcc, and again some other peculiar exit code came up. I even tried to run it on a Ubuntu machine, but unfortunately it couldn't find the output file. When I ran it on a Codechef Online IDE, even though it did not run properly, but the error message was less scarier than that of mingw's.
It said that there was a run-time error and "SIGSEGV" was displayed as an error code. Codechef states that
A SIGSEGV is an error(signal) caused by an invalid memory reference or
a segmentation fault. You are probably trying to access an array
element out of bounds or trying to use too much memory. Some of the
other causes of a segmentation fault are : Using uninitialized
pointers, dereference of NULL pointers, accessing memory that the
program doesn’t own.
It's been a few days that I am trying to solve this, and I am really frustrated by now. First when i started solving this problem I used c arrays, then changed to vectors and finally now to std::set, while hopping that it will solve the problem, but nothing worked. I tried a another dp problem, and again this was the case.
It would be great if someone help me figure out what's wrong in my code.
Thanks in advance.
3221225477 converted to hex is 0xC0000005, which stands for STATUS_ACCESS_VIOLATION, which means you tried to access (read, write or execute) invalid memory.
I remember Java did a much better job of pointing out my mistakes, but here in c++ I don't have a clue of what's happening.
When you run into your program crashing, you should run it under a debugger. Since you're running your code on Windows, I highly recommend Visual Studio 2017 Community Edition. If you ran your code under it, it would point exact line where the crash happens.
As for your crash itself, as PaulMcKenzie points out in the comment, you're indexing an empty vector, which makes std::cin write into out of bounds memory.
integers is a vector which is a dynamic contiguous array whose size is not known at compile time here. So when it is defined initially, it is empty. You need to insert into the vector. Change the following:
for(unsigned int i = 0; i < N; ++i)
cin>>integers[i];
to this:
int j;
for(unsigned int i = 0; i < N; ++i) {
cin>> j;
integers.push_back(j);
}
P.W's answer is correct, but an alternative to using push_back is to pre-allocate the vector after N is known. Then you can read from cin straight into the vector elements as before.
integers = vector<unsigned int>(N);
for (unsigned int i = 0; i < N; i++)
cin >> integers[i];
This method has the added advantage of only allocating memory for the vector once. The push_back method will reallocate if the underlying buffer fills up.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
I'm using g++ command line under Windows 10 to build a basic timing loop and am getting the error: " undefined reference to `timeGetTime#0' " when attempting to compile.
The code, itself, is pretty simple:
#include <windows.h>
#include <iostream>
using namespace std;
int main(){
int start = timeGetTime();
int finish = 10000;
int benchmarks [9] = {1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000};
int i = 0;
int time = timeGetTime() - start;
while( time < finish){
if(time > benchmarks[i]){
cout << benchmarks[i] / 1000 << endl;
i++;
}
return 0;
}
Not sure what I need to do to get g++ to play nicely with the WinAPI. I can't help but wonder if it's an issue with the linker.
You have to link libwinmm.
Undefined reference is an error which occures when your compiler knows that the function exist, it knows its prototype, but can't find its corpse. When it is not one of your functions, it surely means you miss to link a library.
I have a quadratic programming optimization problem that I am solving with qpOASES. In there exists a matrix X that I need to precondition, so I am using Armadillo and the routine arma::pinv from there in order to calculate the Moor-Penrose pseudoinverse.
The problem: I write the matrix X in a file , and then I read it in a separate program (say test.cpp) that does not depend in any way to qpOASES. The routine pinv runs fine.
#include <iostream>
#include <fstream>
#include <armadillo>
#include <string>
using namespace std;
using namespace arma;
int main(){
// Read design matrix.
int NRows = 199;
int NFields = 26;
string flname_in = "chol_out_2_data";
mat A (NRows,NFields);
for (int i=0; i < NRows; ++i)
for (int j=0; j < NFields; ++j)
myin >> A(i,j) ;
// Calculate pseudoinverse
mat M;
pinv(M,A); // <========= THIS fails when I use flag: -lqpOASES
}
When I include the same routine in the file where I perform the QP optimization (say true_QP.cpp), I get a runtime error, due to pinv not being able to calculate the pseudo inverse. I've done extensive tests, the file is read in OK and the values are the same.
I've tracked down the problem that is a conflict in the following way: I compiled the program that does not depend in any way on qpOASES (test.cpp - as described above) also with the flag -lqpOASES and then, the code gives run time error.
That is,compile:
g++ test.cpp -o test.xxx -larmadillo
runs fine:
./test.xxx
compile:
g++ test.cpp -o test.xxx -larmadillo -lqpOASES
throws exception (due to failure of calculating pinv):
./test.xxx
Therefore I suspect some conflict - it seems that using -lqpOASES affects some flag in armadillo also? Any ideas? Is there some dependency in LAPACK/BLAS or some flag internally that may change the setup of Armadillo? Thank you for your time.
Here is the documentation for the arma::pinv function:
http://arma.sourceforge.net/docs.html#pinv
I have resolved the issue by calculating pinv from Eigen, instead of Armadillo.
The function definition I used for Eigen, based on this bug report:
http://eigen.tuxfamily.org/bz/show_bug.cgi?id=257
is:
template<typename _Matrix_Type_>
Eigen::MatrixXd pinv(const _Matrix_Type_ &a, double epsilon =std::numeric_limits<double>::epsilon())
{
Eigen::JacobiSVD< _Matrix_Type_ > svd(a ,Eigen::ComputeThinU | Eigen::ComputeThinV);
double tolerance = epsilon * std::max(a.cols(), a.rows()) *svd.singularValues().array().abs()(0);
return
svd.matrixV() * (svd.singularValues().array().abs() > tolerance).select(svd.singularValues().array().inverse(), 0).matrix().asDiagonal() * svd.matrixU().adjoint();
}
This code compiles and runs though gives a Microsoft compiler error that I cant fix
warning C4700: uninitialized local variable '' used.
This is in the last line of the code, I think
#include <iostream>
using namespace std;
const int DIM0 = 2, DIM1 = 3, DIM2 = 4, DIM3 = 5;
void TestDeclar();
int main(){
TestDeclar();
cout << "Done!\n";
return 0;
}
void TestDeclar(){
//24 - array of 5 floats
float xa[DIM3], xb[DIM3], xc[DIM3], xd[DIM3], xe[DIM3], xf[DIM3];
float xg[DIM3], xh[DIM3], xi[DIM3], xj[DIM3], xk[DIM3], xl[DIM3];
float xm[DIM3], xn[DIM3], xo[DIM3], xp[DIM3], xq[DIM3], xr[DIM3];
float xs[DIM3], xt[DIM3], xu[DIM3], xv[DIM3], xw[DIM3], xx[DIM3];
//6 - array of 4 pointers to floats
float *ya[DIM2] = {xa, xb, xc, xd}, *yb[DIM2] = {xe, xf, xg, xh};
float *yc[DIM2] = {xi, xj, xk, xl}, *yd[DIM2] = {xm, xn, xo, xp};
float *ye[DIM2] = {xq, xr, xs, xt}, *yf[DIM2] = {xu, xv, xw, xx};
//2 - array of 3 pointers to pointers of floats
float **za[DIM1] = {ya, yb, yc};
float **zb[DIM1] = {yd, ye, yf};
//array of 2 pointers to pointers to pointers of floats
float ***ptr[DIM0] = {za, zb};
cout << &***ptr[DIM0] << '\n';
}
You're accessing past the end of the ptr4D. DIM0 is 2, one greater than the last index of 1!
Change the last few lines to:
//array of 2 pointers to pointers to pointers of floats
float ***ptr4D[DIM0] = {za, zb};
cout << &***ptr4D[0] << '\n';
Not sure if I can help you but I tried to find out what's wrong trying to run it on my linux machine. I've compiled it on a ubuntu machine to compare and it went ok, even tellign the compiler to turn on all option warnings (passing the -Wall option). When running, I got this:
# Compiled it with -Wall to enable all warning flags and -g3 to produce extra debug information
~$ g++ -Wall stackoverflow.cpp -g3
./a.out
Segmentation fault (core dumped)
Then I've tried to debug it with GDB (GNU debugger) and got this:
(gdb) r
Starting program: /home/ubuntu/a.out
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400986 in TestDeclar () at stackoverflow.cpp:34
34 cout << &***ptr4D[DIM0] << '\n';
(gdb) s
So it appears that the problem is at the cout line. Checking your code again, DIM0's value is 2, so you're trying to access a memory address beyond the ptr4D. As user1721424 mentioned, just replace the DIM0 with 0 and it's done!
#After fixing it:
~$ ./a.out
0x7fff74cd3830
Done!
Hope it helps!