Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Memory errors HELP!

Reply
Thread Tools

Memory errors HELP!

 
 
electrixnow
Guest
Posts: n/a
 
      02-08-2006
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

 
Reply With Quote
 
 
 
 
int2str@gmail.com
Guest
Posts: n/a
 
      02-08-2006

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

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      02-09-2006
In article <(E-Mail Removed). com>,
"electrixnow" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      02-09-2006
In article <(E-Mail Removed) .com>,
"(E-Mail Removed)" <(E-Mail Removed)> 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.
 
Reply With Quote
 
electrixnow
Guest
Posts: n/a
 
      02-09-2006
I have changed the code and it still blows up at 182 lines
I found that if I comment out the two string lines noted that it runs
to 19000 + lines. http://www.velocityreviews.com/forums/(E-Mail Removed) suggested I was pushing it with
the array size but it works fine. I the array is full with no error
after the
while statement.

Any clue?

and I am mixing C and C++ because have just started using C++
with VC++ Express, it was free, and I have not found a good online
C++ reference. I use to program in C type shell languages in the 80's.
So I need a good online or electronic VC++ reference library. or
hardback
would work, any suggestions are welcome. Thanks for the help thus far.

#include <fstream>
#include <iostream>
#include <string>

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

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

using namespace std;

ifstream inf(MOVEX_QUERY);

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

document = strtok_s( str1, " ,\t\n", &next_token1);
edition = strtok_s( NULL, " ,\t\n", &next_token1);
// string doc(document); // if these two lines are removed
// string edi(edition); // it will run to 19000 + lines
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;
}

Here is a sample of the data being input.

00-00008RA,0004,,,,,,
00-00009RA,0003,,,,,,
00-00010RA,0003,,,,,,
00-00019RA,0005,,,,,,
00-00021RA,0016,,,,,,
00-00036RA,0016,,,,,,
00-00041RA,0005,,,,,,
00-00042RA,0009,,,,,,
00-00043RA,0003,,,,,,
00-00045RA,0008,,,,,,
00-00047RA,0018,,,,,,
00-00051RA,0018,,,,,,
00-00052RA,0029,,,,,,
00-00054RA,0028,,,,,,
00-00055RA,0023,,,,,,
00-00056RA,0006,,,,,,
00-00057RA,0008,,,,,,
00-00058RA,0005,,,,,,
00-00059RA,0005,,,,,,
00-00076RA,0006,,,,,,
00-00085RA,0025,,,,,,
00-00086RA,0025,,,,,,

 
Reply With Quote
 
Jay_Nabonne
Guest
Posts: n/a
 
      02-09-2006
On Wed, 08 Feb 2006 15:01:47 -0800, 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.


What is on line 182 and 183 of the text file?

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


Could any line be longer than 30 characters?

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


What happens here if the line doesn't have a comma?
"document" or "edition" would be null, and initializing a string from that
will die the way you described.

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


Print out each line as you process it. See what the offending line looks
like.

- Jay

 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      02-09-2006
In article <(E-Mail Removed) .com>,
"electrixnow" <(E-Mail Removed)> wrote:

> I have changed the code and it still blows up at 182 lines
> I found that if I comment out the two string lines noted that it runs
> to 19000 + lines. (E-Mail Removed) suggested I was pushing it with
> the array size but it works fine. I the array is full with no error
> after the
> while statement.
>
> Any clue?
>
> and I am mixing C and C++ because have just started using C++
> with VC++ Express, it was free, and I have not found a good online
> C++ reference. I use to program in C type shell languages in the 80's.
> So I need a good online or electronic VC++ reference library. or
> hardback
> would work, any suggestions are welcome. Thanks for the help thus far.


<http://www.sgi.com/tech/stl/>

