C language refresher little sense

Read a news, saying that the future of C language will gradually renaissance with the development of the Internet of Things. Although the C language one that feels very old very original, and it seems everyone can Tucao Hemopurification, but no doubt it is the originator of many of the language, the time to revisit the C language knowledge can be better with the electronic secondary yuan (0- 1 in the world) to communicate

It stems from a hot hot hot hot hot [Speaking]

Hot hot hot [] [] 0xcccccccc common in uninitialized pointer, namely a "field guide", the compiler will all uninitialized pointer to point to a specific memory address 0xcccccccc, therefore commissioning see 0xcccccccc we must consider whether there is a pointer uninitialized situation.

C language, a pointer is defined, the pointer points to where it is necessary for the provision.

Char type definition of such a pointer, which points to a predetermined memory array of characters "1234" in the first element of '1' is located.

char *hhh = "1234";

From the Debug window VS compiler we can observe:

hhh char type pointer points to a memory address 0x01006b30 place, by dereferencing * hhh operations can really see the char type pointer points to an array of characters hhh "1234," the first element "1."

By VS menu bar -> Debug -> Windows -> Memory -> Memory 1 (chosen at random memory 1 to 4 memory window can be, multiple windows just to facilitate comparison), we can see:

Indeed, we did not lie automatic window, starting from the 4 bytes does 0x01006B30 stored character '1', '2', '3', '4'

At this point we can make the brain following screen:

Let's try this statement:

*hhh = "afg";

VS The results show:

 how to explain?

Some students have doubts if :( * hhh as a whole) the first sentence is the "whole" is defined and initialized, the second sentence is to modify the "whole", why the error?

We actually need to understand the true meaning of these two statements.

The first statement defines a char type pointer, and the pointer to a predetermined character array "1234" first element address '1' in the memory

After the second statement, the assignment symbol (that is, equal sign) is an expression of the left already, that hhh pointer dereference, has shown that memory 0x1006b30 at the store is '1', this expression can not give assignments the

We try the following statement, re-compile and run the code:

hhh = "afg";

After running second statement:

At this time, starting at 0x00436b30 4 bytes still exists '1', '2', '3', '4'

此时可以脑补出这个图:

(不要误以为这里是修改了字符串或者是复制了字符串,其实不过是指针的指向发生了变化而已)

(右图中没有画出0x01006b30处开始的4个字节,实际上该内存区域(0x01006b30开始的4个字节)依然为‘1’,‘2’,‘3’,‘4’)

(左图中没有画出0x00436bd4处开始的3个字节,实际上该内存区域(0x00436bd4开始的3个字节)依然为‘a’,‘f’,‘g’)

 

 

 两图中均有“1234”及“afg”,因为字符串字面量具有静态存储器,并不是说不需要的话就会自动被从内存空间中删除。

 

发布了133 篇原创文章 · 获赞 129 · 访问量 33万+

Guess you like

Origin blog.csdn.net/wofreeo/article/details/103430581