0

I am new to C++ programming and had written this C++ code:

//colpitts high freq 1 GHz working with delay
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
using namespace std;

const double pi = 3.1415926;

int main(){
    double c0, dx, dt,C1,C2,L,fs,Ceq, freq, tau, Rload, gload, Re, ge,gm,gc1,gc2,ic1,ic2,il,gl ;
    c0=20000000000;
    dx=0.01;
    dt=dx/(2 * c0);
    cout<<dt<<"\n";
    double V1 [1000000]={};
    double V2 [1000000]={};
    V1[0]=1e-3; 
    C1=1e-12;
    C2=5e-12;
    L=30.4e-9;
    fs=4e12;
    Ceq=(C1 * C2)/(C1+C2);
    cout<<Ceq<<"\n";
    freq=1/(2 * pi * (sqrt(L*Ceq)));
    cout<<freq<<"\n";
    tau=1/freq;
    cout<<tau<<"\n";
    Rload=50;
    Re=1e6; 
    ge=1/Re;
    cout<<ge<<"\n";
    gm=0;
    gc1=(C1)/dt;
    cout<<gc1<<"\n";
    ic1=-((C1)/dt) * V1[0];  
    cout<<ic1<<"\n";
    gc2=(C2)/dt;
    cout<<gc2<<"\n";
    ic2=-((C2)/dt) * V2[0];
    cout<<ic2<<"\n";
    gl=dt/(L);
    cout<<gl<<"\n";
    il=gl * (V2[0]-V1[0]);
    cout<<il<<"\n";
    gload=1/Rload;
    cout<<gload<<"\n";
    return (0);
}

when i run it on a Linux machine, it throws an error of segmentation fault (core dumped), but when I change the array to 100000, no error is thrown and the program executes as expected. I know the problem is with physical memory allocated to me, but would there be a way around it? Can someone kindly guide me as to what modifications i need to bring in?

mc110
  • 2,825
  • 5
  • 20
  • 21
Nehal P
  • 51
  • 1
  • 5

3 Answers3

2

It is the stack. You are using about 16Mb of stack to store those two arrays of doubles (8 bytes per double * 2 arrays * 1,000,000).

Setting a much bigger stack like this:

ulimit -s 32000

Does fix the problem. But putting 16Mb blocks of data on the stack isn't a great idea.

If you move them off the stack like this (and use some blank lines to make the code easier to read :)) then it works fine. However I'd also recommend, as mentioned above, that you look at vector rather than using the C style raw arrays:

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

#include <iostream>
#include <cmath>
#include <string>
#include <cstring>

using namespace std;

const double pi = 3.1415926;


double V1 [1000000]={};
double V2 [1000000]={};


int main(){

    double c0, dx, dt,C1,C2,L,fs,Ceq;
    double freq, tau, Rload, gload; 
    double Re, ge,gm,gc1,gc2,ic1,ic2,il,gl ;
    c0=20000000000;
    dx=0.01;
    dt=dx/(2 * c0);
    cout<<dt<<"\n";

    V1[0]=1e-3; 
    C1=1e-12;
    C2=5e-12;
    L=30.4e-9;
    fs=4e12;

    Ceq=(C1 * C2)/(C1+C2);
    cout<<Ceq<<"\n";
    freq=1/(2 * pi * (sqrt(L*Ceq)));
    cout<<freq<<"\n";

    tau=1/freq;
    cout<<tau<<"\n";

    Rload=50;
    Re=1e6; 
    ge=1/Re;
    cout<<ge<<"\n";

    gm=0;
    gc1=(C1)/dt;
    cout<<gc1<<"\n";

    ic1=-((C1)/dt) * V1[0];  
    cout<<ic1<<"\n";

    gc2=(C2)/dt;
    cout<<gc2<<"\n";

    ic2=-((C2)/dt) * V2[0];
    cout<<ic2<<"\n";

    gl=dt/(L);
    cout<<gl<<"\n";

    il=gl * (V2[0]-V1[0]);
    cout<<il<<"\n";

    gload=1/Rload;
    cout<<gload<<"\n";

    return (0);
}
JCx
  • 2,689
  • 22
  • 32
1

Paul R is right. Variables declared within a function uses "the stack". There is a limit how much space you can use for the local variables.

His suggestions of making those local array declaration global, static, dynamically allocated means that those variables will not use the stack space. Using vector from the STL will work also.

This link may give you an idea on stack space size.

https://stackoverflow.com/a/1825996/3813353

One quick-and-dirty solution is to use limit/ulimit. The drawback is that you have to use this command before you run your program. Simply making the arrays global much easier...

Usually, when you run into problems with stack size, is that you have runaway or infinite recursion. Each call to the recursive function may not use much data from the stack, but if the function keeps calling itself, you'll eventually run out of stack space.

Community
  • 1
  • 1
user3813353
  • 174
  • 4
1

Indeed, you have too much data on stack.

You could use std::vector standard template class (even if you allocate a vector on stack, its data is in heap). So add #include <vector> to the included headers, then code things like

  std::vector<double> v1, v2;
  v1.push_back(2.512);
  v1.push_back(3.17);
  v2.resize(37);
  v2[0] = v1[0] + v1[1]; 
  v2[17] = sqrt(10.0) + 11.0;

Read some STL tutorial; BTW, use and learn C++11 (not some earlier C++ standard). So use a recent compiler (e.g. GCC 4.9, compile with it using g++ -std=c++11 -Wall -g....) and use the gdb debugger.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547