调试时出错 [英] Error while debugging

查看:73
本文介绍了调试时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好专业人士;)



你能解释一下这个错误,我认为这是一个堆栈溢出;你能不给我一个解决方案而不使用动态分配,谢谢你。



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.

'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:

#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();
}

推荐答案

你在堆栈上使用太大的数组:

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



这些分配26 * 600 + 600 * 600 = 375,600双项。因为每个double使用8个字节,所以内存总量大约为3 MB,超过默认堆栈大小1 MB(使用Visual C / C ++)。



为避免这种情况,您可以增加堆栈大小。但更好的解决方案是使用 new (使用C ++)或 malloc (使用C)在堆上分配数组):


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





[更新]

要增加堆栈大小,请参阅MSDN [ ^ ]。

但这是一个针对您的问题的非专业解决方案。大型对象应始终在堆上分配。



[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.


或许Jochen Arndt建议的,有很多方法可以在C ++中定义2D数组。例如,参见 http://stackoverflow.com / questions / 936687 / how-do-i-declare-a-2d-array-in-c-using-new [ ^ ]



我首选的方法是定义一个矩阵类(或使用现有库中的一个)。但如果您更喜欢C语法,并且不喜欢手动进行索引计算,最简单的方法是:

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



我需要 typedef 只是因为我找不到如何定义类型 a 正确,除非通过使用 auto (我故意在C语法的精神中避免使用)。此外,虽然 auto a = new int [26] [600]; 确实编译,但我不确定它是否符合C ++标准,或者实际上是C的一个特性++ 11,甚至是特定于编译器。


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.


这篇关于调试时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