473,396 Members | 1,853 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,396 software developers and data experts.

How to fix Access violation reading location error?

I finished working on my first draft for my email homework for my C++ class but when debugging I recieved a Access violation reading location 0x00370000 error. I know that it has to do with a stack or heap overflow but I don't know hot to correct it. My code is below, any suggestions?

Expand|Select|Wrap|Line Numbers
  1. // main.cpp - email checker (HW1) main entry point
  2. // By Emilio Gonzalez
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. #define MAX_SIZE 260
  9. #define NOT_FOUND -1
  10.  
  11. //Returns the length of text by scanning for null-term
  12. int GetLength(char* text)
  13. {
  14.     int i;
  15.     for(i = 0; text[i] != '/0'; i++)
  16.     {
  17.  
  18.     }
  19.     return i;
  20. }
  21.  
  22.  
  23. bool CheckRuleLocal(char* text, int Achar)
  24. {
  25.     int invalid = 0;
  26.     if(text[0] == '.'|| text[Achar - 1] == '.')
  27.         invalid++;
  28.     if(text[0] == '-'|| text[Achar - 1] == '-')
  29.         invalid++;
  30.     for(int i = 0; i < Achar && invalid == 0; i++)
  31.     {
  32.         if(text[i] == '.' && text[i + 1] == '.')
  33.             invalid++;
  34.         if(text[i] == '-' && text[i + 1] == '-')
  35.             invalid++;
  36.     }
  37.  
  38.     if(invalid > 0)
  39.         return false;
  40.     else
  41.         return true;
  42. }
  43.  
  44. bool CheckRuleDomain(char* text, int Achar, int length)
  45. {
  46.     int invalid = 0;
  47.     if(text[Achar +    1] == '.'|| text[length - 1] == '.')
  48.         invalid++;
  49.     if(text[Achar +    1] == '-'|| text[length - 1] == '-')
  50.         invalid++;
  51.     for(int i = Achar + 1; i < MAX_SIZE && invalid == 0; i++)
  52.     {
  53.         if(text[i] == '.' && text[i + 1] == '.')
  54.             invalid++;
  55.         if(text[i] == '-' && text[i + 1] == '-')
  56.             invalid++;
  57.     }
  58.  
  59.     if(invalid > 0)
  60.         return false;
  61.     else
  62.         return true;
  63. }
  64.  
  65.  
  66. bool ValidCharsL(char* text, char* invalidCharacters, int Achar)
  67. {
  68.     int invalid = 0;
  69.  
  70.     for(int i = 0; i < Achar && invalid == 0; i++)
  71.     {
  72.         for(int t = 0; t < GetLength(invalidCharacters); t++)
  73.         {
  74.             if(text[i] == invalidCharacters[t])
  75.                 invalid++;
  76.         }
  77.     }
  78.     if(invalid > 0)
  79.         return false;
  80.     else
  81.         return true;
  82. }
  83.  
  84. bool ValidCharsD(char* text, char* invalidCharacters, int Achar)
  85. {
  86.     int invalid = 0;
  87.  
  88.     for(int i = Achar + 1; text[i] != '/0'; i++)
  89.     {
  90.         for(int t = 0; t < GetLength(invalidCharacters); t++)
  91.         {
  92.             if(text[i] == invalidCharacters[t])
  93.                 invalid++;
  94.         }
  95.     }
  96.     if(invalid > 0)
  97.         return false;
  98.     else
  99.         return true;
  100. }
  101.  
  102. //Finds the position of @ and returns an int  that will store value if not found returns -1
  103. int Find(char* text, int arraySize)
  104. {
  105.     int AIndex = 0;
  106.     for(int i = 0; i <= arraySize; i++)
  107.     {
  108.         if(text[i] == '@')
  109.             AIndex = i;
  110.     }
  111.         if(AIndex > 0)
  112.             return AIndex;
  113.         else
  114.             return NOT_FOUND;
  115.  
  116. }
  117.  
  118.  
  119. int CheckLengthLocal(char* text, int Achar)
  120. {
  121.     int count = 0;
  122.     for(int i = 0; i != Achar; i++)
  123.     {
  124.         count ++;
  125.     }
  126.     return count;
  127. }
  128.  
  129. int CheckLengthDomain(char* text, int Achar)
  130. {
  131.     int count = 0;
  132.     for(int i = Achar + 1; text[i] != '/0'; i++)
  133.     {
  134.         count ++;
  135.     }
  136.     return count;
  137. }
  138.  
  139. bool IsValid(char* email)
  140. {
  141.     int Length, atIndex, LocalLength, DomainLength, aCheck = 0;
  142.     char* Invalid = "()[]\;:,<>";
  143.  
  144.     // Searches for the NULL terminator at the end of the char array and returns Length of the arry
  145.     Length = GetLength(email);
  146.  
  147.     if(Length <= 256)
  148.     {
  149.         // Finds the @ char and returns an int variable labeling its position
  150.         atIndex = Find(email, Length);
  151.  
  152.         aCheck++;
  153.  
  154.         if(atIndex != NOT_FOUND)
  155.         {
  156.             LocalLength = CheckLengthLocal(email, atIndex);
  157.             DomainLength = CheckLengthDomain(email, atIndex);
  158.  
  159.             aCheck++;
  160.  
  161.             if(LocalLength <= 64 && DomainLength <= 253)
  162.             {
  163.                 aCheck++;
  164.  
  165.                 if(ValidCharsL(email, Invalid, atIndex) == true && ValidCharsD(email, Invalid, atIndex) == true)
  166.                 {
  167.                     aCheck++;
  168.  
  169.                     if(CheckRuleLocal(email, atIndex) == true && CheckRuleDomain(email, atIndex, Length) == true)
  170.                     {
  171.                         aCheck++;
  172.  
  173.                         return true;
  174.                     }
  175.                 }
  176.             }
  177.         }
  178.     }
  179.     if(aCheck != 5)
  180.         return false;
  181.  
  182.  
  183.  
  184.  
  185.  
  186.     //Email = local@domain
  187.     //domain = subdomain1.subdomain2....tld
  188.  
  189.     //NOTE: tlds once tld is identified run match to identify valid tlds: only letters, atleast 2 chars
  190.     // Algorithm
  191.  
  192.     // 1) Find @ and place its position into atIndex
  193.     //   - 0..atIndex is going to be local part
  194.     //   - atIndex + 1..length(email)-1 = domain
  195.     // 2) Check the length of entire email, should be <= 256
  196.     // 3) Check length of Local Part, <=64
  197.     // 4) Check length of domain, <= 253
  198.     // 5) Check localPart for allowed chracters
  199.     //   - 0..9 A..Z a..z _ - .
  200.     // 6) Check Domain for allowed chracters
  201.     //   - 0..9 A..Z a..z _ - .
  202.     // 7) Dot rule on Local Part
  203.     //   - cannot begin with a dot
  204.     //   - cannot end with a .
  205.     //   - cannot have two .
  206.     // 8) Dash rule on Local Part
  207.     //   - cannot begin with a -
  208.     //   - cannot end with a -
  209.     //   - cannot have two -
  210.     // 9-10) Repeat 7-8 for domain
  211.  
  212. }
  213.  
  214. // Dash/dot rule checker
  215. // specialChar can be a dash or a dot
  216. // can add another argument
  217. // CheckRule(email, '.', atIndex +1)
  218. // CheckRule(&email[atIndex + 1], '.')
  219. // CheckRule(&email + atIndex + 1, '.')
  220. //
  221. bool CheckRule(char* emailPart, char speacialChar);
  222.  
  223. //Returns the length of text by scanning for null-term
  224.  
  225.  
  226.  
  227. // Returns the first position of aChar in text when aChar is found
  228. // or -1 (NOT_FOUND) otherwise
  229.  
  230.  
  231. // returns false when text tat are not present in validCharacters
  232. bool CheckForValidChars(char* text, char* validCharacters);
  233.  
  234. void main()
  235. {
  236.     //High level alg
  237.  
  238.     // O) Open input file and output file
  239.     ifstream input("Email.txt");
  240.     ofstream output("Result.txt");
  241.  
  242.     // Oa) While not at the end of input
  243.     while(!input.eof())
  244.     {
  245.         char email[MAX_SIZE];
  246.  
  247.         // Ob)Read email input
  248.         input.getline(email, MAX_SIZE);
  249.  
  250.         // Validat email
  251.         if (IsValid(email))
  252.  
  253.         // Write results to output file
  254.         output<< "1" << endl;
  255.         else
  256.         output << "0" << endl;
  257.     }
  258.  
  259.         // out << IsValid(email) << endl;
  260.  
  261.     //Email = local@domain
  262.     //domain = subdomain1.subdomain2....tld
  263.  
  264.     // Algorithm
  265.  
  266.     // 1) Find @ and place its position into atIndex
  267.     //   - 0..atIndex is going to be local part
  268.     //   - atIndex + 1..length(email)-1 = domain
  269.     // 2) Check the length of entire email, should be <= 256
  270.     // 3) Check length of Local Part, <=64
  271.     // 4) Check length of domain, <= 253
  272.     // 5) Check localPart for allowed chracters
  273.     //   - 0..9 A..Z a..z _ - .
  274.     // 6) Check Domain for allowed chracters
  275.     //   - 0..9 A..Z a..z _ - .
  276.     // 7) Dot rule on Local Part
  277.     //   - cannot begin with a dot
  278.     //   - cannot end with a .
  279.     //   - cannot have two .
  280.     // 8) Dash rule on Local Part
  281.     //   - cannot begin with a -
  282.     //   - cannot end with a -
  283.     //   - cannot have two -
  284.     // 9-10) Repeat 7-8 for domain
  285.  
  286.  
  287.     system("pause");
  288. }
