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;
}

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

Aly

> 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 ***

Thanks a lot for your reply, Dave.

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

Simple test -- move the array off the stack:

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

Any difference?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.