Access Violation

Started by
4 comments, last by frob 8 years, 4 months ago

Hello dear users! I have a problem with my code, and I don't really know where to find it, and what did I do wrong. It's a simple code, with a structure, a linked list, and I want to read a txt into my list, but it always stops running, and shuts down with error. I use CodeBlocks, there are no errors or warnings, but it says this: Process terminated with status -1073741819 (or sometimes -255). Here is the problemic part:
typedef struct jatekos {
int mezszam;
char nev[10+1];
int loves;
int gol;
int perc;
int lap;
struct jatekos *kov;
} jatekos ;

void add( jatekos *head, int d, char *s) /function to fill the list from the back/
{
jatekos *uj=(jatekos*)malloc(si zeof(jatekos));
jatekos *akt=head;
while(akt->kov!=NULL) /I think the problem is somewhere here/
akt=akt->kov;
uj->mezszam=d;
strcpy(uj->nev, s);
uj->kov=NULL;
akt->kov=uj;
}

I always put it in Visual Studio i got this error: "Unhandled exception at 0x010E146E in CA.exe: 0x00005: Access violation reading location xy." And it says there are errors with reading characters of string.
I really hope, that you can help me with this, because i tried to solve it in many ways, and yet nothing happened. Thank you!

Advertisement

How do you add one the first time? (ie head == NULL, so "akt->kov" doesn't exist).

Another thing to check is whether strlen(s) <= 10.

Note that it's a heck faster if you pass the last element into the function instead of the head (eliminating the search).

Also return the address of the newly allocated structure to the caller, so it can store it as new "last element".

You should be able to find the precise problem in a debugger. It's worth finding out how such a thing works if you don't know.

I came here because of the C# tag and was completely betrayed...

Sorry for that:(:D
By the way I solved it, thanks for the help!

Don't delete your original post and mark it solved. Your question could have been useful for someone else. Now this thread is useless noise.

Instead, leave it there and describe how you solved it.

As mentioned, please do not vandalize your posts once you find the solution.

Instead please post what your solution was for those who come after you.

Giving due credit, Swiftcoder reverted the body of the post, I provided a better title than "solved" since that isn't automatically recovered.

This topic is closed to new replies.

Advertisement