Access Violation

Thread Solved

Join Date: Oct 2006
Posts: 58
Reputation: amishosh is an unknown quantity at this point 
Solved Threads: 0
amishosh amishosh is offline Offline
Junior Poster in Training

Access Violation

 
0
  #1
Aug 19th, 2007
Consider the following code:

#include<stdio.h>
#define SET_BIT(buf,n,val)buf[n]=val
int main()
{
char ch='D';
char *name="Yankee Duddle";
int i=5;
SET_BIT(name,i,ch);
printf("%s",name);
}

When compiling with Borland C it worked fine. However when I tried cimpiling it with VS 2005 I got the following error:
"Unhandled exception at 0x00413529 in Learning C.exe: 0xC0000005: Access violation writing location 0x00415645."

When I changed the code to:

char name[]="Yankee Duddle";
Instead of
char *name="Yankee Duddle";

It worked fine. I found this resolution in a forum.

But based on what I learnt in C I don't understand why it did not work in VS. And why by making it an array rather than a pointer it made a difference?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Access Violation

 
2
  #2
Aug 19th, 2007
In case char *name="Yankee Duddle"; string literal turns into an unnamed, static array of characters, and this unnamed array may be stored in read-only memory, and which therefore cannot necessarily be modified. Trying to modify it is undefined behaviour.
Whereas in case of char name[]="Yankee Duddle"; you can modify the contents safely.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Access Violation

 
1
  #3
Aug 19th, 2007
Originally Posted by amishosh View Post
When compiling with Borland C it worked fine. However when I tried cimpiling it with VS 2005 I got the following error:
"Unhandled exception at 0x00413529 in Learning C.exe: 0xC0000005: Access violation writing location 0x00415645."

But based on what I learnt in C I don't understand why it did not work in VS. And why by making it an array rather than a pointer it made a difference?
char *name="Yankee Duddle"; is a constant, it can not be modified.
char name[]="Yankee Duddle"; the elements can be modified.
calling the SET_BIT in main you are trying to modify the value in char *name.

[EDIT]: Sorry, SPS your posted while I was writing this. Making this post a repetition.
Last edited by Aia; Aug 19th, 2007 at 1:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 58
Reputation: amishosh is an unknown quantity at this point 
Solved Threads: 0
amishosh amishosh is offline Offline
Junior Poster in Training

Re: Access Violation

 
0
  #4
Aug 19th, 2007
Why do the two compilers behave differently?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 58
Reputation: amishosh is an unknown quantity at this point 
Solved Threads: 0
amishosh amishosh is offline Offline
Junior Poster in Training

Re: Access Violation

 
0
  #5
Aug 19th, 2007
I think I found the answer (if anyone else has this problem):
http://msdn2.microsoft.com/en-us/lib...yy(vs.71).aspx
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Access Violation

 
0
  #6
Aug 19th, 2007
> Why do the two compilers behave differently?
Because your code invokes undefined behaviour.
The assumption you made was that string constants can be modified, and they cannot.

If your code is correct, then you'll get the same answer with every compiler.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC