CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2004
    Posts
    4

    Lightbulb 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;
    }

  2. #2
    Join Date
    Jun 2004
    Posts
    1,352
    Quote 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++

  3. #3
    Join Date
    Jul 2004
    Posts
    4
    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!

  4. #4
    Join Date
    Jun 2004
    Posts
    1,352
    int main(int argc, char* argv[])
    {

    char *str2[] = "123456789";

    foo( &str2);

    return 0;
    }

    ... try that!!!


    char *str2[] = "123456789"; // I placed * before str2

  5. #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

  6. #6
    Join Date
    Jul 2004
    Posts
    4
    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

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449
    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured