An access violation occurred while writing the location

Source: Internet
Author: User

An access violation occurred while writing the location Summary of Causes :

1: An uninitialized pointer was used (no space allocated to the pointer)

2: Used a pointer that has been removed

3.char str[]= "ABCD". The storage space of this array is opened up in the stack (an array is opened in the stack?). )

Char *str= "ABCD". Str points to a static store, and "ABCD" is in a constant area, and the pointer Str simply points to that position (only a pointer memory is opened in the stack). ), then these values cannot be changed.

In the above array, it is important to note that the characters are copied to the elements of the array. Then it is possible to be arbitrarily altered.

Cases:

char* str = "ABCD"; Char str[] = "ABCD"; capable of resolving strrev (str); char * Strrev (char str[]) {int J, i;for (i = 0, j = strlen (str)-1; i < J; i++, J- -) {char tmp = str[i];str[i] = str[j];//write position conflict str[j] = tmp;} return str;}

Analysis:

Program code Area:

1//main.cpp 2 int a=0;    Global Initialization Zone 3 char *p1; Global Uninitialized Zone 4 main () 5 {6     int b;                  Stack 7     char s[]= "abc";  Stack 8     char *p2;           Stack 9     char *p3= "123456";   123456\0 in the constant area, p3 on the stack. Ten     static int c=0;     Global (static) initialization Zone one     p1 = (char*) malloc (n),     p2 = (char*) malloc;   Areas that are allocated 10 and 20 bytes are in the heap area.     strcpy (P1, "123456");   123456\0 is placed in a constant area, the compiler may optimize it with the P3 to "123456" as a place. 14}

Comparison of access efficiency

Char s1[]= "AAAAAAAAAAAAAAA";
Char *s2= "BBBBBBBBBBBBBBBBB";
AAAAAAAAAAA is assigned at execution time, while BBBBBBBBBBB is determined at compile time, but in subsequent accesses, the array on the stack is faster than the string the pointer points to (such as a heap).


from:http://blog.csdn.net/pipisorry/article/details/37055183

Ref: using recursion to implement string inversion http://blog.csdn.net/andysun1986/article/details/6941230

An access violation occurred while writing the location

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.