内存错误帮助! [英] Memory errors HELP!

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

问题描述

我有以下测试代码打开并读取文件。

文件超过19000行。该文件在每一行包含两个字段

:text,text \ n


我已经测试了一个最多182个循环的测试程序错误

out。

我在代码后面放了错误信息:


我正在使用VC ++ Express Edition


#include< fstream>

#include< iostream>

#include< string>

#include< stdio.h>

#define MOVEX_QUERY" C:\\DWG_DATA \\DOCUMENTS_movex.csv"


int main(){

char * document;

char * edition;

char * str1;

char * next_token1;

int i = 0;


使用std :: string;

使用std :: ifstream;

使用std :: cout;


ifstream inf(MOVEX_QUERY);


if(inf)

{

char namn [20000] [30];

while((inf.getline(namn [i],30))!= NULL)+ + i;

for(int i = 0; i < 182; ++ i){

str1 = namn [i];

cout<< str1<< ''\ n'';


document = strtok_s(str1,",\t\ n",& next_token1);

edition = strtok_s(NULL,",\t\ n",& next_token1);

std :: string doc(document);

std :: string edi(edition);

if(edi == doc)cout<< 这件作品 <<''\ n'';

printf("%s \ n",document);

printf("%s \ n",edition);

cout<<我<<''\ n'';

}


}

其他

{

cout<< 无法打开文件\ n;;

返回1;

}

cout<< PROCESSING COMPLETE\\\
;

返回0;

}

这只是测试代码,有些东西可以删除。

如果我将for循环更改为182或更高

我收到如下消息:

未处理的异常:System.AccessViolationException:Attempted阅读

或写

受保护的内存。这通常表明其他内存已损坏。

at std.basic_string< char,std :: char_traits< char> ;,std :: allocator< char> < blockquote class =post_quotes>。{ctor}



(basic_string< char \,std :: char_traits< char> \,std :: a llocator< char>> *,

SByte *)

at main()in c:\ document / settings \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ b studio 2005 \

projects \documents\document1 \document1 \doc1.cpp:li ne 31

按任意键继续。 。 。


尝试读取或写入受保护的内存


请让我知道您的想法


谢谢

解决方案



electrixnow写道:

我有以下打开并读取文件的测试代码。
该文件超过19000行。该文件在每行包含两个字段
:text,text \ n

我已经进行了一个测试,对于最多182个循环和程序错误

我在代码后面放了错误信息:

我正在使用VC ++ Express Edition




[snipped code]


很难确定,但它看起来像是内存错误。你正在尝试

在堆栈上分配20000个30个字符长度的字符串。那是'

拉伸它肯定。


你发布的代码混合了很多C风格。使用C ++函数和

类。为什么不一直走,让你的代码看起来更友好

友好?


喜欢这样:

#include< ; iostream>

#include< string>

#include< fstream>

#include< sstream>

#include< vector>


使用命名空间std;


struct Doc

{

字符串名称;

字符串版本;

};


int main()

{

ifstream ff(" test.dat");

string line;


vector< Doc> ;文件;

documents.reserve(10000);


while(getline(ff,line))

{

string :: size_type comma_pos = line.find('','');

if(string :: npos == comma_pos)

continue;


Doc doc;

doc.name = line.substr(0,comma_pos);

doc.edition = line。 substr(comma_pos + 1);


cout<< doc.name<< " " << doc.edition<< endl;


documents.push_back(doc);

}

}


然后,您可以以任何您喜欢的方式处理文档向量。


干杯,

Andre

< br>

文章< 11 ********************* @ g47g2000cwa.googlegroups。 com>,

" electrixnow" <信息... @ charter.net>写道:

我有以下测试代码打开并读取文件。
该文件超过19000行。该文件在每行包含两个字段
:text,text \ n"


我必须说,你有一个相当尴尬的C和C ++代码组合。如果你删除了所有的字符,我认为你好了b $ b会好多了。

我已经测试了for循环高达182并且程序错误
out。
我在代码后面放了错误信息:


我刚把它跑到了1750行没有崩溃......我怀疑你的

代码仅读取182行。 : - /

我正在使用VC ++ Express Edition

