473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

access violation problem


int main()
{
char record_typec = 'c';
record_typec = toupper(record_typec);
* rec->Newcrecord.record_type = record_typec;

etc
etc

\*********************************************\

I've run a debug on the program and this is where I'm encountering the access
violation problem. I've tried the dot operator to access the structure but that
doesn't appear to work. How can change the syntax and avoid the access
violation error?
Jul 22 '05 #1
13 2180
"JasBascom" <ja*******@aol.com> wrote in message
news:20***************************@mb-m11.aol.com
int main()
{
char record_typec = 'c';
record_typec = toupper(record_typec);
* rec->Newcrecord.record_type = record_typec;

etc
etc

\*********************************************\

I've run a debug on the program and this is where I'm encountering
the access violation problem. I've tried the dot operator to access
the structure but that doesn't appear to work. How can change the
syntax and avoid the access violation error?

Since most people here lack psychic abilities, I suggest that you tell us
how rec is defined.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2
JasBascom wrote:

int main()
{
char record_typec = 'c';
record_typec = toupper(record_typec);
* rec->Newcrecord.record_type = record_typec;

etc
etc

\*********************************************\

I've run a debug on the program and this is where I'm encountering the
access violation problem. I've tried the dot operator to access the
structure but that doesn't appear to work. How can change the syntax
and avoid the access violation error?


How would anyone hwere know? What is rec? Where is it defined? It seems
to be a pointer, so where did you let it point to something? What is
the '*' at the beginning of the last line supposed to do?

Jul 22 '05 #3
rec is the pointer to a union
it is declared thus union Allrecords* rec.
Now that you know what rec is does that help you to help me?
Jul 22 '05 #4
"JasBascom" <ja*******@aol.com> wrote in message
news:20***************************@mb-m11.aol.com
rec is the pointer to a union
it is declared thus union Allrecords* rec.
Now that you know what rec is does that help you to help me?


Yes. A pointer simply stores a memory address. Thus rec is meant to store
the address of an instance of Allrecords. However, you never create an
instance of Allrecords (as far as one can tell from the still-limited
information that you have provided), so rec never gets the chance to perform
its proper function. Instead, whatever value rec is storing points to memory
that you don't own and hence you get an access violation when you try to
write to it. You could create an instance of Allrecords with the following:

union Allrecords* rec = new Allrecords;

which allocates memory for an instance of Allrecords and makes rec point to
it. You could also forget about pointers and just declare:

union Allrecords rec_obj;

to create an Allrecords object called rec_obj.

Whether you are accessing the members of this structure correctly with

* rec->Newcrecord.record_type = record_typec;

is again impossible to tell without seeing the definition of Allrecords.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #5
On 20 Feb 2004 13:46:17 GMT in comp.lang.c++, ja*******@aol.com
(JasBascom) was alleged to have written:
int main()
{
char record_typec = 'c';
record_typec = toupper(record_typec);
* rec->Newcrecord.record_type = record_typec;

etc


Variable "rec" is never declared above.
"Newrecord" is never declared above.
etc.

Post small, simplified but COMPLETE and COMPILEABLE samples so people
don't have to guess what you are talking about.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[5.8] How do I post a question about code that doesn't work correctly?"
It is always good to check the FAQ before posting. You can get the FAQ
at:
http://www.parashift.com/c++-faq-lite/

Jul 22 '05 #6
The full program is follows. Thank you john for pointing out the access
violation error. i also have a problem with the declaring of an fstream object
- sort_file. For some reason my debugger is unable to move beyond that point in
the code.

I would also like to have rec.Newcrecord.record_type assigned the character 'c'
and to toupper the record_type. can you please help in both instances.

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

struct crecord {
char record_type;
char customercode[5];
char customername[21];
char customeraddress[61];
char customerbalance;
char creditlimit;
int Totalbalance;
int Totalcreditlimit;

};
struct irrecord {
char record_type;
char customercode[5];
char partnum[6];
char issue_rec[5];

};
struct drecord {
char record_type;
char customercode[5];
};
int loop = 200;
long offset = 1;

union Allrecords{
struct crecord Newcrecord;
struct irrecord Newirrecord;
struct drecord Newdrecord;

};
union Allrecords* rec;


void sort_function( union Allrecords* rec, fstream& validdata )
{

union Allrecords *str_ptr1, *str_ptr2, tempstr;
for(int i =0; i< loop; i++)
while( strcmp(str_ptr1[i].Newcrecord.customercode, '\0') ||
strcmp(str_ptr1[i].Newdrecord.customercode, '\0') ||
strcmp(str_ptr1[i].Newirrecord.customercode, '\0'))
{
str_ptr2 = str_ptr1 + 1;//set to next element.

for( i=0; i<loop; i++)
while( strcmp(str_ptr2[i].Newcrecord.customercode, '\0') ||
strcmp(str_ptr2[i].Newdrecord.customercode, '\0'))
{
for(int i=0; i<loop; i++)
if( strcmp( str_ptr1[i].Newirrecord.customercode,
str_ptr2[i].Newirrecord.customercode + 1))
{
tempstr = *str_ptr1;
*str_ptr1 = *str_ptr2;
*str_ptr2 = tempstr;

}
*str_ptr1++;//incremented, so that the same code isn't sorted again
}
str_ptr2++;
}

}



int main()
{
const char sorted_file[] = "A:\\514650SDP2.txt";
const char outfile[] = "A:\\514650VDP1.bin";


long offset = 1;
int filesize;
int reccount;

fstream sort_file;
fstream validdata;

sort_file.open("A:\\514650SDP2.txt", ios::out);
if(!sort_file)
{
cout<<"Cannot create file"<< endl;
return EXIT_FAILURE;
};

validdata.open("A:\\514650VDP1.bin", ios::in || ios::binary);
if(!validdata)
{
cout<<" Cannot find file"<<endl;
return EXIT_FAILURE;
};
validdata.seekg(-offset, ios::end);
filesize = validdata.tellg();
validdata.seekg(offset, ios::beg);

reccount = sizeof(filesize)/sizeof(Allrecords);
rec = new(Allrecords[reccount]);
validdata.read((char*) rec, filesize);//read the whole file.

for(int i =0; i <reccount; i++)
{
switch(rec[i].Newdrecord.record_type)
{
case 'c':
case 'C':
case 'i':
case 'I':
case 'r':
case 'R':
case 'd':
case 'D':
sort_function(rec, validdata);
default:;
};

};

return 0;

}


Jul 22 '05 #7
On 20 Feb 2004 16:57:46 GMT in comp.lang.c++, ja*******@aol.com
(JasBascom) was alleged to have written:
while( strcmp(str_ptr1[i].Newcrecord.customercode, '\0') ||


Note that '\0', being a const integral expression with a value of zero,
is a valid C++ null pointer. But not what you wanted. What you wrote
is equivalent to

while( strcmp(str_ptr1[i].Newcrecord.customercode, NULL) ||

while I think what you wanted is

while( strcmp(str_ptr1[i].Newcrecord.customercode, "") ||
Jul 22 '05 #8
On 20 Feb 2004 16:57:46 GMT in comp.lang.c++, ja*******@aol.com
(JasBascom) was alleged to have written:
validdata.open("A:\\514650VDP1.bin", ios::in || ios::binary);


Wrong, need bitwise or, not boolean or, should be

validdata.open("A:\\514650VDP1.bin", ios::in | ios::binary);

Jul 22 '05 #9
On 20 Feb 2004 16:57:46 GMT in comp.lang.c++, ja*******@aol.com
(JasBascom) was alleged to have written:
for(int i=0; i<loop; i++)
if( strcmp( str_ptr1[i].Newirrecord.customercode,
str_ptr2[i].Newirrecord.customercode + 1))

{
OK, I give up on following the logic of your sort function. Especially
with all the comparison tests mixed in with the sort logic. How bad
would it hurt to replace it all with std::sort() ?

bool compare(const Allrecords* left, const Allrecords* right)
{
return strcmp( left->Newirrecord.customercode,
right->Newirrecord.customercode));
}

Then,

std::sort(rec, rec+reccount, compare);

This is probably not the exact right code because I gave up on exactly
following your logic, but how far off is it?

Jul 22 '05 #10
thank you so far, but i feel the sort is ok, i won't know until I can get the
sort file to work. The sort file does create a text file on the floppy disk,
but when I go to execute the program there is an access violation error
concerning the sort file.
Thanks again for your help, can someone run the program rthrough their own
compiler just to check.
The program is suppose to open a binary file (in this case validdata), and sort
the contents of validdata and then write this to sort_file.
if anyone is feel brave or a little bored can they actually write a program for
me that opens a text file and a binary file and get the contents of the binary
file to the sort_file.

Jul 22 '05 #11
Comments follow.

I see you didn't follow my earlier advice not to try to do too much at once.
Your post is full of code which does nothing like what you hope it does.
This isn't surprising, programming is hard and you are new at this.

There is a way to avoid wasting your time writing lots of code which will
just have to be thrown away. The way is to write small amounts of code at a
time (literally 3, 4, 5 lines at most), and get those lines working before
you write any more. This is how professionals work, although their
experience means that they can write code in bigger chunks than 3 - 5 lines.

The way to have you tearing your hair out is to write lots of code without
doing any testing at all.

Don't worry newbies never follow this advice. You'll find out the hard way.
"JasBascom" <ja*******@aol.com> wrote in message
news:20***************************@mb-m11.aol.com...
The full program is follows. Thank you john for pointing out the access
violation error. i also have a problem with the declaring of an fstream object - sort_file. For some reason my debugger is unable to move beyond that point in the code.

I would also like to have rec.Newcrecord.record_type assigned the character 'c' and to toupper the record_type. can you please help in both instances.

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

struct crecord {
char record_type;
char customercode[5];
char customername[21];
char customeraddress[61];
char customerbalance;
char creditlimit;
int Totalbalance;
int Totalcreditlimit;

};
struct irrecord {
char record_type;
char customercode[5];
char partnum[6];
char issue_rec[5];

};
struct drecord {
char record_type;
char customercode[5];
};
int loop = 200;
long offset = 1;

union Allrecords{
struct crecord Newcrecord;
struct irrecord Newirrecord;
struct drecord Newdrecord;

};
union Allrecords* rec;


void sort_function( union Allrecords* rec, fstream& validdata )
{

union Allrecords *str_ptr1, *str_ptr2, tempstr;
for(int i =0; i< loop; i++)
while( strcmp(str_ptr1[i].Newcrecord.customercode, '\0') ||
strcmp(str_ptr1[i].Newdrecord.customercode, '\0') ||
strcmp(str_ptr1[i].Newirrecord.customercode, '\0'))
{
str_ptr2 = str_ptr1 + 1;//set to next element.

for( i=0; i<loop; i++)
while( strcmp(str_ptr2[i].Newcrecord.customercode, '\0') ||
strcmp(str_ptr2[i].Newdrecord.customercode, '\0'))
{
for(int i=0; i<loop; i++)
if( strcmp( str_ptr1[i].Newirrecord.customercode,
str_ptr2[i].Newirrecord.customercode + 1))
{
tempstr = *str_ptr1;
*str_ptr1 = *str_ptr2;
*str_ptr2 = tempstr;

}
*str_ptr1++;//incremented, so that the same code isn't sorted again
}
str_ptr2++;
}

}
This function will crash as soon as it is entered. The problem is the
uninitialised pointers str_ptr1 and str_ptr2. This is the same mistake you
made in main. More seriously this code performs nothing like a sort, not
even close, it should be thrown away.



int main()
{
const char sorted_file[] = "A:\\514650SDP2.txt";
const char outfile[] = "A:\\514650VDP1.bin";


long offset = 1;
int filesize;
int reccount;

fstream sort_file;
fstream validdata;

sort_file.open("A:\\514650SDP2.txt", ios::out);
if(!sort_file)
{
cout<<"Cannot create file"<< endl;
return EXIT_FAILURE;
};

validdata.open("A:\\514650VDP1.bin", ios::in || ios::binary);
if(!validdata)
{
cout<<" Cannot find file"<<endl;
return EXIT_FAILURE;
};
validdata.seekg(-offset, ios::end);
filesize = validdata.tellg();
validdata.seekg(offset, ios::beg);
What is this? Why seek to one byte before then end of the file? What do you
think that achieves?

reccount = sizeof(filesize)/sizeof(Allrecords);
This is wrong. Should be filesize not sizeof(filesize). This is a good
example of where you are gonig wrong. Obviously you cannot go any further
until you have worked out the number of records in the file. Until you can
do this the rest of your program isn't going to work. So you should have
written the above code, and then STOPPED THERE! Tested your code and seen if
you calculated the number of records correctly. Then you can carry on and
write some more code. At the moment your code doesn't work, and you have no
idea of its the number of records that is wrong, or something completely
different.

rec = new(Allrecords[reccount]);


validdata.read((char*) rec, filesize);//read the whole file.

for(int i =0; i <reccount; i++)
{
switch(rec[i].Newdrecord.record_type)
{
case 'c':
case 'C':
case 'i':
case 'I':
case 'r':
case 'R':
case 'd':
case 'D':
sort_function(rec, validdata);
This seems a bit confused, are you trying to sort all the records or just
one record? I presume you are trying to sort all the records, but then why
is the sort_function call in a loop?
default:;
};

};

return 0;

}


Seriously the best advice would be to throw this code away and take my
advice to write the program a little bit at a time. You are just heading for
weeks and weeks of frustration any other way.

john
Jul 22 '05 #12

"JasBascom" <ja*******@aol.com> wrote in message
news:20***************************@mb-m11.aol.com...
thank you so far, but i feel the sort is ok, i won't know until I can get the sort file to work.
That's the problem isn't it? Suppose you get the sort file to work, maybe
the changes you make as a result of that, will mean that you have to rewrite
the sort function.

For the record the sort function is not remotely right.
The sort file does create a text file on the floppy disk,
but when I go to execute the program there is an access violation error
concerning the sort file.
Thanks again for your help, can someone run the program rthrough their own
compiler just to check.
The program is suppose to open a binary file (in this case validdata), and sort the contents of validdata and then write this to sort_file.
if anyone is feel brave or a little bored can they actually write a program for me that opens a text file and a binary file and get the contents of the binary file to the sort_file.


Something like this (untested, but please test it your self!)

ifstream validdata("myfile", ios::in|ios::binary);
validdata.seekg(0, ios::end);
filesize = validdata.tellg();
recount = filesize/sizeof(Allrecords);
rec = new Allrecords[reccount];
validdata.seekg(0, ios::beg);
validdata.read(rec, filesize);
validdata.close();

john
Jul 22 '05 #13
On Fri, 20 Feb 2004 18:32:29 -0000 in comp.lang.c++, "John Harrison"
<jo*************@hotmail.com> was alleged to have written:

"JasBascom" <ja*******@aol.com> wrote in message
news:20***************************@mb-m11.aol.com...
thank you so far, but i feel the sort is ok, i won't know until I can get

the
sort file to work.


That's the problem isn't it? Suppose you get the sort file to work, maybe
the changes you make as a result of that, will mean that you have to rewrite
the sort function.


Ditto. Comment out the sort function and don't even worry about it
until the program reads and writes the file without sorting it.

Jul 22 '05 #14

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

Similar topics

1
by: SCS | last post by:
System: Windows 2003 Server PHP 5 Final IIS 6 Problem: Every time I run a PHP page I get "PHP has encountered an Access Violation at 017473CD" at the bottom of the page... But even worse,...
9
by: Allen | last post by:
Hi all, I tried posting to one of the ms.win32 groups and haven't gotten a response yet. It may be my C++ that's at fault anyway so I'll try here too. I wrote a .dll that is misbehaving...
0
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...
7
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...
1
by: Thomas Albrecht | last post by:
My application fails during initialization of the dlls with an ExecutionEngineException and a access violation in the MFC app. The structure of the program looks like: MFC app -> mixed DLL ->...
0
by: techie | last post by:
I have created an event sink in my ATL COM project. The event sink receives events from a C# component. There is no problem with receiving events but when my COM object is released I get an...
0
by: techie | last post by:
I have created an event sink in my ATL COM project. The event sink receives events from a C# component. There is no problem with receving events but when my COM object is released I get an access...
10
by: Robert | last post by:
I am an attorney in a non-profit organization and a self-taught programmer. I'm trying to create a client db that will allow me to search for potential conflicts of interest based either on Social...
2
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...
39
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.