0xC0000005: Access violation writing location 0x00000004.

Started by
14 comments, last by GameDev.net 17 years, 9 months ago
Oh yeah, BTW, usually a function returns 0 for success, and any other value to denote an error (which you can conveniently put into an enum for readability).
Advertisement
Quote:Original post by Prototype
Oh yeah, BTW, usually a function returns 0 for success, and any other value to denote an error (which you can conveniently put into an enum for readability).


Thanks thanks :)
It is perhaps worth noting the following point.

If you get an error such as: "Access violation writing location 0x00000000."
Then it usually means that you have dereferenced a NULL pointer.

However you got: "Access violation writing location 0x00000004."
When you get an error like this, with a small non-zero value, then
it often (but not always) means that your "this" pointer is NULL.

In this case you had "loaded = true;". Notice that loaded is exactly 4 bytes
into the class Sprite as it follows a pointer (4 bytes) in the declaration.

So given the error message the first thing to do was to check the "this"
pointer in the debugger, or just read the code that called the function
and check whether the pointer could be NULL.

Martin
Hi everyone, I think I have a similar problem with VS.NET 2003. I get
Unhandled exception at 0x0041821a in test.exe: 0xC0000005: Access violation writing location 0x004436ec.

message with this code:
#include <conio.h>#include <iostream>using namespace std;void revert(char *str){  char *last_ch = str;     while (*++last_ch)    //*pluspluslast_ch      ;                       --last_ch;              while (str < last_ch)    {                      		    char tmp;    tmp = *str;    *str++ = *last_ch;	//buggy line (*strplusplus)    *last_ch-- = tmp;  }	}int main(){  char *zaq = "abcdefgh";  cout<<zaq<<endl;  revert(zaq);	  cout<<endl<<zaq<<endl;    getch();  return 0;}

It comes from "Pointers on C" by Kenneth A. Reek (chapter 6, ex. 3). The function should put chars in reverse order in a given char array without using indices and local temp arrays (by using pointers only). I don't know what is wrong with this code - it comes from the spoiler at the end of the book :(
Your problem is:

char *zaq = "abcdefgh";

the fix:

char zaq[] = "abcdefgh";

zaq was pointing pointing to a memory address inside the text of your program, which cannot be changed.
thx alot :) my mistake in main

This topic is closed to new replies.

Advertisement