|
-
July 29th, 2004, 10:47 PM
#1
Why the Access violation here ?
Hi all,
why I am getting "access violation" in line 1 below in foo(...)?
void foo( char* str )
{
if( str != NULL && strlen(str) > 0 )
{
str[0] = '9'; // line 1
}
}
int main(int argc, char* argv[])
{
char* str2 = "123456789";
foo( str2);
return 0;
}
-
July 29th, 2004, 10:55 PM
#2
 Originally Posted by Dhafir
Hi all,
why I am getting "access violation" in line 1 below in foo(...)?
void foo( char* str )
{
if( str != NULL && strlen(str) > 0 )
{
str[0] = '9'; // line 1
}
}
int main(int argc, char* argv[])
{
char* str2 = "123456789";
foo( str2);
return 0;
}
void foo( char* str )
{
if( str != NULL && strlen(str) > 0 )
{
str[0] = '9'; // line 1
}
}
int main(int argc, char* argv[])
{
char str2[] = "123456789";
foo( &str2);
return 0;
}
... try that!!!
.... btw this is a 'C' question not VC++
-
July 29th, 2004, 11:58 PM
#3
Thanks ADSOFT, VC++ gives me :
error C2664: cannot convert parameter 1 from 'char (*)[10]' to 'char *'
However, I am not looking for a solution, I am asking "why" I am getting "access violation" in my example?
The answer is a bit tricky!
-
July 30th, 2004, 12:08 AM
#4
int main(int argc, char* argv[])
{
char *str2[] = "123456789";
foo( &str2);
return 0;
}
... try that!!!
char *str2[] = "123456789"; // I placed * before str2
-
July 30th, 2004, 12:41 AM
#5
When u declare,
char* str2 = "123456789";
compiler creates a character pointer (str2) and initialized to point to a constant unnamed character array "123456789".
Attempting to modify the const array is causing the problem.
you may try the following,
char str2[] = "123456789";
but you should not attempt to assign some than 9 characters(since the initial size of ur array is 9)
Sreedharan
-
August 1st, 2004, 03:08 PM
#6
SreeDharan.. you got it..
It is the "successful" assignement of a const char* ( like "123456789") to a non const char* that is confusing in that piece of code. Normally the compiler would pick this up in other cases and throw a compilation error, for example:
std::string str1("123456789");
char* str2 = str1.c_str(); // error C2440:
compiling...
error C2440: 'initializing' : cannot convert from 'const char *' to 'char *'
the compiler will throw a C2440 at you and you would know, however it doesn't do the same for:
char* = "123456789"; // compiles successfully
Thanks to ADSOFT & SreeDharan
Dhafir
-
August 1st, 2004, 03:47 PM
#7
 Originally Posted by Dhafir
Hi all,
why I am getting "access violation" in line 1 below in foo(...)?
Just to add, it is undefined behavior to change a string-literal. Your program may not have necessarily crashed, depending on the compiler.
A string-literal:
Code:
char *p = "string";
p[0] = 'x';
This crashes in VC++ 6.0, but I believe that VC++ 5.0 didn't crash when you attempt to do this. That is what is meant by "undefined behavior" -- anything can happen.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|