473,770 Members | 5,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access violation error

In the code I supplied before this one, the cause of the problem is an
access violation error. When I run the debugger it skips into what I
can only assume is the compilers version of my code. And I don't know
where the problem lies in the code. Can anyone run it through there
own compiler please and tell me where and what the problem is.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;

struct crecord {
long customercode[5];
char customername[21];
char customeraddress[61];
double customerbalance ;
double creditlimit;

};
crecord Newcrecord;

struct irrecord {
long customercode[5];
long partnum[5];
long issue_rec;

};
irrecord Newirrecord;

struct drecord {
long customercode[5];
};
drecord Newdrecord;


////////////////////////////////////////////////////////////
bool IRRecordvalidle ngth(char* record)
{
int len;

switch(record[0])
{
case'i':
case'I':
case'r':
case'R':

len= strlen(record);
if(len<17)
return false;
break;
}

return true;
};
////////////////////////////////////////////////////////////
bool CRecordvalidlen gth(char* record)
{
int len;

switch(record[0])
{
case'c':
case'C':

len = strlen(record);
if(len<103)
return false;
break;

}
return true;

}
////////////////////////////////////////////////////////
bool Drecordvalidlen gth( char* record )
{
int len;

switch(record[0])
{
case'd':
case'D':

len = strlen(record);
if(len<7)
return false;
break;

}
return true;
}
////////////////////////////////////////////////////////////////////////////
bool CheckAllNumeric ( const char * string )
{
int Len = strlen( string );

for( int i = 0; i < Len; ++i )
if( !isdigit( string[i] ) )
return false;

return true;
}

////////////////////////////////////////////////////////////////////////////
bool CheckFloatNumbe r( const char* string )
{
int Len = strlen( string );

for( int i = 0; i < Len; ++i )
if( !isdigit( string[i] ) &&
string[i] != '.' )
return false;

return true;
}

///////////////////////////////////////////////////////////////////////////
irrecord* CheckDigit(ofst ream& prnfile, ofstream& validdata, char*
record )
{
char code[5];
int weightingfactor = 5;
int checkdigit;
int remainder;
int check1;

strncpy(code, &record[2], 6);
code[5] = '\0';
Newirrecord.cus tomercode[5] = atol( code );
for(int i =0; i < 6; ++i)
check1 += Newirrecord.cus tomercode[i] * weightingfactor ;
weightingfactor --;

remainder = check1 % 11;

checkdigit = 11 - remainder;

if(! Newirrecord.cus tomercode[5] == checkdigit){
prnfile<< "Invalid customer number";
prnfile<< record << endl;
}
return false;

if(Newirrecord. customercode[5] == checkdigit){

validdata.write ((char*) record, Newirrecord.cus tomercode[5]);
}

}
/////////////////////////////////////////////////////////////////////////////////////

drecord* checkdigit(ofst ream& prnfile, ofstream& validdata, char*
record )
{
char code[5];
int weightingfactor = 5;
int checkdigit;
int remainder;
int check1;

strncpy(code, &record[2], 5);
code[5] = '\0';
Newdrecord.cust omercode[5] = atol( code );
for(int i =0; i < 4; ++i)
check1 += Newdrecord.cust omercode[i] * weightingfactor ;
weightingfactor --;

remainder = check1 % 11;

checkdigit = 11 - remainder;

if(! Newdrecord.cust omercode[5] == checkdigit){
prnfile<< "Invalid customer number";
prnfile<< record << endl;
}
return false;

validdata.write ((char*) record, Newdrecord.cust omercode[5]);
}
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
crecord* checkdigitforcu stomercode( ofstream& prnfile, ofstream&
validdata, char* record )
{
char code[5];
int weightingfactor = 5;
int checkdigit;
int remainder;
int check1;

strncpy(code, &record[2], 5);
code[5] = '\0';
Newcrecord.cust omercode[5] = atol( code );
for(int i =0; i < 6; ++i)
check1 += Newcrecord.cust omercode[i] * weightingfactor ;
weightingfactor --;

remainder = check1 % 11;

checkdigit = 11 - remainder;

if(checkdigit == 11){
checkdigit = 'X';
}
else
if(checkdigit = checkdigit = 10){
checkdigit = '0';
}
if(! Newcrecord.cust omercode[5] == checkdigit){
prnfile<< "Invalid customer number";
prnfile<< record << endl;
}
return false;

validdata.write ((char*) record, Newcrecord.cust omercode[5]);

}
////////////////////////////////////////////////////////////////////////////////////
irrecord* checkdigitforpa rtnum(ofstream& prnfile, ofstream& validdata,
char* record)
{
int weightingfactor 2;
char part_num[6];
int partnumcheck;
int produce;
int holdall;

strncpy(part_nu m, &record[7], 6);
part_num[6] = '\0';
Newirrecord.par tnum[6] = atol( part_num );

weightingfactor 2 = 6;

for(int i=0; i < 6; ++i)
holdall += Newirrecord.par tnum[i] * weightingfactor 2;
weightingfactor 2 --;

produce = holdall % 11;

partnumcheck = 11 - produce;
if(partnumcheck == 11){
partnumcheck = 'X';
}
else
if(partnumcheck = 10){
partnumcheck = '0';
}

if(! Newirrecord.par tnum[5] == partnumcheck){
prnfile<< "Invalid: Incorrect part number, check digit invalid";
prnfile<< record << endl;
}
return false;

validdata.write ((char*) record, Newirrecord.par tnum[5]);

}