>
> #include <fstream>
> #include <iostream>
> #include <string>
>
> #define MOVEX_QUERY "C:\\DWG_DATA\\DOCUMENTS_movex.csv" //
>
> int main(){
> char * document;
> char * edition;
> char * str1;
> char * next_token1;
> int lines=0;
>
> using namespace std;
>
> ifstream inf(MOVEX_QUERY);
>
> if (inf)
> {
> char namn[20000][30];
> while (lines < 20000 && (inf.getline(namn[lines], 30)) !=
> NULL)++lines;
> for (int i = 0; i < lines; ++i){
> str1 = namn[i];
> cout << str1 << '\n';
>
> document = strtok_s( str1, " ,\t\n", &next_token1);
> edition = strtok_s( NULL, " ,\t\n", &next_token1);


I think the reason it crashes is because at line 182 in the file,
'document' is NULL. To prove it do this:

string doc;
string edi;
if ( document && edition ) {
doc = document;
edi = edition;
}
else { cout << "Would have crashed!\n"; }

> // string doc(document); // if these two lines are removed
> // string edi(edition); // it will run to 19000 + lines
> 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;
> }
>
> Here is a sample of the data being input.
>
> 00-00008RA,0004,,,,,,
> 00-00009RA,0003,,,,,,
> 00-00010RA,0003,,,,,,
> 00-00019RA,0005,,,,,,
> 00-00021RA,0016,,,,,,
> 00-00036RA,0016,,,,,,
> 00-00041RA,0005,,,,,,
> 00-00042RA,0009,,,,,,
> 00-00043RA,0003,,,,,,
> 00-00045RA,0008,,,,,,
> 00-00047RA,0018,,,,,,
> 00-00051RA,0018,,,,,,
> 00-00052RA,0029,,,,,,
> 00-00054RA,0028,,,,,,
> 00-00055RA,0023,,,,,,
> 00-00056RA,0006,,,,,,
> 00-00057RA,0008,,,,,,
> 00-00058RA,0005,,,,,,
> 00-00059RA,0005,,,,,,
> 00-00076RA,0006,,,,,,
> 00-00085RA,0025,,,,,,
> 00-00086RA,0025,,,,,,




--
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.
 
Reply With Quote
 
int2str@gmail.com
Guest
Posts: n/a
 
      02-09-2006

electrixnow wrote:
> I have changed the code and it still blows up at 182 lines
> I found that if I comment out the two string lines noted that it runs
> to 19000 + lines. (E-Mail Removed) suggested I was pushing it with
> the array size but it works fine. I the array is full with no error
> after the
> while statement.
>
> Any clue?


Still unrecoverable code .
Using Daniels suggestions and your update on the format, here is a
nicer version:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <map>

using namespace std;

int main()
{
ifstream ff( "test.dat" );
string document, edition, rest;

map<string,string> documents;

while( getline( ff, document, ',' )
&& getline( ff, edition, ',' )
&& getline( ff, rest ))
{
cout << document << " " << edition << endl;
documents.insert(make_pair(document, edition ));
}
}

 
Reply With Quote
 
electrixnow
Guest
Posts: n/a
 
      02-09-2006
I found the problem. Down in the file at the line it blows up at looks
like this.

083256,,,,,,,

I made that line up, but it has no data after the delimiter comma.

also I found that if I put at least 2 new lines in after the last data
line
it also blows up.

I am thinking of a way to fix.

Here is my real program I am working on, disreguard the old one.
-----------

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <ostream>


#define MOVEX_QUERY "C:\\DWG_DATA\\DOCUMENTS_MOVEX.dat"
#define CONTROL_LIST "C:\\DWG_DATA\\DOCUMENTS_LIST.txt"
#define REQUEST_LIST "C:\\DWG_DATA\\REQUEST_LIST.txt"

using namespace std;

typedef vector<string> stringvec;

void upper_case(string in, stringvec &out) {
// Erase all entries from the vector
out.swap(stringvec());
int len = in.length();
for (int a=0; a<len; ++a)
in[a] = toupper(in[a]);
out.push_back(in);
}



int main()
{
char * document;
char * edition;
char input_line[31];
char * next_token1;
int i=0;
ifstream inf(MOVEX_QUERY);
ifstream inf2(CONTROL_LIST);
ofstream outf(REQUEST_LIST);
if (inf)
{
while ((inf.getline(input_line,30)) != NULL)
{
//while (! inf.eof()) // eof did'nt work
//inf.getline(input_line,30); // don't know why
document = strtok_s( input_line, " ,\t\n", &next_token1);
edition = strtok_s( NULL, " ,\t\n", &next_token1);
stringvec doc,edi;
upper_case(document,doc);
upper_case(edition,edi);
cout << "vector document " << doc[0] <<'\n'; // for test
cout << "vector edition " << edi[0] <<'\n'; // for test
cout << i <<'\n'; // for test


if ( doc[0] == edi[0] )
{

if (! outf)
{
cout << "File " << REQUEST_LIST;
cout << " could not be opened.";
return -1;
}
outf << doc[0]<<","<< edi[0]<< endl;
}
++i;
}
}
else
{
cout << "File " << MOVEX_QUERY;
cout << " could not be opened.";
return 1;
}
outf.close();
cout << "PROCESSING COMPLETE\n";
return 0;
}
-----------
Here is a short data test file I made. I won't work on the last line.
526104A, 526104a
526105-3, C
526106-ir ,D
526107-ap,,

 
Reply With Quote
 
electrixnow
Guest
Posts: n/a
 
      02-09-2006
Gotter FIXED
check for empty lines and missing data first!

THANKS FOR THE HELP

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Out-of-memory errors and caching errors. George1776 ASP .Net 2 09-14-2006 03:34 PM
Obsolete Errors and Deprecated Errors using Dotnet Framework 2.0 SenthilVel ASP .Net 0 06-07-2006 11:48 AM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
Internet Explorer causing errors/ slow internet speed and Outlook express errors Jeanne Medley Computer Support 2 02-11-2004 11:44 PM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM



Advertisments