Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Can someone please explain to me why an exception is thrown in the below code for the char array but not for the int array?

Thanks for any information.

C#
void swap(int &i, int& j)
{
    int t = i;
    i = j;             // NO PROBLEM HERE
    j = t;
}
void swap(char &i, char &j)
{
    char  t = i;
    i = j;              // EXCEPTION THROWN !  ACCESS VIOLATION !
    j = t;
}

C#
int main()
{

    char * temp = "abc";
    int ints[3] = {1, 2 ,3 };
    std::cout << ints[0] << " " << ints[1] << endl;
    swap(ints[0], ints[1]);
    std::cout << ints[0] << " " << ints[1] << endl;;

    std::cout << temp[0] << " " << temp[1] << endl;
    swap(temp[0], temp[1]);
    std::cout << temp[0] << " " << temp[1] << endl;
    return 0;
}
Posted

The problem is in the char* temp = "abc" and in the imperfect type system of C++.
int ints[3] = {1, 2, 3}; 

is actually an array of 3 integers, whose initial values are 1,2,3. The int[] is not const and the value can be changed (as you do when assigning i=j; j=t; in your swap.

char* temp="abc",

is actually a pointer to a char, that is initializad to point to the first character of the string literal "abc" that is treated as a constant.
(yes: its type should be const char* but is also convertible to char* to retain C backward compatibility).
Your i=j is trying to write into a read-only memory page.

Try
char temp[4] = {'a', 'b', 'c', '\0' };

Should work as with the int do.
 
Share this answer
 
Comments
minkowski 5-Mar-11 14:58pm    
Thanks for your comment. Now that you wrote "const char *" I can see why it would fail. But would'nt the compiler pick up on that since it is really "const" ? Isn't the compiler strict enough?
Sergey Alexandrovich Kryukov 5-Mar-11 18:42pm    
Good question. I think you can rightfully blame the compiler. At the moment, I can't tell about the standard, would be good to check it. But it's known that the standard and real-life C++ compilers are different things, if several aspects. Too bad...
--SA
minkowski 5-Mar-11 15:15pm    
it will also work with

char temp[] = "abc";
Emilio Garavaglia 6-Mar-11 3:52am    
Probably yes, but the compiler may have a subtle role. That's not obvious for all of them.
minkowski 6-Mar-11 5:11am    
Ha ha, I know it works for a char array as interviewers like to ask to reverse a char array as a warm up question.

=O)
Because "abc" is a constant string...

Which makes temp a pointer to a read-only string.

You get an access violation because you are trying to write to read-only memory!

Try it with:
C#
char temp[] = { 'a', 'b', 'c', '\0'};
And it will work again.
 
Share this answer
 
Comments
minkowski 5-Mar-11 14:58pm    
Thanks for your input. I can understand why it wouldn't work if it is really a "const char *". Cheers!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900