Access violation reading location 0x00000006 - c++

I have the following code which finds the strings that contain no Alphabets. Cases like mynumber123 shall not be recognized and the numberFinder() should return false and case like 123 shall be recognized and numberFinder() shall return true as well as the begin index of the number.
the constructor:
CaddressParser::CaddressParser(string fileName) //constructor
{
m_fileName=fileName;
int length=getLength(m_fileName.c_str());
m_text =fileReader(m_fileName.c_str());
m_length=length;
}
which initializes a string m_text that contains the contents of a text file
Somewhere along the implementation I come across the following code:
for (i;i<m_length;i++)
{
bool UpperCaseBeforeNoFound=false;
if(this->numberFinder (i).second)
{
//do some calculations.
}
}
the numberFinder function is implemented as follows:
pair <int,bool> CaddressParser::numberFinder(int counter)
{
bool noFound=isdigit(m_text[counter]); //number found? -> true
if(noFound)
{
int end=HouseNoDigits(counter);
if(((counter-1)>=0) && ((counter +end-1) <m_length))
{
if((!(isalpha(m_text[counter-1]))) && (!isalpha(m_text[counter+end-1])))
{
return make_pair(counter,noFound); //return index if true
}
}
}
else return make_pair(0,noFound);
}
Now the problem is for a text file containing the following text "he23 Market street London Q12 H13". I get the error mentioned in the headline and the debugger takes me to the line in the which contains :
if(this->numberFinder (i).second)
I can't figure out why this is happening. Please help me figure it out.

