全局变量的实现 [英] Global variable implementation

查看:61
本文介绍了全局变量的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我写了下面的程序:

文件1:

 的#include<&stdio.h中GT;
INT全球;
无效print_global1(){
        的printf(%P \\ N,&安培;全局);
}

文件2:

 的#include<&stdio.h中GT;
烧焦全球;
无效print_global2(){
        的printf(%P \\ N,&安培;全局);
}

文件3:

 无效print_global1();
无效print_global2();
诠释的main()
{
        print_global1();
        print_global2();        返回0;
}

输出:

  $ ./a.out
0x804a01c
0x804a01c

下面是我的问题:


  • 为什么实施INT全局和字符全球为同一全局变量的链接:

  • 为什么编译器不抱怨(不与 -Wall -Wextra -ansi ...最小的警告)

  • 如何在全局变量的大小管理(int和字符的大小是不同的)

PS:第二个问题是建筑/编译器相关的,所以让我们的GCC或Visual C ++(对于C)与INT大小为32位。

编辑:这是不是C ++但是C A QUESTION

我用gcc版本4.4.1和Ubuntu 9.10的,下面是编辑控制台输出:

  $ LS
global_data1.c global_data2.c global_data.c$ GCC -Wall -Wextra -ansi global_data的* .c
$ ./a.out
0x804a01c
0x804a01c
要么
$ GCC -Wall -Wextra -ansi -c global_data的* .c
$ GCC -Wall -Wextra -ansi global_data *的.o
$ ./a.out
0x804a01c
0x804a01c


解决方案

GCC 没有报告任何错误/警告。但 G ++ 一样。

编辑:

看起来像C允许暂定的定义 的变量。

在您的情况下,无论是全局定义是暂定的,在这种情况下链接器看到的第一个选择。

您的文件2更改为:

 字符全球= 1; //没有更多的试探性的...但明确。

现在,如果你编译像以前一样,在文件1暂定DEF将被忽略。

请两者均由DEF明确的:

  INT全球= 1; // file1中烧焦全球= 1; // file2中

现在既可以忽略不计,我们得到了多个高清错误。

When I write the following program:

file 1:

#include <stdio.h>    
int global;    
void print_global1() {
        printf("%p\n", &global);
}

file 2:

#include <stdio.h>
char global;    
void print_global2() {
        printf("%p\n", &global);
}

file 3:

void print_global1();
void print_global2();
int main()
{
        print_global1();
        print_global2();

        return 0;
}

output:

$ ./a.out
0x804a01c
0x804a01c

Here is my question:

  • Why are the linker implementing "int global" and "char global" as the same global variable:
  • How come the compiler does not complain (not the smallest warning with -Wall -Wextra -ansi ...)
  • How are the size of the global variable managed (the size of int and char are different)

PS: The second question is architecture/compiler related, so lets take the gcc or Visual C++ (for C) with the int size as 32 bits

EDIT: THIS IS NOT A QUESTION FOR C++ BUT for C!

I use gcc version 4.4.1 and on Ubuntu 9.10, Here is the compilation console output:

$ ls
global_data1.c  global_data2.c  global_data.c

$ gcc -Wall -Wextra -ansi global_data*.c
$ ./a.out
0x804a01c
0x804a01c
or 
$ gcc -Wall -Wextra -ansi -c global_data*.c
$ gcc -Wall -Wextra -ansi global_data*.o
$ ./a.out
0x804a01c
0x804a01c

解决方案

gcc does not report any error/warnings. But g++ does.

EDIT:

Looks like C allows tentative definitions for a variable.

In your case both the global definitions are tentative and in that case the first one seen by the linker is chosen.

Change your file2 to:

char global = 1; // no more tentative...but explicit.

Now if you compile like before, the tentative def in file1 will be ignored.

Make both the def explicit by:

int global = 1; // in file1

char global = 1; // in file2

now neither can be ignored and we get the multiple def error.

这篇关于全局变量的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