[ ] view plain copy
1.
2.
3. int a = 0;// 全局初始化区
4. char *p1; //全局未初始化区
5.
6. int main()
7. {
8. int b; //b在栈区
9. char s[] = "abc"; //栈
10. char *p2; //p2在栈区
11.
12. char *p3 = "123456"; //123456\0在常量区,p3在栈上。
13. static int c =0;//全局(静态)初始化区
14.
15. p1 = (char *)malloc(10);
16. p2 = (char *)malloc(20); //分配得来得10和20字节的区域就在堆区。
17.
18. strcpy(p1, "123456"); //123456\0放在常量区,编译器可能会将它与p3所指向的"123456"优化成一个地方。
19. }
[cpp] view plain copy
1.
2.
3. void *malloc(size_t size);
[cpp] view plain copy
1. int *get_memory(int n)
2. {
3. int *p;
4. p = (int *)malloc(sizeof(int));
5. if(p == NULL)
6. {
7. printf("malloc error\n");
8. return p;
9. }
10.
11. memset(p,0,n*sizeof(int));
12. }
[cpp] view plain copy
#
void free(void *ptr);
[view plain copy ]
free(p);
p = NULL;
char s1[] = "aaaaaaaaaaaaaaa";
char *s2 = "bbbbbbbbbbbbbbbbb";
[cpp] view plain copy
1. #include
2. void main()
3. {
4. char a = 1;
5. char c[] = "1234567890";
6. char *p ="1234567890";
7. a = c[1];
8. a = p[1];
9. return;
10. }
对应的汇编代码
[cpp] view plain copy
1. 0: a = c[1];
2. 00401067 8A 4D F1 mov cl,byte ptr [ebp-0Fh]
3. 0040106A 88 4D FC mov byte ptr [ebp-4],cl
4. 11: a = p[1];
5. 0040106D 8B 55 EC mov edx,dword ptr [ebp-14h]
6. 00401070 8A 42 01 mov al,byte ptr [edx+1]
7. 00401073 88 45 FC mov byte ptr [ebp-4],al