Re: Memory Errors please help

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"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?





.



Relevant Pages

  • Re: Nested loop with no overlap
    ... > all the loops are for the same integers range. ... void swap(char *l, char *r) ... void reverse ... int compare ...
    (comp.programming)
  • Re: Reversing a string
    ... 100000 loops, best of 3: 6.24 usec per loop ... Note that in the second version, I changed the code a little bit so that it no longer iterates over every char in the string and instead just calls listo convert it to a list of chars in order to call list.reverseon it. ...
    (comp.lang.python)
  • Memory Errors please help
    ... char * document; ... printf("%s\n", document); ... This is just test code, there are things that could be removed. ... Attempted to read or write protected memory ...
    (microsoft.public.vc.language)
  • creating child processes
    ... whats wrong?? ... y for 2 loops, it prints 3 loops? ... int main{ ... char A = 'A'; ...
    (comp.os.linux.development.system)