Segmentation fault. Core dumped

Pages: 12
@alexBB as per Andy's remarks, Declaring the function in main() is legal, but it is so unusual, that most programmers have never seen it before. I've only seen it in questions on this site.

So yes, you need the declaration, but it's better? to have it outside main().
Thank you guys. You are good teachers. I will look into this declaration business. Now I have much more serious problems. The vectors are fine but I need to make sure the complex numbers are represented by double precision. How can I do it? I've tried various ways to change the vector declarations and all I get is pages of compile errors.

Please, help.

Thanks. Alex
Last edited on
Here's an example using your typedef:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>     // std::cout
#include <complex>      // std::complex, std::real
#include <vector>
typedef std::complex<double> dcomp;

int main ()
{   dcomp mycomplex (10.0,1.0);  // A sample complex number
    std::vector<dcomp>  v_comp;  // A vector of complex<double>

    v_comp.push_back (mycomplex);
    std::cout << "Real part: " << std::real(v_comp[0]) << '\n';
// OR 
    std::cout << "Real part: " << v_comp[0].real() << '\n';
    return 0;
}
Real part: 10

Last edited on
Thank you, AbstractionAnon. I am trying to implements your code but I don't quite understand why I need push_back in it.
Last edited on
Line 8 creates an empty vector.

Line 10 adds a dcomp object to the vector.

If you do not do the push_back, there would be no object on the vector to print at line 11. v_comp[0] refers to the first entry of the vector. If the vector is empty, v_comp[0] is not a valid reference.

Thank you. I made an attempt but it did not compile. I don't know if I should start another thread or keep adding posts here but I do have a new crop of problems with vectors now. Here is a part of my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 #include "fft_vec2.h"
#include <math.h>
#include <fstream>      
#include <complex>
#include <vector>
typedef std::complex<double> dcomp;

double blackmanHarrisWindow (int n, int N); 

void fft(int N)
{
    std::ofstream outFileInput,outFileOutput;
    std::vector<dcomp> Input(N), Output(N);
    std::complex<double> valueC;
    double xx;  
    
    outFileInput.open ("cppFFTinp.dat");
    outFileOutput.open("cppFFTout.dat");
    for (int jj = 0; jj<N-1; jj++) {
	xx = blackmanHarrisWindow(jj,N);
	valueC = dcomp(xx,0);
	Input[jj] = valueC;    // Input[jj].push_back (valueC) <== did not compile, it gave me an error.
	outFileInput << Input[jj].real() << '\n';
    }

    CFFT::Forward (Input,Output,N);


The function header is:
1
2
static bool CFFT::Forward (std::vector<dcomp> Input, std::vector<dcomp> Output, const unsigned int N)
{


I get matching errors. On the last statement (26) it is:

1
2
3
4
5
6
 error: no matching function for call to ‘CFFT::Forward(std::vector<std::complex<double> >&, std::vector<std::complex<double> >&, int&)’
fft_vec2.cpp:25:34: note: candidates are:
fft_vec2.h:22:14: note: static bool CFFT::Forward(int, int, unsigned int)
fft_vec2.h:22:14: note:   no known conversion for argument 1 from ‘std::vector<std::complex<double> >’ to ‘int’
fft_vec2.h:23:14: note: static bool CFFT::Forward(int*, unsigned int)
fft_vec2.h:23:14: note:   candidate expects 2 arguments, 3 provided


The header file fft_vec2.h has this content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #ifndef _FFT_H_
#define _FFT_H_

#include <vector>
#include <stdlib.h>
#include <complex>

typedef std::complex<double> dcomp;
typedef std::vector<dcomp> vectComp;

class CFFT
{
public:
  static bool Forward(std::vector<dcomp> Input, std::vector<dcomp> Output, const unsigned int N);
  static bool Inverse(std::vector<dcomp> Input, std::vector<dcomp> Output, const unsigned int N, const bool Scale = true);
protected:
  static void Rearrange(std::vector<dcomp> Input, std::vector<dcomp> Output, const unsigned int N);
  static void Perform(std::vector<dcomp> Data, const unsigned int N, const bool Inverse = false);
  static void Scale(std::vector<dcomp> Data, const unsigned int N);
};

#endif 


Honestly, I don't understand what the compiler wants.

Please help. Thank you, - Alex



Last edited on
Alex,

I could be wrong, but I suspect that there has been some confusion among the various people trying to assist you. Most don't realize that your complex type is not std::complex and they have inadvertently advised you wrongly.

Since you are using the CFFT class you have to use the supplied complex class not std::complex.

The following code, based partially on your previous posts, uses vectors, compiles and writes the real part to the output file.

Again, I may be off base with this. HTH


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "fft.h"
#include <math.h>
#include <cmath>
#include <fstream>      // std::ofstream
#include <iostream>
#include <vector>

double blackmanHarrisWindow (int n, int N);

void fft(int N)
{
    std::ofstream outFileInput,outFileOutput;
    std::vector<complex> Input, Output(N);
    double xx;
    outFileInput.open ("cppFFTinp.dat");
    outFileOutput.open("cppFFTout.dat");
    for (int jj = 0; jj<N-1; jj++) {
        xx = blackmanHarrisWindow(jj,N);
        Input.emplace_back(xx, 0);
        outFileInput << Input[jj].re() << '\n';
    }
    CFFT::Forward (&Input[0], &Output[0], N);
    //CFFT::Inverse (&Output[0], &Input[0],N);
    //for (auto it = Input.begin(); it != Input.end(); ++it) {
    for (auto it = Output.begin(); it != Output.end(); ++it) {
        outFileOutput << (*it).re() << std::endl;
    }
}

 int main ()
{
     fft(256);
}            // main

double blackmanHarrisWindow (int n, int N) {
    double res,PI=3.141592653589793238462643383279502884197169;
    int N1;
    N1 = N-1;
    res = 0.35875-0.48829*cos(2.0*PI*n/N1)+0.14128*cos(4.0*PI*n/N1)+0.01168*cos(6.0*PI*n/N1);
    return res;
}


Edit: changed code (accidentally left in code from testing)
Last edited on
Thank you norm b. I will try to sort it all out tomorrow. It is my recollection that I tried to include complex.h supplied by Librow

http://www.librow.com/articles/article-10

but it led to significant problems and never compiled although I could be many times wrong. I will have to check it all out soon.

- Alex
Topic archived. No new replies allowed.
Pages: 12