If this condition in CaddressParser::numberFinder(int counter) fails:
if (counter - 1 >= 0 && counter + end - 1 < m_length)
the function will exit without returning a value, resulting in undefined behavior.
The complexity of the conditionals in the function isn't helped by the poor formatting (at least as posted in the question).
You might get the behavior you need by removing the else so any 'fall-through' will return the default pair value (but that will depend on if that's the value you want to really return in that scenario):
pair <int,bool> CaddressParser::numberFinder(int counter)
{
bool noFound=isdigit(m_text[counter]); //number found? -> true
if(noFound)
{
// ...
}
return make_pair(0,noFound);
}

Access Violation error is normally due to NULL reference. One of the function that you are calling is trying to access a NULL pointer. Make sure your isdigit function returns true or false, m_text points to an exiting memory location. If not you need to allocate the memory. You should also check if the fileName is NULL.

Related

Cannot dereference double pointer, " no match for operator* "

I'm trying to search through an array of pointers to objects of class Shape. I have written the following code. However, I'm getting this error: "no match for operator*", and I don't know where to go from here. Any help is appreciated.
Shape** shapesArray;
bool doesNameExist(string name) {
for (int i = 0; i < shapeCount; i++)
{
if(*(shapesArray[i])->getName() == name)
{
return true;
}
else
{
return false;
}
}
}
shapesArray is a Shape**
shapesArray[i] is Shape*
(shapesArray[i])->getName() is dereferencing shapesArray[i] and calls its
member getName
So far nothing wrong. I guess this is what you actually want to get, but you add another *:
*(shapesArray[i])->getName() tries to dereference what was returned from getName (a std::string perhaps?)
PS: You return from the loop in the first iteration in either case. If you want to search in the array you need to loop until you find it (then return true) or loop till the end (then return false after the loop, because it wasn't found).

bool method inside a class ''Control may reach end o non-void function''

So, i have a class called Vuelo, it has a method in which i can add a passenger to an airplane flight, i must check that the passenger id is not already in the array (the array is at first with all zeros), i must also check that there is enough space for another passenger to be added (max 10)
bool Vuelo :: agregarPasajero(int id)
{
int i = 0;
for(int iC = 0; iC < 10; iC++)
{
if (listaPasajeros[iC] == id)
{
i++;
return false;
}
}
if(i == 0)
{
if(cantidadPasajeros >= 10)
{
return false;
}
else
{
return true;
cantidadPasajeros++;
}
}
}
If i is not zero, you get to the end of the function without any kind of return statement. Since you declared the function to always return a bool, you should provide one for that case.
Now, you may know that i will never be zero at that spot, but the logic for that is fairly complex (I missed it on the first reading), and a compiler cannot be expected to realize that there is in fact no chance of control flow ever getting to the end of the function without encountering a return. In this case it's best to add a dummy return.
You can probably get away with not having a dummy return if you remove the bogus i == 0 test. i will necessarily always be zero at that point, since if it were ever increased, the function immediately returns false.
The statement cantidadPasajeros++; will never be executed since it is located after a return statement. Any halfway decent compiler also warns on that.

C++ String Length Check

bool fitsKey3(string n) {
int ncheck = str.length(n);
if (ncheck = KEY3) {
return true;
} else {
return false;
}
}
The above function uses a string "n" that is a string given to the function from an input file. I want to write this function that checks the length of this "identifier code" from the input file (it's a drone project), and if the length of the security code is equal to the constant integer "KEY3 (= 50), it returns true. Otherwise, return false.
How do I fix this setup?
= assigns the value of KEY3 to ncheck.
== compares ncheck and KEY3 for equality.
Also, unless you're being paid by lines of code, I'd suggest using the much simpler and clearer form:
return n.length() == KEY3;
(I corrected your usage of the length() member function, since I suppose it was only a typo.)
And as pointed out by Anon Mail, unless you want to make a copy of the string every time you call the function, I'd suggest only passing a reference to it (const because you're not modifying it):
bool fitsKey3(string const& n)
I would write it like this:
bool fitsKey3(string n) {
return n.length() == KEY3;
}
You do two operations:
Get string length by n.length()
Compare the length with a KEY3 (NB: use == to compare)

An example of an inserting and modifying function in an array help (c++)

Basically, I've got almost the whole code done, I'm just getting errors and I don't know what I should take out or add to these functions. I know they're linked in a way in that their language should be almost opposite of each other.
In the Insert function, it's supposed to check and see if the new entry already exists (if it does, then the function returns nothing) and if the entry doesn't exist, then it inserts the entry into the array.
bool ArrayRecord::InsertItem(int item, char* fName, char* lName, double gpa, char* pnum)
{
int location = 0;
// int item;
while (location < length)
{
if (list[location].id == item) return 0;
else {
list[length].id = item;
list[length].firstName = fName;
list[length].lastName = lName;
list[length].gpa = gpa;
list[length].phonenumber = pnum;
length++;
return 1;
/*for the duplicate id prevention search id first if found then return function immediately otherwise start insertion*/
}
}
}
And here is the call from the header for the above function:
bool InsertItem(int, char*,char*,double,char*);
Now, this is the one really giving me a headache. I have been on a few other forums, trying to get help with this one but I'm confused by the help that was given to me. Modify is to first search for an entry. If the entry is found, the entry is modified. If the entry is not found, then the entry is inserted into the array.
bool ArrayRecord::Modify(int item, char* fName, char* lName, double gpa, char* pnum)
{
int location = 0;
int item; //item has no valid value
while (location < length)
{
//item has no valid value - how do you know it's equal to the id value in the current location?
if (list[location].id == item) return &list[location];
else location++;
}
/*Do a search if it's found then modify the selection but if it's not found then insert a new info*/
}
And here is the call in the header for modify.
bool Modify(int, char*, char*, double, char*);
And here are the errors
Error 1 error C2082: redefinition of formal parameter 'item'
Warning 2 warning C4800: 'StudentRecord *' : forcing value to bool 'true' or 'false' (performance warning)
I need examples with very easy language to understand and hopefully a very detailed explanation of what is going on and what I need to do. I am a beginner and I've been out of practice, so I've forgotten some stuff. I need clarification.
Couple things:
The loop in InsertItem will be executed only once, because location is nullified at the start. If you want to check item id before adding new item, the function body should looks something like this:
for (int location = 0; location < length; ++location)
if (list[location].id == item) return false;
list[length].id = item;
list[length].firstName = fName;
list[length].lastName = lName;
list[length].gpa = gpa;
list[length].phonenumber = pnum;
length++;
return true;
The second function should be different:
for (int location; location < length; ++location) {
if (list[location].id == item) {
// modify an item
list[location].firstName = fName;
list[location].lastName = lName;
list[location].gpa = gpa;
list[location].phonenumber = pnum;
return false;
}
}
// add new item
list[length].id = item;
list[length].firstName = fName;
list[length].lastName = lName;
list[length].gpa = gpa;
list[length].phonenumber = pnum;
length++;
return true;
There also would be good to have a check of maximum list length prior adding a new item to it or use std::vector
"Redefinition of formal parameter" is because you brought item in as an input to the function and then declared a second item with the same name (see the second line into ArrayRecord::Modify vs. the list of parameters supplied to the function). Consider trying to declare such a variable like this:
bool myFunc() {
int item;
int item;
// ...
return 0;
}
This is the same thing as having the variable item declared as a function parameter followed by declaring it as a local variable: you have two definitions of the same variable in the same scope.
Secondly, the warning is telling you that the function ArrayRecord::Modify returns a value of the bool type, but in one of your return statements within this function, you are returning a value which is not a bool type and has no "obvious" translation to the bool type. Specifically, the line if (list[location].id == item) return &list[location]; returns an address reference to a location within the list array (which will never be 0 if all goes well), and so the compiler must decide how to translate &list[location] into a true or false return value.
In response to the commented issues, I suggest fixing the "return value" issue mentioned above, followed by adding a "fallback return" in each function such as return 0; as the last statement in each function. While your code appears to cover all possible paths to the ends of the functions with return statements, the compiler disagrees, and your options are to either provide "default" return values in case everything else fails, or to turn off that particular warning in your compiler. The former is much recommended over the latter.
With the warning about failing to have return values in all code paths, note that
while (a < b) {
if (q)
return 1;
else
return 0;
}
is not guaranteed to return a value. The execution of the code might jump over while (a < b) { ... } when a>=b, and the compiler doesn't see any reason that a<b is a guaranteed starting condition.

c++ how to compare a string to a dynamic structure string using pointers?

I have to code a method of a class in C++ that shows me all the attributes of a class when they give me only the name. For example, I have a class 'Team' with a_name, a_goals and a_points. So when they give me a string with the name, I have to compare it to my dynamic structure and find the team with the same name to show it. I have this code:
void Classificacio::mostrar(string nom) const {
Equip eq;
Node* i=a_inici;
bool trobat=false;
while(!trobat && i!=NULL) {
if(nom.compare(i->a_equip.NomEquip())==0) trobat=true;
else i=i->seg;
}
if(trobat==true) eq=i->a_equip;
cout << eq << endl;
}
NomEquip() is a method that returns the team name.
But it doesn't work. Every time I try to execute it with the debugger, it stops in the line with the if. Any ideas what I'm doing wrong?
EDIT: Wanted to translate it to english but I forgot some things, just copy/pasted it this time.
There is a possibility of crashing in the line:
if (trobat == true) eq=i->a_equip;
Because you check for 'i!=NULLin thewhileloop. One of the terminating conditions of thewhileloop is thati == NULL`.
Assuming the while loop was terminated because i == NULL, your if statement will dereference a NULL pointer which is undefined behavior.
Edit 1:
If it crashes at if (nom.compare(i->a_equip.NomEquip()) == 0), and we know i is valid, it leads that the NomEquip function is a leading culprint.
Change your while loop to:
while (...)
{
std::string nom_equip = i->a_equip.NomEquip();
if (nom == nom_equip)
//...
}
Now place breakpoint at the std::string line and step into the function to trace it.