Jump to content
  • Advertisement
Sign in to follow this  
ordered_disorder

Access Violations and Memory

This topic is 4781 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm really confused and I hope one you more experienced programmers can sort me out. I have the follow c++ source ->
void main()
{
	unsigned int *ptr_to_mem = (unsigned int *)0x00000000; // could be anything for all I care, 0xXXXXXXXX
	*ptr_to_mem = 0xFFFFFFFF;
}

As you can see I'm trying to change the value of the memory at this location. I'm doing this to teach me more about the OS, winDBg(visual studio's debugger) and how memory works in windows, all for my future mastery of assembly programming. I keep getting access violations 0xC0000005 which is perplexing because I thought each program was given its own 4 GB address space, because windows runs in protected mode. When I debug the program I see at the address 0x0000000 there is just uninitialized data, so I don't know why I would be getting the access violation, because from my perspective it's just free memory! This makes me wonder how the compiler sets up memory to be used when I dynamically allocate it. This is what I get for learning assembly, a lot of questions.

Share this post


Link to post
Share on other sites
Advertisement
On windows the memory addresses from the range 0x0000 0000 to 0x0000 ffff are a protected range that your program cannot write to without generating an access violation. Even if you had a pointer to another location, a program can only legitimately write to a memory location that has been committed, which may not be true of a given random memory address.

Share this post


Link to post
Share on other sites
Just because your program is given a 4GB address space, it doesn't mean it is free to use whichever parts of that it chooses. The top 2GB are reserved for the OS, and the lower 2GB are only made usable when you request memory from the OS. Moreover, the bottom 64(?)KB will never be allocated to you, in order that writes to NULL pointers are caught.

Share this post


Link to post
Share on other sites
This code

unsigned int *ptr_to_mem = (unsigned int *)0x00000000;
*ptr_to_mem = 0xFFFFFFFF;

is akin to

unsigned int *ptr_to_mem = NULL;
*ptr_to_mem = 0xFFFFFFFF;

that means that ptr_to_mem doesn't point to valid memory, so attempting to dereference the pointer in order to write a value to the space it points to, throws an access violation exception.

Share this post


Link to post
Share on other sites
nice observation there LessBread, didn't even realize. I get get an access violation at pretty much any memory address I try to write and read from, even if the memory address > 0x0000FFFF, I guess I have to go through the operating system to get access to my program's memory, i.e. by using functions defined in kernal32.lib

Share this post


Link to post
Share on other sites
Quote:
Original post by ordered_disorder
nice observation there LessBread, didn't even realize. I get get an access violation at pretty much any memory address I try to write and read from, even if the memory address > 0x0000FFFF, I guess I have to go through the operating system to get access to my program's memory, i.e. by using functions defined in kernal32.lib


Basically, just stick to the memory you've new'ed or malloc'ed. Everything else can give you an access violation (Occasionally it won't, but only because you accidentally hit somewhere within a page that's already been allocated to you)

Share this post


Link to post
Share on other sites
Different parts of memory have different permissions. Some you can't touch at all, some you can only read from. Some parts you can run code in and some parts you can't, etc, etc. An access violation means that you've tried to do something that is not allowed for that particular piece of memory.

You can have the OS allocate memory for you and once that's done you can usually request that it change the permissions around.

(and windbg is not Visual Studio's debugger. The debugger built into VS and windbg are two unrelated debuggers).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!