#include< fstream>
#include< iostream>
#include< string> ;
#include< stdio.h>
#define MOVEX_QUERY" C:\\DWG_DATA \\DOCUMENTS_movex.csv"

int main(){
char * document;
char * edition;
char * str1;
char * next_token1;
int i = 0;


您是否知道您定义了两个名称为我的不同变量?

(此处以及下面的for循环。)不是好主意也许是来源

您的问题。


将上面的行更改为int lines = 0;

使用std :: string;
使用std :: ifstream;
使用std :: cout;

ifstream inf(MOVEX_QUERY);

if(inf)
{char namn [20000] [30];
while((inf.getline(namn [i],30))!= NULL)++ i;


以上''我'必须改为''line'',而后卫可能是个好主意。

while(行< 20000&& inf.getline(namn [lines],30)!= NULL)

++行;


我怀疑如果你把:


cout<<线<< ''\ n'';


你会发现它输出182.

for(int i = 0; i< 182; ++ i){


将上面的''182'改为''line''

str1 = namn [i];
cout< ;< str1<< ''\ n'';

document = strtok_s(str1,",\t\ n",& next_token1);
edition = strtok_s(NULL," ,\t\ n",& next_token1);
std :: string doc(document);
std :: string edi(edition);
if(edi == doc )cout<< 这件作品 <<''\ n'';
printf("%s\ n",document);
printf("%s \ n",edition);
cout<<我<<''\ n'';
}

}
其他
{
cout<< 无法打开文件\ n;
返回1;
}
cout<< PROCESSING COMPLETE\\\
;
返回0;
}

这只是测试代码,有些东西可以删除。
如果我改变for循环到182或更大
我收到如下消息:

未处理的异常:System.AccessViolationException:尝试读取
或写入受保护的内存。这通常表明其他内存已损坏。
在std.basic_string< char,std :: char_traits< char> ;,std :: allocator< char>

。 {ctor}


(basic_string< char \,std :: char_traits< char> \,std :: a llocator< char>> *,
SByte *)
at the main()in c:\documents and settings \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ :li ne 31
按任意键继续。 。 。

尝试读取或写入受保护的内存

请让我知道您的想法

谢谢




-

魔术取决于传统和信仰。它不欢迎观察,

也不会通过实验获利。另一方面,科学的经验基于
;它可以通过观察和实验来纠正。


在文章< 11 ******************** ** @ g47g2000cwa.googlegroups .com>,

" in ***** @ gmail.com" <在***** @ gmail.com>写道:

electrixnow写道:

我有以下测试代码打开并读取文件。
该文件超过19000行。该文件在每行包含两个字段
:text,text \ n

我已经进行了一个测试,对于最多182个循环和程序错误

我在代码后面放了错误信息:

我正在使用VC ++ Express Edition



[snipped code]

您发布的代码混合了大量的C风格。使用C ++函数和
类。为什么不一直走,让你的代码看起来更友好?

如下:
#include< iostream>
#include< string> ;
#include< fstream>
#include< sstream>
#include< vector>
使用命名空间std;

struct Doc
{
字符串名称;
字符串版本;
};