////////////////////////////////////////////////////////////////////////////////////
bool CheckAddress( const char* alpha )
{

int max_alpha = 60;

for( int i = 0; i < max_alpha; ++i )
if( !isalpha( alpha[i] ) && ( alpha[i] != ';;;;' ) && ( alpha[i] !=
' ' ))
if(Newcrecord.c ustomeraddress[61] != (alpha[i]))
return false;

return true;
}
///////////////////////////////////////////////////////////////////////////////
bool Processcrecord( ofstream& prnfile, ofstream& validdata, char*
record )
{
char customercode[6];
char balance[10];
char limit[10];
crecord Newcrecord;

//
// process the customer code
//
strncpy( customercode, &record[1], 5 );
customercode[5] = '\0';
if( !CheckAllNumeri c( customercode ) ) {
prnfile << "Invalid: customer code needs to be all numeric
[position 3 - 5 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.cust omercode[5] = atol( customercode );

//
// process the customer name
//
strncpy( Newcrecord.cust omername, &record[7], 20 );
Newcrecord.cust omername[20] = '\0';

//
// process the customer address
//
strncpy( Newcrecord.cust omeraddress, &record[27], 61 );
Newcrecord.cust omeraddress[61] = '\0';


// process the customer balance
//
strncpy( balance, &record[87], 9 );
balance[9] = '\0';
if( !CheckFloatNumb er( balance ) ) {
prnfile << "Invalid: balance field is not a valid number [position
88 - 9 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.cust omerbalance = atof( balance );
validdata.write ((char*) record, Newcrecord.cust omerbalance );

//
// process the customer limit
//
strncpy( limit, &record[97], 9 );
limit[9] = '\0';
if( !CheckFloatNumb er( limit ) ) {
prnfile << "Invalid: limit field is not a valid number [position
98 - 9 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.cred itlimit = atof( limit );

validdata.write ((char*) record, Newcrecord.cred itlimit );

return true;

}

///////////////////////////////////////////////////////

bool processdrecord( char* record, ofstream& prnfile, ofstream&
validdata )
{
drecord Newdrecord;
char customercode[5];

strncpy( customercode, &record[1], 5);
customercode[5] = '\0';
if(!CheckAllNum eric( customercode )){
prnfile<< "Invalid: D record customer code does not contain
numerical format";
prnfile<< record <<endl;
return false;
}
Newdrecord.cust omercode[5] = atol( customercode );

validdata.write ((char*) record, Newdrecord.cust omercode[5] );
return true;
}

/////////////////////////////////////////////////////////////////////////
bool ProcessIRecord( ofstream& prnfile, ofstream& validdata, char*
record )
{
char customer[6];
char issue_rec[5];
char part[6];

long customer_code;
long issuerec;
long partnum[6];

//
// process the customer code
//
strncpy( customer, &record[2], 5 );
customer[6] = '\0';
if( !CheckAllNumeri c( customer ) ) {
prnfile << "Invalid: customer code needs to be all numeric
[position 3 - 5 characters]:\n";
prnfile << record << endl;
return false;
}
customer_code = atol( customer );

//
// process the part number
//
strncpy( part, &record[7], 6 );
part[6] = '\0';
if( !CheckAllNumeri c( part ) ) {
prnfile << "Invalid: part number needs to be all numeric [position
9 - 6 characters]:\n";
prnfile << record << endl;
return false;
}
partnum[6] = atol( part );

validdata.write ((char*) record, partnum[6]);

return true;

//
// process the third number
//
strncpy( issue_rec, &record[13], 5 );
issue_rec[5] = '\0';
if( !CheckAllNumeri c( issue_rec ) ) {
prnfile << "Invalid: the code needs to be all numeric [position 16
- 4 characters]:\n";
prnfile << record << endl;
return false;
}
issuerec = atol( issue_rec );
return true;
}

/////////////////////////////////////////////////////////////////////////
int main()
{
const char infile[] = "A:\\514650TD.t xt";
const char outfile[] = "A:\\514650VD.D AT";
const char printerfile[] = "A:\\514650.TXT ";

int max = 256;

char temp1[256];

ifstream testdata;
ofstream prnfile;
ofstream validdata;

testdata.open(" A:\\514650TD.tx t", ios::in);
if(!testdata)
{
cout<<"The file does not already exist" << infile << endl;
return EXIT_FAILURE;
}

validdata.open( "A:\\514650.DAT ", ios::out | ios::binary);
if(!validdata.i s_open())
{
cout<<" The file could not be opened " << outfile <<endl;
return EXIT_FAILURE;
}

prnfile.open("A :\\514650.TXT", ios::out);
if(!prnfile)
{
cout<< "File could not be opened" << prnfile << endl;
return EXIT_FAILURE;
};
prnfile << "C_RECORD, I-R RECORD, D_RECORD ERRORS" << endl;

while( testdata.getlin e( temp1, sizeof(temp1)) ){
if(!CRecordvali dlength( temp1 )){
prnfile<<"Inval id: record does not contain enough characters:\n";
prnfile<< temp1 <<endl;
continue;
}
if(!IRRecordval idlength( temp1 )){
prnfile<< "Invalid: record does not contain enough characters:\n";
prnfile<< temp1 << endl;
continue;
}
if(!Drecordvali dlength( temp1 )){
prnfile<< "Invalid: record does not contain enough characters:\n";
prnfile<< temp1 << endl;
continue;
};

switch( temp1[0] )
{
case 'c':
case 'C':
Processcrecord( prnfile, validdata, temp1 );
checkdigitforcu stomercode( prnfile, validdata, temp1);
break;

case 'i':
case 'I':
case 'r':
case 'R':
ProcessIRecord( prnfile, validdata, temp1 );
CheckDigit( prnfile, validdata, temp1 );
checkdigitforpa rtnum( prnfile, validdata, temp1 );
break;

case 'd':
case 'D':
processdrecord( temp1, prnfile, validdata );
checkdigit( prnfile, validdata, temp1 );
break;
default: prnfile<< "Unknown record";
prnfile<< temp1 << endl;
};

}
testdata.close( );
validdata.close ();
prnfile.close() ;
return EXIT_SUCCESS;

};
Jul 19 '05 #1
11 4197
muser wrote:
In the code I supplied before this one, the cause of the problem is an
access violation error. When I run the debugger it skips into what I
can only assume is the compilers version of my code. And I don't know
where the problem lies in the code. Can anyone run it through there
own compiler please and tell me where and what the problem is.
I'm not going to go through your code line by line, but at least a
couple of things jump right out. See below:


#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;

struct crecord {
long customercode[5];
char customername[21];
char customeraddress[61];
double customerbalance ;
double creditlimit;

};
crecord Newcrecord;

struct irrecord {
long customercode[5];
long partnum[5];
long issue_rec;

};
irrecord Newirrecord;

struct drecord {
long customercode[5];
};
drecord Newdrecord;


////////////////////////////////////////////////////////////
bool IRRecordvalidle ngth(char* record)
{
int len;

switch(record[0])
{
case'i':
case'I':
case'r':
case'R':

len= strlen(record);
if(len<17)
return false;
break;
}

return true;
};
////////////////////////////////////////////////////////////
bool CRecordvalidlen gth(char* record)
{
int len;

switch(record[0])
{
case'c':
case'C':

len = strlen(record);
if(len<103)
return false;
break;

}
return true;

}
////////////////////////////////////////////////////////
bool Drecordvalidlen gth( char* record )
{
int len;

switch(record[0])
{
case'd':
case'D':

len = strlen(record);
if(len<7)
return false;
break;

}
return true;
}
////////////////////////////////////////////////////////////////////////////
bool CheckAllNumeric ( const char * string )
{
int Len = strlen( string );

for( int i = 0; i < Len; ++i )
if( !isdigit( string[i] ) )
return false;

return true;
}

////////////////////////////////////////////////////////////////////////////
bool CheckFloatNumbe r( const char* string )
{
int Len = strlen( string );

for( int i = 0; i < Len; ++i )
if( !isdigit( string[i] ) &&
string[i] != '.' )
return false;

return true;
}

///////////////////////////////////////////////////////////////////////////
irrecord* CheckDigit(ofst ream& prnfile, ofstream& validdata, char*
record )
{
char code[5];
int weightingfactor = 5;
int checkdigit;
int remainder;
int check1;

strncpy(code, &record[2], 6);
Your copying 6 `char's into an array of length 5.
code[5] = '\0';
No, `code[5]' does not exist. The last element in the array is
`code[4]' (arrays in C++ are zero-based, hence valid indices run
from 0 to 4).
Newirrecord.cus tomercode[5] = atol( code );
for(int i =0; i < 6; ++i)
check1 += Newirrecord.cus tomercode[i] * weightingfactor ;
weightingfactor --;

remainder = check1 % 11;

checkdigit = 11 - remainder;

if(! Newirrecord.cus tomercode[5] == checkdigit){
prnfile<< "Invalid customer number";
prnfile<< record << endl;
}
return false;

if(Newirrecord. customercode[5] == checkdigit){

validdata.write ((char*) record, Newirrecord.cus tomercode[5]);
}

}
/////////////////////////////////////////////////////////////////////////////////////

drecord* checkdigit(ofst ream& prnfile, ofstream& validdata, char*
record )
{
char code[5];
int weightingfactor = 5;
int checkdigit;
int remainder;
int check1;

strncpy(code, &record[2], 5);
code[5] = '\0';
Similarly, see above.
Newdrecord.cust omercode[5] = atol( code );
for(int i =0; i < 4; ++i)
check1 += Newdrecord.cust omercode[i] * weightingfactor ;
weightingfactor --;

remainder = check1 % 11;

checkdigit = 11 - remainder;

if(! Newdrecord.cust omercode[5] == checkdigit){
prnfile<< "Invalid customer number";
prnfile<< record << endl;
}
return false;

validdata.write ((char*) record, Newdrecord.cust omercode[5]);
}
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
crecord* checkdigitforcu stomercode( ofstream& prnfile, ofstream&
validdata, char* record )
{
char code[5];
int weightingfactor = 5;
int checkdigit;
int remainder;
int check1;

strncpy(code, &record[2], 5);
code[5] = '\0';
Newcrecord.cust omercode[5] = atol( code );
for(int i =0; i < 6; ++i)
check1 += Newcrecord.cust omercode[i] * weightingfactor ;
weightingfactor --;

remainder = check1 % 11;

checkdigit = 11 - remainder;

if(checkdigit == 11){
checkdigit = 'X';
}
else
if(checkdigit = checkdigit = 10){
checkdigit = '0';
}
if(! Newcrecord.cust omercode[5] == checkdigit){
prnfile<< "Invalid customer number";
prnfile<< record << endl;
}
return false;

validdata.write ((char*) record, Newcrecord.cust omercode[5]);

}
////////////////////////////////////////////////////////////////////////////////////
irrecord* checkdigitforpa rtnum(ofstream& prnfile, ofstream& validdata,
char* record)
{
int weightingfactor 2;
char part_num[6];
int partnumcheck;
int produce;
int holdall;

strncpy(part_nu m, &record[7], 6);
part_num[6] = '\0';
Newirrecord.par tnum[6] = atol( part_num );

weightingfactor 2 = 6;

for(int i=0; i < 6; ++i)
holdall += Newirrecord.par tnum[i] * weightingfactor 2;
weightingfactor 2 --;

produce = holdall % 11;

partnumcheck = 11 - produce;
if(partnumcheck == 11){
partnumcheck = 'X';
}
else
if(partnumcheck = 10){
partnumcheck = '0';
}

if(! Newirrecord.par tnum[5] == partnumcheck){
prnfile<< "Invalid: Incorrect part number, check digit invalid";
prnfile<< record << endl;
}
return false;

validdata.write ((char*) record, Newirrecord.par tnum[5]);

}

////////////////////////////////////////////////////////////////////////////////////
bool CheckAddress( const char* alpha )
{

int max_alpha = 60;

for( int i = 0; i < max_alpha; ++i )
if( !isalpha( alpha[i] ) && ( alpha[i] != ';;;;' ) && ( alpha[i] !=
' ' ))
if(Newcrecord.c ustomeraddress[61] != (alpha[i]))
return false;

return true;
}
///////////////////////////////////////////////////////////////////////////////
bool Processcrecord( ofstream& prnfile, ofstream& validdata, char*
record )
{
char customercode[6];
char balance[10];
char limit[10];
crecord Newcrecord;

//
// process the customer code
//
strncpy( customercode, &record[1], 5 );
customercode[5] = '\0';
if( !CheckAllNumeri c( customercode ) ) {
prnfile << "Invalid: customer code needs to be all numeric
[position 3 - 5 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.cust omercode[5] = atol( customercode );

//
// process the customer name
//
strncpy( Newcrecord.cust omername, &record[7], 20 );
Newcrecord.cust omername[20] = '\0';

//
// process the customer address
//
strncpy( Newcrecord.cust omeraddress, &record[27], 61 );
Newcrecord.cust omeraddress[61] = '\0';


// process the customer balance
//
strncpy( balance, &record[87], 9 );
balance[9] = '\0';
if( !CheckFloatNumb er( balance ) ) {
prnfile << "Invalid: balance field is not a valid number [position
88 - 9 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.cust omerbalance = atof( balance );
validdata.write ((char*) record, Newcrecord.cust omerbalance );

//
// process the customer limit
//
strncpy( limit, &record[97], 9 );
limit[9] = '\0';
if( !CheckFloatNumb er( limit ) ) {
prnfile << "Invalid: limit field is not a valid number [position
98 - 9 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.cred itlimit = atof( limit );

validdata.write ((char*) record, Newcrecord.cred itlimit );

return true;

}

///////////////////////////////////////////////////////

bool processdrecord( char* record, ofstream& prnfile, ofstream&
validdata )
{
drecord Newdrecord;
char customercode[5];

strncpy( customercode, &record[1], 5);
customercode[5] = '\0';
if(!CheckAllNum eric( customercode )){
prnfile<< "Invalid: D record customer code does not contain
numerical format";
prnfile<< record <<endl;
return false;
}
Newdrecord.cust omercode[5] = atol( customercode );

validdata.write ((char*) record, Newdrecord.cust omercode[5] );
return true;
}

/////////////////////////////////////////////////////////////////////////
bool ProcessIRecord( ofstream& prnfile, ofstream& validdata, char*
record )
{
char customer[6];
char issue_rec[5];
char part[6];

long customer_code;
long issuerec;
long partnum[6];

//
// process the customer code
//
strncpy( customer, &record[2], 5 );
customer[6] = '\0';
if( !CheckAllNumeri c( customer ) ) {
prnfile << "Invalid: customer code needs to be all numeric
[position 3 - 5 characters]:\n";
prnfile << record << endl;
return false;
}
customer_code = atol( customer );

//
// process the part number
//
strncpy( part, &record[7], 6 );
part[6] = '\0';
if( !CheckAllNumeri c( part ) ) {
prnfile << "Invalid: part number needs to be all numeric [position
9 - 6 characters]:\n";
prnfile << record << endl;
return false;
}
partnum[6] = atol( part );

validdata.write ((char*) record, partnum[6]);

return true;

//
// process the third number
//
strncpy( issue_rec, &record[13], 5 );
issue_rec[5] = '\0';
if( !CheckAllNumeri c( issue_rec ) ) {
prnfile << "Invalid: the code needs to be all numeric [position 16
- 4 characters]:\n";
prnfile << record << endl;
return false;
}
issuerec = atol( issue_rec );
return true;
}

/////////////////////////////////////////////////////////////////////////
int main()
{
const char infile[] = "A:\\514650TD.t xt";
const char outfile[] = "A:\\514650VD.D AT";
const char printerfile[] = "A:\\514650.TXT ";

int max = 256;

char temp1[256];

ifstream testdata;
ofstream prnfile;
ofstream validdata;

testdata.open(" A:\\514650TD.tx t", ios::in);
if(!testdata)
{
cout<<"The file does not already exist" << infile << endl;
return EXIT_FAILURE;
}

validdata.open( "A:\\514650.DAT ", ios::out | ios::binary);
if(!validdata.i s_open())
{
cout<<" The file could not be opened " << outfile <<endl;
return EXIT_FAILURE;
}

prnfile.open("A :\\514650.TXT", ios::out);
if(!prnfile)
{
cout<< "File could not be opened" << prnfile << endl;
return EXIT_FAILURE;
};
prnfile << "C_RECORD, I-R RECORD, D_RECORD ERRORS" << endl;

while( testdata.getlin e( temp1, sizeof(temp1)) ){
if(!CRecordvali dlength( temp1 )){
prnfile<<"Inval id: record does not contain enough characters:\n";
prnfile<< temp1 <<endl;
continue;
}
if(!IRRecordval idlength( temp1 )){
prnfile<< "Invalid: record does not contain enough characters:\n";
prnfile<< temp1 << endl;
continue;
}
if(!Drecordvali dlength( temp1 )){
prnfile<< "Invalid: record does not contain enough characters:\n";
prnfile<< temp1 << endl;
continue;
};

switch( temp1[0] )
{
case 'c':
case 'C':
Processcrecord( prnfile, validdata, temp1 );
checkdigitforcu stomercode( prnfile, validdata, temp1);
break;

case 'i':
case 'I':
case 'r':
case 'R':
ProcessIRecord( prnfile, validdata, temp1 );
CheckDigit( prnfile, validdata, temp1 );
checkdigitforpa rtnum( prnfile, validdata, temp1 );
break;

case 'd':
case 'D':
processdrecord( temp1, prnfile, validdata );
checkdigit( prnfile, validdata, temp1 );
break;
default: prnfile<< "Unknown record";
prnfile<< temp1 << endl;
};

}
testdata.close( );
validdata.close ();
prnfile.close() ;
return EXIT_SUCCESS;

};


The items noted above are sufficient to produce undefined behavior
-- after which all bets are off.

Also, in the future, pare down your code to the smallest possible
compilable snippet *before* you post; in doing so, you will often
find the error yourself!

HTH,
--ag
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 19 '05 #2
"muser" <ch**********@h otmail.com> wrote in message
news:f9******** *************** ***@posting.goo gle.com...
In the code I supplied before this one, the cause of the problem is an
access violation error. When I run the debugger it skips into what I
can only assume is the compilers version of my code. And I don't know
where the problem lies in the code. Can anyone run it through there
own compiler please and tell me where and what the problem is

[SNIP WAY TOO MUCH INFORMATION]

If you read the replies to your earlier post, you will see your problem.
Maybe your news server is slow - even then try to keep posts about the same
problem in the same thread; wait at least a day or two to see if your post
went through.

You frequently write past the end of an array, as in:

char code[5]; // has 5 chars, numbered _0_ to _4_
code[5] = '\0'; // you are writing to char _5_ which does not exist!

Don't do this.
Jul 19 '05 #3
"Kevin Saff" <go********@kev in.saff.net> wrote in message news:<Hn******* *@news.boeing.c om>...
"muser" <ch**********@h otmail.com> wrote in message
news:f9******** *************** ***@posting.goo gle.com...
In the code I supplied before this one, the cause of the problem is an
access violation error. When I run the debugger it skips into what I
can only assume is the compilers version of my code. And I don't know
where the problem lies in the code. Can anyone run it through there
own compiler please and tell me where and what the problem is

[SNIP WAY TOO MUCH INFORMATION]

If you read the replies to your earlier post, you will see your problem.
Maybe your news server is slow - even then try to keep posts about the same
problem in the same thread; wait at least a day or two to see if your post
went through.

You frequently write past the end of an array, as in:

char code[5]; // has 5 chars, numbered _0_ to _4_
code[5] = '\0'; // you are writing to char _5_ which does not exist!

Don't do this.


Kevin, doesn't my code[5] contain a NULL character and isn't that
housed in the fifth element?
Jul 19 '05 #4

"muser" <ch**********@h otmail.com> wrote in message news:f9******** *************** ***@posting.goo gle.com...
You frequently write past the end of an array, as in:

char code[5]; // has 5 chars, numbered _0_ to _4_
code[5] = '\0'; // you are writing to char _5_ which does not exist!

Don't do this.


Kevin, doesn't my code[5] contain a NULL character and isn't that
housed in the fifth element?


No, code[5] refers to the sixth element.

code[0] is the first, code[1] is the scond, code[2] is the third, code[3] is the forth,
code[4] is the fifth.

These are the only locations you are allowed to access.

code[5] is one past the end of the allocation. You touch it, you can't count
on any specific behavior.
Jul 19 '05 #5
muser wrote:

"Kevin Saff" <go********@kev in.saff.net> wrote in message news:<Hn******* *@news.boeing.c om>...
"muser" <ch**********@h otmail.com> wrote in message
news:f9******** *************** ***@posting.goo gle.com...
In the code I supplied before this one, the cause of the problem is an
access violation error. When I run the debugger it skips into what I
can only assume is the compilers version of my code. And I don't know
where the problem lies in the code. Can anyone run it through there
own compiler please and tell me where and what the problem is

[SNIP WAY TOO MUCH INFORMATION]

If you read the replies to your earlier post, you will see your problem.
Maybe your news server is slow - even then try to keep posts about the same
problem in the same thread; wait at least a day or two to see if your post
went through.

You frequently write past the end of an array, as in:

char code[5]; // has 5 chars, numbered _0_ to _4_
code[5] = '\0'; // you are writing to char _5_ which does not exist!

Don't do this.


Kevin, doesn't my code[5] contain a NULL character and isn't that
housed in the fifth element?


code[5] is the *sixth* element. char code[5] declares an array of five
elements, numbered 0 to 4.

James
Jul 19 '05 #6
"Kevin Saff" <go********@kev in.saff.net> wrote in message news:<Hn******* *@news.boeing.c om>...
"muser" <ch**********@h otmail.com> wrote in message
news:f9******** *************** ***@posting.goo gle.com...
In the code I supplied before this one, the cause of the problem is an
access violation error. When I run the debugger it skips into what I
can only assume is the compilers version of my code. And I don't know
where the problem lies in the code. Can anyone run it through there
own compiler please and tell me where and what the problem is

[SNIP WAY TOO MUCH INFORMATION]

If you read the replies to your earlier post, you will see your problem.
Maybe your news server is slow - even then try to keep posts about the same
problem in the same thread; wait at least a day or two to see if your post
went through.

You frequently write past the end of an array, as in:

char code[5]; // has 5 chars, numbered _0_ to _4_
code[5] = '\0'; // you are writing to char _5_ which does not exist!

Don't do this.


sorry kevin, posted my reply moments after getting up, disregard my
reply, and thank you for the advance.
Jul 19 '05 #7
Kevin Saff <go********@kev in.saff.net> writes
You frequently write past the end of an array, as in:

char code[5]; // has 5 chars, numbered _0_ to _4_
code[5] = '\0'; // you are writing to char _5_ which does not exist!


I'm a bit surprised that the OP's compiler didn't issue a warning on
this.
--
Simon Elliott
http://www.ctsn.co.uk/


Jul 19 '05 #8
Simon Elliott wrote:
Kevin Saff <go********@kev in.saff.net> writes
You frequently write past the end of an array, as in:

char code[5]; // has 5 chars, numbered _0_ to _4_
code[5] = '\0'; // you are writing to char _5_ which does not exist!

I'm a bit surprised that the OP's compiler didn't issue a warning on
this.


I'd be very surprised if any compiler did, actually.

- Adam

--
Reverse domain name to reply.

Jul 19 '05 #9
Adam Fineman <af******@retup moc.org> writes:
Simon Elliott wrote:
Kevin Saff <go********@kev in.saff.net> writes
You frequently write past the end of an array, as in:

char code[5]; // has 5 chars, numbered _0_ to _4_
code[5] = '\0'; // you are writing to char _5_ which does not exist!

I'm a bit surprised that the OP's compiler didn't issue a warning on
this.


I'd be very surprised if any compiler did, actually.


Be surprised, then :-)

$icc -w2 compilertest.cp p
compilertest.cp p(3): warning #175: subscript out of range
code[5] = '\0';

$icc -V
Intel(R) C++ Compiler for 32-bit applications, Version 7.0 Build 20030212Z
Copyright (C) 1985-2003 Intel Corporation. All rights reserved.

kind regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
18031
by: Steven Reddie | last post by:
I understand that access violations aren't part of the standard C++ exception handling support. On Windows, a particular MSVC compiler option enables Microsoft's Structured Exception Handling (SEH) in C++ EH so that a catch (...) will catch an access violation. I don't know if other platforms support something similar. I'm wondering about how to best protect an application or library from poorly written user-defined callbacks. It...
0
2724
by: Steven Reddie | last post by:
In article <slrnbnj19j.av.juergen@monocerus.manannan.org>, Juergen Heinzl wrote: >In article <f93791bd.0309282133.650da850@posting.google.com>, Steven Reddie wrote: >> I understand that access violations aren't part of the standard C++ >> exception handling support. On Windows, a particular MSVC compiler >> option enables Microsoft's Structured Exception Handling (SEH) in C++ >> EH so that a catch (...) will catch an access violation. ...
7
3140
by: Daniel | last post by:
I want to write a method to remove the last node of the linked list. But the error "Access Violation" exists and the error point to this method. What does it means by Access Violation and how can I debug it? Thanks void RemoveNode(StepNodePtr pList) { StepNodePtr preq, q; q = pList; preq = pList;
0
1814
by: Microsoft News | last post by:
I'm getting the following error when I shut down my C# .NET v1.1 application: 0xC0000005: Access violation reading location 0x73bc0000 This error didn't occur until I added a TabControl to my form and placed a
0
2533
by: Bruce Pataki | last post by:
I am creating an MFC application with activeX document server support. The application runs perfectly fine when i run as a standalone application. But when i run the application in Internet explorer, it opens however opening a form from the top menubar, it gives an error "access violation reading location" having error code 0xC0000005 (Running in standalone mode does not give this error). I have used global pointers to the objects I...
1
13906
by: =?Utf-8?B?c2F0aGVlc2t1bWFy?= | last post by:
In my project i have a component named PTS Engine which is developed in VC++ ..Net 2003. And it is migrated to VC++.NET 2005 version. Initially this migration process has coded and tested in VC++ .NET 2005 Beta version. In beta version everything is working fine. When i tryied to run in .NET 2005 full version i am facing the following access violation Error. "Unhandled exception at 0x4bc0145c in Engine.exe: 0xc0000005: Access Violation...
2
2317
by: jthep | last post by:
I'm trying to get this piece of code I converted from C to work in C++ but I'm getting an access violation error. Problem occurs at line 61. Someone can help me with this? The function display(llist mylist) displays a list of choices for a record book: void display(llist mylist) { char name, address, telno, input; int yearofbirth, choice, records; yearofbirth = 0;
0
1751
by: Paavo Helde | last post by:
Shawn <sfncook@gmail.comwrote in news:6410e273-318e-44a0-9735-b922366ca1ab@w1g2000prd.googlegroups.com: I bet you have some multithreading access error. Ensure that your threads do not read or write any shared variables without proper locking. If you are not able to spot the error by the code inspection, then I suggest the Intel Thread Checker tool - it finds such kind of errors for you automatically (although not necessarily faster...
2
4294
by: =?Utf-8?B?c29jYXRvYQ==?= | last post by:
Hi, I have a DLL in VC6, when a specific function is called it will spawns a few threads and then return. The threads stay running and inside one of these threads an event is created using the win32 CreateEvent() call: Code Snippet static HANDLE hReadyEvent;
39
4291
by: Martin | last post by:
I have an intranet-only site running in Windows XPPro, IIS 5.1, PHP 5.2.5. I have not used or changed this site for several months - the last time I worked with it, all was well. When I tried it just now, I am getting the subject error message (specifically: PHP has encountered an access violation at 00F76E21). The error is NOT occurring on every page request (but it is on most of them) and, when I get the error, simply pressing <F5to...
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10053
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10001
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.