今天面试的一道题,大家给分析分析

dongliang_nuaa 2008-11-21 01:09:25
一个全局变量数组比如 int a[10000]=0;
初始化和未初始化有什么区别?
...全文
448 40 打赏 收藏 转发到动态 举报
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuxipangde01 2008-11-23
  • 打赏
  • 举报
回复
学习了!
newerC 2008-11-23
  • 打赏
  • 举报
回复
mark,
up jznhljg !!!
malone1 2008-11-23
  • 打赏
  • 举报
回复
回帖是一种美德!传说每天回帖即可获得 10 分可用分
zhoujinming1983 2008-11-23
  • 打赏
  • 举报
回复
学习,学习。
qq675927952 2008-11-23
  • 打赏
  • 举报
回复
up
zedzhao 2008-11-23
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 jznhljg 的回复:]
C/C++ code
lee@Lee:~/MyProject/temp$ cat ./test.c && size ./test
#include <stdio.h>

int array[10000] ;
int main(int argc, char* argv[])
{
printf("Array location:%p\n", array);
printf("array[0] = %d\n", array[0]);

return 0;
}
text data bss dec hex filename
951 264 40032 41247 a11f ./test





C/C++ code
l…
[/Quote]

不太理解, 那为什么初始化和没初始化的生成的exe文件大小不一样??
jznhljg 2008-11-23
  • 打赏
  • 举报
回复

Data segment
From Wikipedia, the free encyclopedia
Jump to: navigation, search

A data segment is one of the sections of a program in an object file or in memory, which contains the global variables that are initialized by the programmer. It has a fixed size, since all of the data in this section is set by the programmer before the program is loaded. However, it is not read-only, since the values of the variables can be altered at runtime. This is in contrast to the Rodata (constant, read-only data) section, as well as the code segment (also known as text segment).

In the PC architecture there are four basic read-write memory regions in a program: Stack, Data, BSS, and Heap. Sometimes the data, BSS, and heap areas are collectively referred to as the "data segment".

1. The Data segment contains constants used by the program that are not initialized to zero. For instance the string defined by char s[] = "hello world"; in C would exist in the data part.
2. The BSS segment starts at the end of the data segment and contains all global variables that are initialized to zero. For instance a variable declared static int i; would be contained in the BSS segment.
3. The heap area begins at the end of the data segment and grows to larger addresses from there. The Heap area is managed by malloc, realloc, and free, which use the brk and sbrk system calls to adjust its size. The Heap area is shared by all shared libraries and dynamic load modules in a process.
4. The stack is a LIFO structure, typically located in the higher parts of memory. It usually "grows down" with every register, immediate value or stack frame being added to it. A stack frame consists at minimum of a return address.
jznhljg 2008-11-23
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <malloc.h> /* for using "ptrdiff_t" which defined in GLIBC */
#include <unistd.h>
#include <alloca.h> /* just for set a example */

extern void afunc(void);/* show the stack increament */

int bss_var; /* auto initialize to 0, it should be arranged to BSS */
int data_var = 42; /* initialize to NON-ZERO, it should be arranged to data segment. */

int main(int argc, char **argv) /* the arguments has no use */
{
char *p, *b , *nb;
int temp_var1;
int temp_var2;
int temp_array[100000];
int temp_var3;

printf("Text location :\n");
printf("\tAddress of main: %p\n", main);
printf("\tAddress of afunc: %p\n", afunc);

printf("In main Function:\n");
printf("\tAddress of temp_var1: %p\n", &temp_var1);
printf("\tAddress of temp_var2: %p\n", &temp_var2);
printf("\tAddress of temp_array: %p\n", temp_array);
printf("\tAddress of temp_array[1]: %p\n", &temp_array[1]);
printf("\tAddress of temp_array[99999]: %p\n", &temp_array[99999]);
printf("\tAddress of temp_var3: %p\n", &temp_var3);

printf("Stack location: \n");
afunc();

p = (char *) alloca(32);
if (p!= NULL)
{
printf("\tStart of alloca()'ed array: %p\n", p);
printf("\tEnd of alloca()'ed array: %p\n", p+31);
}//if
printf("Data location: \n");
printf("\tAddress of data_var: %p\n", &data_var);

printf("BSS location: \n");
printf("\tAddress of bss_var: %p\n", &bss_var);

b = sbrk((ptrdiff_t) 32); /* increament the address space. */
nb = sbrk((ptrdiff_t) 0);
printf("Heap location:\n");
printf("\tInitial end of heap: %p\n", b);
printf("\tNew end of heap: %p\n", nb);

b = sbrk((ptrdiff_t) -16); /* contract the arrdress space. */
nb = sbrk((ptrdiff_t) 0);
printf("\tFinal end of heap: %p\n", nb);
return 0;
}//main()


void afunc(void)
{
static int level = 0; /* recursion level */
int stack_var; /* auto variable on stack */

if (++level == 3)
{
return;
}//if

printf("\tStack level %d: address of stack_var: %p\n", level, &stack_var);
afunc();
}//afunc()

-------------------------------------------------------------------------
lee@Lee:~/MyProject/utility$ ./print_address
Text location :
Address of main: 0x8048484
Address of afunc: 0x80486b9
In main Function:
Address of temp_var1: 0xbfc41990
Address of temp_var2: 0xbfc4198c
Address of temp_array: 0xbfbdff08
Address of temp_array[1]: 0xbfbdff0c
Address of temp_array[99999]: 0xbfc41984
Address of temp_var3: 0xbfc41988
Stack location:
Stack level 1: address of stack_var: 0xbfbdfee4
Stack level 2: address of stack_var: 0xbfbdfeb4
Start of alloca()'ed array: 0xbfbdfed0
End of alloca()'ed array: 0xbfbdfeef
Data location:
Address of data_var: 0x804a020
BSS location:
Address of bss_var: 0x804a030
Heap location:
Initial end of heap: 0x87ae000
New end of heap: 0x87ae020
Final end of heap: 0x87ae010


程序栈 高地址

| 堆栈段
| 向下增长的栈

....

地址空间里可能有"洞"

....

| 向上增长的堆
|
|

------------
BSS:用0填充的变量 数据段
--------------
全局变量和静态变量 低地址

----------------

可执行代码 代码段

------------------------------------

ps.:对照着打印的地址看...

zedzhao 2008-11-23
  • 打赏
  • 举报
回复
楼上的解释不错哦
jznhljg 2008-11-23
  • 打赏
  • 举报
回复

lee@Lee:~/MyProject/temp$ cat ./test.c && size ./test
#include <stdio.h>

int array[10000] ;
int main(int argc, char* argv[])
{
printf("Array location:%p\n", array);
printf("array[0] = %d\n", array[0]);

return 0;
}
text data bss dec hex filename
951 264 40032 41247 a11f ./test



lee@Lee:~/MyProject/temp$ cat ./test.c && size ./test

/* 初始化BSS段也是在BSS段中。 大小还是一样 */
#include <stdio.h>

int array[10000] = {0};
int main(int argc, char* argv[])
{
printf("Array location:%p\n", array);
printf("array[0] = %d\n", array[0]);

return 0;
}
text data bss dec hex filename
951 264 40032 41247 a11f ./test



lee@Lee:~/MyProject/temp$ cat ./test.c && size ./test

/* 把数组改成10。 BSS段大小变化 */
#include <stdio.h>

int array[10] ;
int main(int argc, char* argv[])
{
printf("Array location:%p\n", array);
printf("array[0] = %d\n", array[0]);

return 0;
}
text data bss dec hex filename
951 264 72 1287 507 ./test


tunnel115 2008-11-22
  • 打赏
  • 举报
回复
up
yc406740872 2008-11-22
  • 打赏
  • 举报
回复
明白了
reniuliu 2008-11-22
  • 打赏
  • 举报
回复
如果初始化了,编译器将a填入data段,同时在exe文件中分配空间并用初始值填写,程序运行之初
data段直接拷贝进入内存。

如果没有初始化,编译器及仅仅将a的大小计入bbs段的大小,并不再exe中分配空间,程序运行之初,
按照bbs记录的大小一次性分配bbs段,只有当程序用到a时,再才在bbs段中给出其空间。

萧霖 2008-11-22
  • 打赏
  • 举报
回复
跟踪一下不就知道了
kuxiaohua 2008-11-22
  • 打赏
  • 举报
回复
你可以用一下代码试一下看看有何不同:
//初始化的
#include <stdio.h>
int a[10]={0};
void main()
{
printf("%d\n",sizeof(a));
for(int i=0;i<10;i++)
printf("%d",a[i]);
printf("\n");

}
//未初始化的
#include <stdio.h>
int a[10];
void main()
{
printf("%d\n",sizeof(a));
for(int i=0;i<10;i++)
printf("%d",a[i]);
printf("\n");

}
wangkangsoldier 2008-11-21
  • 打赏
  • 举报
回复
同意楼上的 这些知识可以参考《c++ primer》
sw1024 2008-11-21
  • 打赏
  • 举报
回复
作为一个良好的习惯,对变量进行合理的初始化是必不可少的。编译器不通,造成的结果有可能不同,所以还是自己初始化比较好
OenAuth.Core 2008-11-21
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ztz0223 的回复:]
引用楼主 dongliang_nuaa 的帖子:
一个全局变量数组比如 int a[10000]=0;
初始化和未初始化有什么区别?

肯定是有区别的

C/C++ codeint a[10000]=0;



表示用NULL作为地址赋值给a,那么这个过程是错误的,编译通不过的

不显式赋值相当于

C/C++ codeint a[10000]={0};




就是把所有数值对象的值赋值为0
[/Quote]

占一个
deerwin1986 2008-11-21
  • 打赏
  • 举报
回复
没区别啊貌似
向良玉 2008-11-21
  • 打赏
  • 举报
回复
若果初始化则为初始化的值,否则默认为0
加载更多回复(20)

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