943,747 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 8220
  • C RSS
Aug 19th, 2007
0

Access Violation

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
amishosh is offline Offline
59 posts
since Oct 2006
Aug 19th, 2007
2

Re: Access Violation

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.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 19th, 2007
2

Re: Access Violation

Click to Expand / Collapse  Quote originally posted by amishosh ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Aug 19th, 2007
0

Re: Access Violation

Why do the two compilers behave differently?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
amishosh is offline Offline
59 posts
since Oct 2006
Aug 19th, 2007
0

Re: Access Violation

I think I found the answer (if anyone else has this problem):
http://msdn2.microsoft.com/en-us/lib...yy(vs.71).aspx
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
amishosh is offline Offline
59 posts
since Oct 2006
Aug 19th, 2007
0

Re: Access Violation

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Read a Line of Text from the User
Next Thread in C Forum Timeline: Can i program an usb port?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC