Hi All,

I'm a beginner in C and C++, and I get a "Stack Overflow" problem when I compile my code. I use visual studio 2008.

My code uses some library, that has some header files, and some .cpp files. The library basically helps me in getting a random number from a uniform discrete distribution.

My code (a file called program.cpp) and the .cpp and .h files of the library I'm using are all included in the attached .zip file.

Any help will be really appreciated!

And for convenience, the contents of my main code are:

``````#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstdlib>
using namespace std;
#define WANT_STREAM
#define WANT_MATH
#define WANT_TIME

#include "include.h"
#include "newran.h"
#include "tryrand.h"

#ifdef use_namespace
using namespace NEWRAN;
#endif

#define N 3499
#define start 0

int main (void)
{
//int tmp;
//int s;
//int count;

ofstream fout("input.dat");

Real probs[N];
Real val[N];
for(int i=0;i<N;i++)
{
probs[i]=1/(float)N;
val[i]=start+i;
}
DiscreteGen discrete(N,probs,val);
srand ( time(NULL) );
double mySeed=(double)rand()/RAND_MAX;
//cout<<mySeed<<endl;
discrete .Set (mySeed);
//Sleep(2000);
int a[9000][9900];
//for (ttt=0;ttt<100;ttt++){
//fout<<"[";
////discrete.Set(0.02);
//for (uuu=0; uuu<1000; uuu++){
//fout << discrete.Next() <<" ";
//}
//fout<<"]";
//}
//fout<<"];";
int i=0;
while(i<1000){
fout<<"[";
int k=discrete.Next();
int j=discrete.Next();

int p=discrete.Next();
int l=discrete.Next();
int v=discrete.Next();
for (int e=0;e<3500;e++){
if(e==k || e==j || e==p ||e==l ||e==v){
a[i][e]=1;
}
else {
a[i][e]=0;
}
fout<<a[i][e];
fout<<" ";
}
fout<<"] ";
fout<<"[";

int t=discrete.Next();
int y=discrete.Next();

int q=discrete.Next();
int s=discrete.Next();
int z=discrete.Next();
int w=discrete.Next();
for (int e=0;e<3500;e++){
if(e==t || e==y || e==q ||e==s ||e==z || e==q){
a[i+1][e]=1;
}
else {
a[i+1][e]=0;
}
fout<<a[i][e];
fout<<" ";
}
fout<<"] ";
fout<<"[";

int mm=discrete.Next();
int jj=discrete.Next();

int pp=discrete.Next();
int ll=discrete.Next();
int vv=discrete.Next();
int gg=discrete.Next();
for (int e=0;e<3500;e++){
if(e==mm || e==jj || e==pp ||e==ll ||e==vv || e==gg){
a[i+2][e]=1;
}
else {
a[i+2][e]=0;
}
fout<<a[i][e];
fout<<" ";
}
fout<<"]";

i=i+3;
}

return 0;
}``````
3
Contributors
6
Replies
8
Views
7 Years
Discussion Span
Last Post by Dave Sinkula

Sorry, I forgot to include the file. Here it goes!

Aly

Attachments

> int a[9000][9900];
How does the size of this compare say to the 1MB default stack size?

Well I'm not sure how it is compared, but I adjusted the stack size in the project properties in visual studio 2008 to its maximum (which is 1,000,000,000, but I don't know its limits).

Also, I think my code only fills [1002][3500], and in each it puts either 1 or 0.

So?

So?

I'm guessing that in excess of ~356,400,000 bytes on the stack might possibly contribute to a stack overflow.

My code (a file called program.cpp) and the .cpp and .h files of the library I'm using are all included in the attached .zip file.

Maybe not quite all?

hist.cpp
In file included from Programs/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/iostream.h:31,
from files/include.h:138,
from files/hist.cpp:4:
Programs/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
files/hist.cpp:8:21: tryrand.h: No such file or directory
program.cpp
In file included from Programs/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/iostream.h:31,
from files/include.h:138,
from files/program.cpp:11:
Programs/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
files/program.cpp:13:21: tryrand.h: No such file or directory
files/program.cpp: In function `int main()':
files/program.cpp:61: warning: converting to `int' from `Real'
files/program.cpp:62: warning: converting to `int' from `Real'
files/program.cpp:64: warning: converting to `int' from `Real'
files/program.cpp:65: warning: converting to `int' from `Real'
files/program.cpp:66: warning: converting to `int' from `Real'
files/program.cpp:82: warning: converting to `int' from `Real'
files/program.cpp:83: warning: converting to `int' from `Real'
files/program.cpp:85: warning: converting to `int' from `Real'
files/program.cpp:86: warning: converting to `int' from `Real'
files/program.cpp:87: warning: converting to `int' from `Real'
files/program.cpp:88: warning: converting to `int' from `Real'
files/program.cpp:107: warning: converting to `int' from `Real'
files/program.cpp:108: warning: converting to `int' from `Real'
files/program.cpp:110: warning: converting to `int' from `Real'
files/program.cpp:111: warning: converting to `int' from `Real'
files/program.cpp:112: warning: converting to `int' from `Real'
files/program.cpp:113: warning: converting to `int' from `Real'
*** Errors occurred during this build ***

Do you think I exceeded that bytes limit?

I attached the missing files, but they are not used in my code though!

Thanks a lot for attention.

Aly

Attachments
``````#define WANT_STREAM
#define WANT_TIME

#include "include.h"
#include "newran.h"
#include "tryrand.h"

#ifdef use_namespace
using namespace NEWRAN;
#endif

int main()
{
time_lapse tl;      // measure program run time

Random::Set(0.46875);

Real* s1; Real* s2; Real* s3; Real* s4;
cout << "\nBegin test\n";   // Forces cout to allocate memory at beginning
cout << "Now print a real number: " << 3.14159265 << endl;
{ s1 = new Real[8000]; delete [] s1; }
{ s3 = new Real; delete s3;}
{

Real* A = new Real[3750];

long n = 200000;
long n_large = 1000000;

test1(n);
test2(n);
test3(n_large);
test4(n);
test5(n_large);

cout << "\nEnd of tests\n";

delete [] A;
}

{ s2 = new Real[8000]; delete [] s2; }
cout << "\n(The following memory checks are probably not valid with all\n";
cout << "compilers - see documentation)\n";
cout << "\nChecking for lost memory: "
<< (unsigned long)s1 << " " << (unsigned long)s2 << " ";
if (s1 != s2) cout << " - error\n"; else cout << " - ok\n\n";
{ s4 = new Real; delete s4;}
cout << "\nChecking for lost memory: "
<< (unsigned long)s3 << " " << (unsigned long)s4 << " ";
if (s3 != s4) cout << " - error\n"; else cout << " - ok\n\n";

return 0;
}

//************** elapsed time class ****************

time_lapse::time_lapse()
{
start_time = ((double)clock())/(double)CLOCKS_PER_SEC;
}

time_lapse::~time_lapse()
{
double time = ((double)clock())/(double)CLOCKS_PER_SEC - start_time;
cout << "Elapsed (processor) time = " << setw(10) << setprecision(4) << time << " seconds" << endl;
cout << endl;
}``````
``````// tryrand.h

// print time between construction and destruction
class time_lapse
{
double start_time;
public:
time_lapse();
~time_lapse();
};

void test1(int);
void test2(int);
void test3(int);
void test4(int);
void test5(int);
void Histogram(Random*, int);          // draw histogram with n obsv

// body file: tryrand1.cpp
// body file: tryrand2.cpp
// body file: tryrand3.cpp
// body file: tryrand4.cpp
// body file: tryrand5.cpp
// body file: hist.cpp``````

Simple test -- move the array off the stack:

``````int a[9000][9900];
int main (void)``````

Any difference?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.