Feb 17 '11 #1
2 7776
Bump. Anything even a "I don't get it".
Feb 20 '11 #2
johny10151981
1,059 1GB
The reason is very simple..................

Your for loop are always an infinite loop

Why?
Expand|Select|Wrap|Line Numbers
  1.  for(i = 0; text[i] != '/0'; i++)
'/0' this is not String terminating Character. '\0' this is string terminating character.

so it keep checking statement is always remaining true. But char array length is 256 so, when it reach 256th memory its automatically invalid.. Memory access violation occurred with no doubt.

You also have mistake in
char* Invalid = "()[]\;:,<>";

in your checking you used single back slash but to read single back slash you must have to put double back slash "\\"
Feb 20 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Nasser | last post by:
Hello, I am coding a mathematical model with VC++. When I debug the code, I face with no erroe, but during executing, I face with below error "Unhandled exception at 0x0040c275 in Tar.exe:...
0
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...
3
by: Fred | last post by:
I receive this message under certain conditions: 1. "Unmanaged Code Debugging" is checked in Property Pages. 2. Certain routines are "stressed." The error does not appear unless loops are...
2
by: bhreddy | last post by:
Hi All, Can someone help me out how can I resolve the error "0xC0000005: Access violation reading location 0x513112f4"? Steps I followed... 1. I ran the application at DOS prompt 2. After...
1
by: Yanping Zhang | last post by:
Here are more details about my codes, please help! The function declared in C: typedef void (WINAPI *PLEARNCALLBACKPROC) (unsigned int progress, unsigned int sigQuality, unsigned long...
1
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++...
3
by: raghunadhs | last post by:
Hi all, i have developed a ".dll" in vc++, regarding to find out CRC32. and in my V.B 6.0 application i am calling that .dll. If i run my v.b application it is working means.. i am able to find...
13
by: ycinar | last post by:
A quick question: Why doesn't the following code catch the Access violation reading location exception? it crashes on line if ( xyz ) with an Access violation reading location exception. Is not...
3
by: Pinux | last post by:
Hi, I am writing a multi-threads encryption application. The idea of the code is to create a number of threads to encrypt files. I have a thread pool say the maximum threads is 10. If the number...
1
by: poko | last post by:
I have come across an exception that I have no idea about. " Unhandled exception at 0x00a2ac1b in SampleSocketClient.exe: 0xC0000005: Access violation writing location 0x00000020." Below is the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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...
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
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...
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,...

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.