Globals如何处理C. [英] How Globals is handle in C.

查看:94
本文介绍了Globals如何处理C.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C中处理Globals,因为全局变量可以通过

在该程序中的任何函数中获取。


它们是否存储在堆中,因此它们是他们可以在c中处理它们是怎样的。

解决方案

Umesh写道:< blockquote class =post_quotes>如何在C中处理全局变量,因为该程序中的任何函数都可以访问全局变量。


我假设您的意思是带有文件范围和外部链接的变量

全局变量。明确说明这一点,因为C

标准没有提到全球标准。变量属性。

它们是否存储在堆中,因此它们可以跨越函数进行访问。或者它们如何处理c。




如何完成,属于实现(例如,您的平台/操作

系统/编译器/链接器组合)。 C既不知道
堆也不知道堆栈。

如果你想知道全局怎么样?变量是为你的
实现实现的,在新闻组中询问你的至少部分

实现是关于主题的。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。


Umesh写道:

如何在C中处理Globals,因为该程序中的任何函数都可以获取全局变量。

它们是否存储在堆中,因此它们可以在功能中被激活。或者它们如何在c中处理。




如评论其他 - post你对术语的使用有点偏僻。 C有



" heap"。一些实现使用堆数据结构用于malloc()ed

数据。


所谓的全局数据通常保存在单独的内存区域中。

主要

区域通常是静态的,全局的,动态的和堆栈(我打赌我错过了

)。


从某种意义上说,我看不到你的问题。全球数据只是可以从任何地方访问的数据。在平坦的地址空间中,这比本地数据更容易实现。也许我很早就接触过FORTRAN

...

-

Nick Keighley


编程永远不会无聊,因为任何平凡的东西和重复的b $ b应该由电脑完成。

~Alan Turing


" Nick Keighley" < NI ****************** @ hotmail.com>写道:

Umesh写道:

如何在C中处理全局变量,因为全局变量可以被该程序中的任何函数所取代。



如同评论其他 - 发布您使用的术语是有点偏。 C
没有堆。某些实现使用堆数据结构来表示
malloc()ed数据。

所谓的全局数据通常保存在单独的内存区域中。
主要区域通常是静态的,全局的,动态的和堆栈(我打赌
我错过了一个)。

从某种意义上说,我看不到你的问题。全局数据只是可以从任何地方访问的数据。在平坦的地址空间中,这比本地数据更容易实现。也许我早年也接触过FORTRAN ......




C中有三个*存储持续时间*:静态,自动和。静态是指全局和全局。变量和变量

在具有静态的函数内声明关键词。自动将
指向不是静态的局部变量(通常存储在堆栈中)。

Allocated指的是用malloc()分配的对象(通常存储

on a heap)。


这与范围和可见性不同,它决定了

符号的可见位置。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


How Globals are handle in C since global variables can be acessed by
any function in that program.

Is it they stored in heap so they can be acessed across fuction.OR how
they handle in c.

解决方案

Umesh wrote:

How Globals are handle in C since global variables can be acessed by
any function in that program.
I assume you mean "variable with file scope and external linkage"
by "global variable". State things like that clearly, as the C
standard does not mention a "global" variable property.
Is it they stored in heap so they can be acessed across fuction.OR how
they handle in c.



How this is done, belongs to the implementation (your platform/operating
system/compiler/linker combination, for example). C knows neither
heap nor stack.
If you want to know how "global" variables are realized for your
implementation, ask in a newsgroup where at least part of your
implementation is on-topic.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Umesh wrote:

How Globals are handle in C since global variables can be acessed by
any function in that program.

Is it they stored in heap so they can be acessed across fuction.OR how
they handle in c.



as was commented else-post your use of terminology is a bit off. C has
no
"heap". Some implementations use a heap data structure for malloc()ed
data.

So called global data is usually held in a separate memory area. The
major
areas are usually static, global, dynamic and the stack (I bet I missed
one).

In a sense I can''t see your problem. Global data is simply data that is

accessable from anywhere. In a flat address space this is easier to
implement than local data. Perhaps I was exposed to FORTRAN too
early in life...
--
Nick Keighley

Programming should never be boring, because anything mundane and
repetitive should be done by the computer.
~Alan Turing


"Nick Keighley" <ni******************@hotmail.com> writes:

Umesh wrote:

How Globals are handle in C since global variables can be acessed by
any function in that program.

Is it they stored in heap so they can be acessed across fuction.OR how
they handle in c.



as was commented else-post your use of terminology is a bit off. C
has no "heap". Some implementations use a heap data structure for
malloc()ed data.

So called global data is usually held in a separate memory area. The
major areas are usually static, global, dynamic and the stack (I bet
I missed one).

In a sense I can''t see your problem. Global data is simply data that
is accessable from anywhere. In a flat address space this is easier
to implement than local data. Perhaps I was exposed to FORTRAN too
early in life...



There are three *storage durations* in C: static, automatic, and
allocated. Static refers to both "global" variables and variables
declared within functions with the "static" keyword. Automatic refers
to local variables that aren''t static (commonly stored on a stack).
Allocated refers to objects allocated with malloc() (commonly stored
on on a heap).

This is distinct from scope and visibility, which determine where a
symbol is visible.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于Globals如何处理C.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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