Re: Memory Errors please help
- From: "Jim Langston" <tazmaster@xxxxxxxxxxxxxx>
- Date: Thu, 9 Feb 2006 01:03:27 -0800
"electrixnow" <info...@xxxxxxxxxxx> wrote in message
news:1139439798.367936.240170@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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::allocator<char> >* ,
SByte* )
at main() in c:\documents and settings\grant\my documents\visual
studio 2005\
projects\documents\document1\document1\doc1.cpp:line 31
Press any key to continue . . .
Attempted to read or write protected memory
Please let me know what you think
Thanks
One thing I would try is either change the declaration of namn to:
char namn[20000][31];
or change your getline to:
while ((inf.getline(namn[i], 29)) != NULL)++i;
This is because a c-style string takes an extra byte to hold the null
terminator.
Still, thought, it should hit a null somewhere in the rest of the file.
Have you considered making namn a std::vector<std::string> instead of a
c-style array?
.
- References:
- Memory Errors please help
- From: electrixnow
- Memory Errors please help
- Prev by Date: Re: 64 bit performance speedup over 32 bit
- Next by Date: Re: Use STL classes thread-safely
- Previous by thread: Memory Errors please help
- Next by thread: Re: Memory Errors please help
- Index(es):
Relevant Pages
|