静态,全局变量内存分配 [英] static, global variable memory allocation

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

问题描述

这是一个面试问题,我在这里给出了答案,你能不能给我b $ b请检查我吗?


问:什么是内存分配对于函数中的静态变量,

一个自动变量和全局变量?


我的答案:函数和全局变量中的静态变量是

在头部分配,自动变量在堆栈中分配。


对吗?

解决方案

fd*******@gmail.com aécrit:


这是一个面试问题,我在这里给出了答案,你能给我一个b $ b请检查我吗?


问:函数中静态变量的内存分配是什么,

自动变量和全局变量?


我的答案:函数和全局中的静态变量变量是在头部分配的
,自动变量是allocat堆栈。


对吗?



也许你的意思是堆不是头 ????

如果是这样的话,你大多是对的,这取决于

的实现和操作系统的运行等。


一个程序有一个固定区域,包含程序的初始数据。

这不是传统意义上的堆,只是一个RAM空间

填充表格,字符串,双数据等。


堆是一个可变区域,用于在程序执行期间动态地为数据分配空间

。该数据可以是函数本地的
,也可以是全局的。你混淆了一个变量的范围

,以及分配它的地方,

不一样。


{

char * m = malloc(2678);

//使用m

免费(m);

}


m是从堆中分配的,但它是该块的本地元素

,因为它在块完成之前被释放。指针类型的变量

m是该块的本地,并且仅当该块处于活动状态时才有效



大多数情况下,局部变量是从堆栈中分配的,但是有些人在这里说有没有堆栈的机器实现了

C.我不知道,我从未见过其中的一个。在大多数系统中的任何

情况下,堆栈都存在并且用于分配局部变量


在文章< 11 ********************* @ v33g2000cwv.googlegroups中。 com>,
fd*******@gmail.com < fd *** **** @ gmail.com写道:


>这是一个面试问题,我在这里给出了答案,你能不能给我查一下?

函数中静态变量的内存分配是什么,
自动变量和全局变量?
我的答案:函数中的静态变量和全局变量分配在头部,并且自动变量在堆栈中分配。



这对于常见的实现是正确的,但它不必像这样工作

。并且自动变量通常存储在寄存器中,就像堆栈一样。


(我假设你的意思是堆而不是头)虽然在用户大脑中分配

变量的想法很有吸引力。)


- Richard


-

考虑在一些字母表中需要多达32个字符

- 1963年的X3.4。


" fd ******* @ gmail.com" < fd ******* @ gmail.comwrites:


我的回答:函数和全局变量中的静态变量是

在头部分配,自动变量在堆栈中分配。



许多答案可能是正确的。这个错了。

-

Ben Pfaff
bl * @ cs .stanford.edu
http://benpfaff.org


This is an interview question and I gave out my answer here, could you
please check for me?

Q. What are the memory allocation for static variable in a function,
an automatic variable and global variable?

My answer: static variable in function and global variable are
allocated in head, and automatic variable is allocated in stack.

Right?

解决方案

fd*******@gmail.com a écrit :

This is an interview question and I gave out my answer here, could you
please check for me?

Q. What are the memory allocation for static variable in a function,
an automatic variable and global variable?

My answer: static variable in function and global variable are
allocated in head, and automatic variable is allocated in stack.

Right?

Maybe you mean "heap" not "head" ???

If that is the case, you are mostly right, it depends on the
implementation and OS running, etc.

A program has a fixed area with the initial data of the program.
This is not the heap in the traditional sense but just a RAM space
filled with tables, strings, double data, etc.

The heap is a variable area used to allocate space for data
dynamically during program execution. That data can be
local to a function, or be global. You are confusing the scope
of a variable, and the place where it is allocated, what is
not the same.

{
char *m = malloc(2678);
// use of m
free(m);
}

m is allocated from the heap, but it is local to that block
since it is freed before the block is finished. The varible
"m", of pointer type, is local to that block, and is active
only when that block is active.

Mostly, local variables are allocated from the stack, but some
people here say there are machines without stack that implement
C. I do not know, I have never seen one of those. In any
case in most systems, the stack exists and it is used to
allocate local variables.


In article <11*********************@v33g2000cwv.googlegroups. com>,
fd*******@gmail.com <fd*******@gmail.comwrote:

>This is an interview question and I gave out my answer here, could you
please check for me?

Q. What are the memory allocation for static variable in a function,
an automatic variable and global variable?

My answer: static variable in function and global variable are
allocated in head, and automatic variable is allocated in stack.

This is right for common implementations, but it doesn''t have to work
like that. And auto variables are commonly stored in registers as
well as on the stack.

(I assume you mean "heap" rather than "head", though the idea of allocating
variables in the user''s brain appeals.)

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.


"fd*******@gmail.com" <fd*******@gmail.comwrites:

My answer: static variable in function and global variable are
allocated in head, and automatic variable is allocated in stack.

Many answers could possibly be right. This one is wrong.
--
Ben Pfaff
bl*@cs.stanford.edu
http://benpfaff.org


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

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