Connecting Tech Pros Worldwide Help | Site Map

exception : 0xC0000005: Access Violation

  #1  
Old November 7th, 2005, 09:55 AM
batista
Guest
 
Posts: n/a
Hi,

I am getting the following exception

First-chance exception in TestMansoor.exe: 0xC0000005: Access Violation

When i run the following code

1 int main(void)
2 {
3 char* str1 = "MA";
4 char s = str1[0];
5 str1[0] = 'r';
6 return 0;
7 }

i'm getting exception on line 5. Why?

Bye.

  #2  
Old November 7th, 2005, 09:55 AM
Sandeep
Guest
 
Posts: n/a

re: exception : 0xC0000005: Access Violation



batista wrote:[color=blue]
> Hi,
>
> I am getting the following exception
>
> First-chance exception in TestMansoor.exe: 0xC0000005: Access Violation
>
> When i run the following code
>
> 1 int main(void)
> 2 {
> 3 char* str1 = "MA";
> 4 char s = str1[0];
> 5 str1[0] = 'r';
> 6 return 0;
> 7 }
>
> i'm getting exception on line 5. Why?
>[/color]

str is a constant string , you can only access it. If you want to
change the contents of str, you will either have to declare it as an
array of allocate memory for it.

  #3  
Old November 7th, 2005, 10:05 AM
Kyle
Guest
 
Posts: n/a

re: exception : 0xC0000005: Access Violation


batista wrote:[color=blue]
> Hi,
>
> I am getting the following exception
>
> First-chance exception in TestMansoor.exe: 0xC0000005: Access Violation
>
> When i run the following code
>
> 1 int main(void)
> 2 {
> 3 char* str1 = "MA";
> 4 char s = str1[0];
> 5 str1[0] = 'r';
> 6 return 0;
> 7 }
>
> i'm getting exception on line 5. Why?
>
> Bye.
>[/color]

becouse "MA" is a string iteral, and as such cant be modified, even
though its possilble to get a non const pointer to it for some bizzare
reason

your code is more less equivalent of

((char*)"MA")[0] = 'r';

if you want to get a copy of "MA" what you want to do is
char str1[] = "MA";
now str1[0] is modifiable
  #4  
Old November 7th, 2005, 06:15 PM
Gianni Mariani
Guest
 
Posts: n/a

re: exception : 0xC0000005: Access Violation


batista wrote:[color=blue]
> Hi,
>
> I am getting the following exception
>
> First-chance exception in TestMansoor.exe: 0xC0000005: Access Violation
>
> When i run the following code
>
> 1 int main(void)
> 2 {
> 3 char* str1 = "MA";
> 4 char s = str1[0];
> 5 str1[0] = 'r';
> 6 return 0;
> 7 }
>
> i'm getting exception on line 5. Why?[/color]


Line 3 should read:

const char* str1 = "MA";

It is specifically allowed by the standard as your original because of
the volume of legacy code that would otherwise break. (IMHO a bad move
by the standards comittee.)

You can however rewrite line 3 like:

char str1[] = "MA";

.... now you're free to change the three characters that is str1.


  #5  
Old November 8th, 2005, 03:35 AM
Greg Comeau
Guest
 
Posts: n/a

re: exception : 0xC0000005: Access Violation


In article <1131356517.625797.72670@g49g2000cwa.googlegroups. com>,
batista <saadtheleon@gmail.com> wrote:[color=blue]
>Hi,
>
>I am getting the following exception
>
>First-chance exception in TestMansoor.exe: 0xC0000005: Access Violation
>
>When i run the following code
>
>1 int main(void)
>2 {
>3 char* str1 = "MA";
>4 char s = str1[0];
>5 str1[0] = 'r';
>6 return 0;
>7 }
>
>i'm getting exception on line 5. Why?[/color]

Check out http://www.comeaucomputing.com/techtalk/#stringliteral
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
0xC0000005 Access violation on a CreateEvent() call =?Utf-8?B?c29jYXRvYQ==?= answers 2 June 27th, 2008 08:59 PM
First-chance exception in data_generation.exe: 0xC0000005: Access Violation. ritvik answers 1 May 1st, 2007 05:14 AM
0xC0000005 Access Violation Help? Umar Alam answers 1 April 11th, 2006 11:25 AM
0xC0000005: Access violation reading location 0x513112f4 bhreddy answers 2 March 27th, 2006 11:45 AM