int main()
{/ / ifstream ff(" test.dat");
字符串行;

vector< Doc>文件;
documents.reserve(10000);

while(getline(ff,line))
{
string :: size_type comma_pos = line.find(' ','');
if(string :: npos == comma_pos)
继续;

Doc doc;
doc.name = line.substr(0 ,comma_pos);
doc.edition = line.substr(comma_pos + 1);

cout<< doc.name<< " " << doc.edition<< endl;

documents.push_back(doc);
}


然后您可以以任何您喜欢的方式处理文档向量。




或者更好......


int main()

{

ifstream inf(MOVEX_QUERY);

vector< Document>文件;

文件doc;

while(getline(inf,doc.name,'','')&& getline(inf,doc.edition))

documents.push_back(doc);

//你会对文件做什么......

}


根据我的想法,我知道要解决OP的问题,地图可能会更有意义......


int main()

{

ifstream inf(MOVEX_QUERY);

map< string,string>文件;

string doc;

string edi;

while(getline(inf,doc,'','')&& getline(inf,edi)){

if(documents.count(doc)== 0)

documents.insert(make_pair(doc,edi));

else

cout<< 重复输入找到了\ n; //这里做什么用?

}


以上内容可以更方便地比较来自

的文档版本这个文件,与另一个文件的同一文档的版本。


-

魔术取决于传统和信仰。它不欢迎观察,

也不会通过实验获利。另一方面,科学的经验基于
;它可以通过观察和实验进行校正。


I have the following test code that opens and reads a file.
The file is over 19000 lines long. The file contains two fields
on each line: "text,text\n"

I have placed a test that for loops up to 182 and the program errors
out.
I placed the error message after the code:

I am using VC++ Express Edition

#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>
#define MOVEX_QUERY "C:\\DWG_DATA\\DOCUMENTS_movex.csv"

int main(){
char * document;
char * edition;
char * str1;
char * next_token1;
int i=0;

using std::string;
using std::ifstream;
using std::cout;

ifstream inf(MOVEX_QUERY);

if (inf)
{
char namn[20000][30];
while ((inf.getline(namn[i], 30)) != NULL)++i;
for (int i = 0; i < 182; ++i){
str1 = namn[i];
cout << str1 << ''\n'';

document = strtok_s( str1, " ,\t\n", &next_token1);
edition = strtok_s( NULL, " ,\t\n", &next_token1);
std::string doc(document);
std::string edi(edition);
if ( edi == doc ) cout << "THIS WORKS" <<''\n'';
printf( "%s\n", document );
printf( "%s\n", edition );
cout << i <<''\n'';
}

}
else
{
cout << "Could not open file\n";
return 1;
}
cout << "PROCESSING COMPLETE\n";
return 0;
}
This is just test code, there are things that could be removed.
If I change the for loop to 182 or greater
I get a message as follows:
Unhandled Exception: System.AccessViolationException: Attempted to read
or write
protected memory. This is often an indication that other memory is
corrupt.
at std.basic_string<char,std::char_traits<char>,std:: allocator<char>

.{ctor}


(basic_string<char\,std::char_traits<char>\,std::a llocator<char> >* ,
SByte* )
at main() in c:\documents and settings\grant\my documents\visual
studio 2005\
projects\documents\document1\document1\doc1.cpp:li ne 31
Press any key to continue . . .

Attempted to read or write protected memory

Please let me know what you think

Thanks

解决方案


electrixnow wrote:

I have the following test code that opens and reads a file.
The file is over 19000 lines long. The file contains two fields
on each line: "text,text\n"

I have placed a test that for loops up to 182 and the program errors
out.
I placed the error message after the code:

I am using VC++ Express Edition



[snipped code]

Hard to tell for sure, but it looks like a memory error. You''re trying
to allocate 20000 strings of 30 character length on the stack. That''s
stretching it for sure.

The code you posted mixes a lot of "C style" with C++ functions and
classes. Why not go all the way and make your code look a little more
friendly?

Like so:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

struct Doc
{
string name;
string edition;
};

int main()
{
ifstream ff( "test.dat" );
string line;

vector<Doc> documents;
documents.reserve( 10000 );

while( getline( ff, line ))
{
string::size_type comma_pos = line.find( '','' );
if ( string::npos == comma_pos )
continue;

Doc doc;
doc.name = line.substr( 0, comma_pos );
doc.edition = line.substr( comma_pos + 1 );

cout << doc.name << " " << doc.edition << endl;

documents.push_back( doc );
}
}

You can then process the documents vector in any way you like.

Cheers,
Andre


In article <11*********************@g47g2000cwa.googlegroups. com>,
"electrixnow" <info...@charter.net> wrote:

I have the following test code that opens and reads a file.
The file is over 19000 lines long. The file contains two fields
on each line: "text,text\n"
I must say, you have a rather awkward mix of C and C++ code. I think you
would be much better off if you removed all the char*s
I have placed a test that for loops up to 182 and the program errors
out.
I placed the error message after the code:
I just ran it out to 1750 lines with no crash... I suspect that your
code is only reading in 182 lines. :-/
I am using VC++ Express Edition

#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>
#define MOVEX_QUERY "C:\\DWG_DATA\\DOCUMENTS_movex.csv"

int main(){
char * document;
char * edition;
char * str1;
char * next_token1;
int i=0;
Did you know you define two different variables both with the name ''i''?
(Here and in the for loop below.) Not a good idea and maybe the source
of your problem.

Change the line above to "int lines = 0;"
using std::string;
using std::ifstream;
using std::cout;

ifstream inf(MOVEX_QUERY);

if (inf)
{
char namn[20000][30];
while ((inf.getline(namn[i], 30)) != NULL)++i;
The above ''i''s will have to be changed to ''lines'' and a guard would
probably be a good idea.

while (lines < 20000 && inf.getline(namn[lines], 30) != NULL)
++lines;

I suspect that if you put:

cout << lines << ''\n'';

You will find that it outputs 182.
for (int i = 0; i < 182; ++i){
Change the ''182'' above to ''lines''
str1 = namn[i];
cout << str1 << ''\n'';

document = strtok_s( str1, " ,\t\n", &next_token1);
edition = strtok_s( NULL, " ,\t\n", &next_token1);
std::string doc(document);
std::string edi(edition);
if ( edi == doc ) cout << "THIS WORKS" <<''\n'';
printf( "%s\n", document );
printf( "%s\n", edition );
cout << i <<''\n'';
}

}
else
{
cout << "Could not open file\n";
return 1;
}
cout << "PROCESSING COMPLETE\n";
return 0;
}
This is just test code, there are things that could be removed.
If I change the for loop to 182 or greater
I get a message as follows:
Unhandled Exception: System.AccessViolationException: Attempted to read
or write
protected memory. This is often an indication that other memory is
corrupt.
at std.basic_string<char,std::char_traits<char>,std:: allocator<char>

.{ctor}


(basic_string<char\,std::char_traits<char>\,std::a llocator<char> >* ,
SByte* )
at main() in c:\documents and settings\grant\my documents\visual
studio 2005\
projects\documents\document1\document1\doc1.cpp:li ne 31
Press any key to continue . . .

Attempted to read or write protected memory

Please let me know what you think

Thanks



--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.


In article <11**********************@g47g2000cwa.googlegroups .com>,
"in*****@gmail.com" <in*****@gmail.com> wrote:

electrixnow wrote:

I have the following test code that opens and reads a file.
The file is over 19000 lines long. The file contains two fields
on each line: "text,text\n"

I have placed a test that for loops up to 182 and the program errors
out.
I placed the error message after the code:

I am using VC++ Express Edition



[snipped code]

Hard to tell for sure, but it looks like a memory error. You''re trying
to allocate 20000 strings of 30 character length on the stack. That''s
stretching it for sure.

The code you posted mixes a lot of "C style" with C++ functions and
classes. Why not go all the way and make your code look a little more
friendly?

Like so:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

struct Doc
{
string name;
string edition;
};

int main()
{
ifstream ff( "test.dat" );
string line;

vector<Doc> documents;
documents.reserve( 10000 );

while( getline( ff, line ))
{
string::size_type comma_pos = line.find( '','' );
if ( string::npos == comma_pos )
continue;

Doc doc;
doc.name = line.substr( 0, comma_pos );
doc.edition = line.substr( comma_pos + 1 );

cout << doc.name << " " << doc.edition << endl;

documents.push_back( doc );
}
}

You can then process the documents vector in any way you like.



Or better yet...

int main( )
{
ifstream inf( MOVEX_QUERY );
vector<Document> documents;
Document doc;
while ( getline( inf, doc.name, '','' ) && getline( inf, doc.edition ) )
documents.push_back( doc );
// do what you will to the documents...
}

From what I think I know about the OP''s problem to solve, a map may make
more sense...

int main( )
{
ifstream inf( MOVEX_QUERY );
map<string, string> documents;
string doc;
string edi;
while ( getline( inf, doc, '','' ) && getline( inf, edi ) ) {
if ( documents.count( doc ) == 0 )
documents.insert( make_pair( doc, edi ) );
else
cout << "duplicate entry found\n"; // what to do here?
}

The above will make it easer to compare the edition of a document from
this file, to the edition of the same document of the other file.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.


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

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