Advertising (This ad goes away for registered users. You can Login or Register)

Serious memory problem

Programming on your favorite platform, for your favorite platform? Post here
Locked
iSWORD
Posts: 170
Joined: Mon Sep 27, 2010 9:50 pm

Serious memory problem

Post by iSWORD »

While I was learning dealing with pointers and memory addresses in C, I created an application that messes with the memory in a for loop

Code: Select all

for (i=0;i<100;i++)
{
        ptr++;
        *ptr = 0
}
And now I have serious problems with my computer.. most of the applications show a memory access violation error.. and when I try to write or read the value of a memory address (with C) I get the same error !!
Advertising
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: Serious memory problem

Post by m0skit0 »

Of course. You're trying to access memory that doesn't belong to your process. Modern computers have memory protection, which means each process can only access his own memory partitions and not others. You're trying to access memory which doesn't belong to your process. And anyway, you didn't specify the initial address for ptr :|

Also the explanation about other programs failing is because you most likely erased some data they had, since you're assinging 0 to all that memory zone (i don't know how what zone it is and how much big because you didn't specify the initial ptr address and its type).
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
iSWORD
Posts: 170
Joined: Mon Sep 27, 2010 9:50 pm

Re: Serious memory problem

Post by iSWORD »

Code: Select all

char c;
char *ptr;

ptr = &c;
JJS
Big Beholder
Posts: 1416
Joined: Mon Sep 27, 2010 2:18 pm
Contact:

Re: Serious memory problem

Post by JJS »

Well, c is a single byte on the stack. So you are getting a pointer to that and then overwrite a big chunk of the stack.

You have to allocate memory first, either static on the stack with

Code: Select all

char c[100];
or dynamic on the heap

Code: Select all

char* c = (char*)malloc(100);

Also you are first incrementing ptr, then assigning it. That means you don't set the first element to 0. Instead you write up to the 101st element. I don't think this is what you intended.


I don't see how this can affect other programs though. Unless you are using a system without memory protection for processes (that is pre-Windows 95 era stuff) this is not possible.
iSWORD
Posts: 170
Joined: Mon Sep 27, 2010 9:50 pm

Re: Serious memory problem

Post by iSWORD »

Hmm.. I know the 101 thing..
BTW I'm using Windows XP SP3..
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: Serious memory problem

Post by m0skit0 »

Then your questions are already answered. I don't see what else has to be added here.

And Windows is not open source, so who knows why other programs fail and how the stack is implemented.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
Locked

Return to “Programming”