Click here to Skip to main content
15,882,163 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more: , +
hello professionals ;)

can you explain me this error, I think this is a Stack overflow; can you give me a solution without using the dynamic allocation and thank you.

C++
'test.exe' : Chargé 'C:\Documents and Settings\Administrateur\Bureau\test\Debug\test.exe', Les symboles ont été chargés.
'test.exe' : Chargé 'C:\WINDOWS\system32\ntdll.dll'
'test.exe' : Chargé 'C:\WINDOWS\system32\kernel32.dll'
'test.exe' : Chargé 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcp90d.dll', Les symboles ont été chargés.
'test.exe' : Chargé 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcr90d.dll', Les symboles ont été chargés.
Exception de première chance à 0x00412d27 dans test.exe : 0xC00000FD: Stack overflow.
Exception non gérée à 0x00412d27 dans test.exe : 0xC00000FD: Stack overflow.
Le programme '[2716] test.exe: Natif' s'est arrêté avec le code 0 (0x0).



that is my source code:
C++
#include <cstdlib> 
#include <fstream> 
#include <iostream> 
#include <string> 
#include "stdafx.h";
#include <stdio.h>      
#include <stdlib.h>     
#include <math.h>
using namespace std; 

double distance(double v1[], double v2[])
{
	double s=0;
	double res=0;
	double dif=0;

	for (int i=0; i<26; i++)
	{
		dif=abs(v1[i]-v2[0]);
		for (int j=1; j<26; j++)
		{
			if(abs(v1[i]-v2[j])<dif)
			{
				dif=abs(v1[i]-v2[j]);
			}
		}
		s=s+dif;
	}
	res=s/26;
	return res;
}


void copie(double mat[26][600],double v[],int ind)
{

	for(int i=0; i<26;i++)
	{
		v[i]=mat[i][ind];
	}
}


int main()
{

	double glob [26][600];
	double score [600][600];



	//location dynamique de tableau de vecteurs descripteurs
	//double **glob = new double*[600];
	//for (int i = 0; i < 600; i++)
	//glob[i] = new double[26];

	//location dynamique de matrice résultat
	//double **score = new double*[600];
	//for (int i = 0; i < 600; i++)
	//score[i] = new double[600];

	//Remplir le tableau glob par les vecteurs descripteurs 
	for(int x=0;x<600;x++)
	{

		char fil[265];

		sprintf(fil, "database/%d.txt", x);
		ifstream fichier(fil, ios::in);

		// on ouvre en lecture

		if(fichier)  // si l'ouverture a fonctionné

		{  
			for(int i=0;i<26;i++)
			{
				string contenu;  // déclaration d'une chaîne qui contiendra la ligne lue
				getline(fichier, contenu);  // on met dans "contenu" la ligne	
				glob[i][x]=atof(contenu.c_str());

			}

		}
		else
			cerr << "Impossible d'ouvrir le fichier !" << endl;
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////
	//initialisation de tableau score

	for (int i=0; i<600; i++){
		for (int j=0; j<600; j++){
			score[i][j] = 0;

		}
	}

	//////////////////////////////////////////////////////////////////////////////////////////////////////////

	//calcule de distance ((valeur absolu (V1(i)-V2(i)/26)

	double d;
	double v1[26];
	double v2[26];

	for(int i=0; i<600; i++)
	{
		for(int j=0; j<600; j++)
		{
			if(i!=j)
			{

				copie(glob,v1,i);
				copie(glob,v2,j);
				d=distance(v1,v2);
				score[i][j]=d;
			}
		}
	}


	//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//écriture de tableau glob dans la ficher out.txt
	ofstream fichier1; 
	fichier1.open("out.txt", ios::out);


	if(fichier1)

	{
		for(int a=0; a<26; a++)
		{
			for(int b=0; b<600; b++)
			{
				fichier1 << glob[a][b]<<"    ";

			}
			fichier1 <<"\n";
		}
		fichier1.close();
	}
	/////////////////////////////////////////////////////////////////////////////////////////////////////////

	//écriture de tableau score dans la ficher score.matrix
	ofstream fichier2; 
	fichier2.open("score.matrix", ios::out);


	if(fichier2)

	{
		for(int o=0; o<600; o++)
		{
			for(int p=0; p<600; p++)
			{
				fichier2 << score[o][p]<<" ";

			}
			fichier2 <<"\n";
		}
		fichier2.close();
	}


	//getchar();
}
Posted
Updated 13-Jul-15 3:11am
v2
Comments
Richard MacCutchan 13-Jul-15 8:44am    
Where does the exception occur?

You are using too large arrays on the stack:
double glob [26][600];
double score [600][600];

These allocate 26 * 600 + 600 * 600 = 375,600 double items. Because each double uses 8 bytes, the total amount of memory is about 3 MB which is more than the default stack size of 1 MB (with Visual C/C++).

To avoid this you can increase the stack size. But a much better solution is to allocate the arrays on the heap using new (with C++) or malloc (with C):
double *glob = new double[26*600];
double *score = new double[600*600];


Note that this requires additional code to calculate the effective item offsets (two examples from your code):
//initialisation de tableau score
for (int i=0; i<600; i++){
    for (int j=0; j<600; j++){
        score[i*600+j] = 0;
    }
}

for(int a=0; a<26; a++)
{
    for(int b=0; b<600; b++)
    {
        fichier1 << glob[a*600+b]<<"    ";
    }
    fichier1 <<"\n";
}


[UPDATE]
To increase the stack size see the MSDN[^].
But that is a non-professional solution for your problem. Large objects should be always allocated on the heap.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 13-Jul-15 9:51am    
I guess you are right, this is the exact problem. My 5.
—SA
bilel hmam 13-Jul-15 10:09am    
Thanks you Jochen Arndt,
How I can can increase the stack size.
Jochen Arndt 13-Jul-15 10:11am    
I have updated my answer meanwhile with a MSDN link.
Alternately to what Jochen Arndt suggested, there are many ways to define a 2D array in C++. See for example http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new[^]

My preferred way is defining a matrix class (or using one from an existing library such). but if you prefer the C syntax, and don't like to do the index calculations by hand, the easiest method is this:
C++
typedef int (int_600)[600];
void foo() {
   int_600 *a = new int_600[26];// defines a 26 by 600 matrix
   a[12][123]=1;
   delete [] a;
}

I needed the typedef only because I couldn't find out how to define the type of a correctly, except through the use of auto (which I deliberately avoided in the spirit of C syntax). Also, while auto a = new int[26][600]; does compile, I am not sure if it conforms to C++ standard, or is in fact a feature of C++11, or even compiler specific.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900