A dialogue between variables and memory space usage

Source: Internet
Author: User
Old Ding-Xiamen (43515273) 4:45:54
# Include <iostream>
# Include <iomanip>
Using namespace STD; void copystr (char * str1, char * str2)
{
For (; * str2! = '\ 0'; str1 ++, str2 ++)
* Str1 = * str2;
* Str1 = '\ 0 ';
} Void main ()
{
Char A [10]; // If you replace this sentence with: char * A = "Ghi";, an error occurs. Why?
Char * B = "ABC ";
Copystr (A, B );
Cout <"A =" <A <Endl;
}
Xiamen yingzhi-Huang shuangquan (962017) 10:33:56
Local variables are all allocated to the stack. For example, if char * A is defined, the stack occupies the size of a pointer.
Char A [10] occupies 10 bytes. Some systems occupy 12 bytes for alignment.
The stack content can be changed, but char * A = "Ghi"; is actually divided into two parts: char * A and "Ghi", where char * A is in the stack, it occupies four bytes, while "Ghi" is allocated in Code And Program The code is in a zone.
So it cannot be changed to char * A = "Ghi ";
Char * B = "ABC ";
A = B; it is positive, because a is in the stack and changes the four bytes of a pointer.
Copy B to a is wrong, because this action uses ABC to overwrite the three Ghi characters, and the three Ghi characters are in the memory of the Code area, which is divided into four types, one is code space, the other is data space (including global variables, local variables (stacks), and heap space)
The code space cannot be changed, but the data space can be changed. Guo zhishun (513367429) 10:41:02
Char A [] = "hello ";
Char A [10] = "hello ";
Where are these two stored? Old Ding-Xiamen (43515273) 10:41:38
Char A [] = "hello ";
Char A [10] = "hello ";
Where are these two stored?
Save in heap Xiamen yingzhi-Huang shuangquan (962017) 10:42:36
Char A [] occupies 6 bytes of space. If it is a local variable, it is allocated to the stack. "Hello" occupies 6 bytes and is hidden in the Code. When the program is running, there is an initialization action that copies the content at the location "hello" to the location char [].
Two copies of space are occupied, one is occupied by the program space, and the other is occupied by the data space, but the compiler automatically adds a copy action in. Old Ding-Xiamen (43515273) 10:44:14
The stack is inseparable from the single chip microcomputer. The single chip microcomputer stack is the stack here, inertial thinking Xiamen yingzhi-Huang shuangquan (962017) 10:44:23
Char A [10] occupies 10 bytes of data space, and "hello" occupies 6 bytes of code space. Guo zhishun (513367429) 10:45:49
Is there a copy? Xiamen yingzhi-Huang shuangquan (962017) 10:47:23
The occupied data space may be stored in "global variables (the size of the global space is fixed after the program is compiled), depending on the method you define ), bureau variable (stack) "two situations, if you need to store in the heap, you need to call malloc to manually allocate memory, and then assign a value to it
Yes, there are also copy actions.
All initialization of variables has a copy behavior. Xiamen yingzhi-Huang shuangquan (962017) 10:48:33
Variable definition. During the process of running the program, the definition and value assignment process, initialization, is to write the definition and value assignment in a line of code. Guo zhishun (513367429) 10:49:00
Well, it's very thorough.
Does this knowledge belong to the compilation principle? Xiamen yingzhi-Huang shuangquan (962017) 10:52:10
Knowledge of some compilation principles, as well as system and hardware architecture

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.