为什么不分配原始数据类型的内存? [英] why memory for primitive data types is not allocated?

查看:176
本文介绍了为什么不分配原始数据类型的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原始数据类型,如char,bool和int有一些内存。假设char,它有1字节的内存。当我们在代码中使用char变量时,变量必须要有1个字节的内存。为什么在这种情况下我们不分配内存。和我们从来没有为它分配内存,它如何使用内存,即。在这种情况下,CPU是否为它分配内存。我在某处读取原始数据类型放在堆栈上,并在完成工作后将其删除。什么样的堆栈?

解决方案

在过于简单化的风险下,有三类内存为数据:1)静态,2)堆栈3)堆。



它们以不同的方式分配。



如果你有

  static char something;在函数或

中定义的



  char something;函数外部的

,该数据由链接器使用来自编译器的指令定义,程序加载器。



几乎每个处理器使用堆栈来支持嵌套数据(例如,函数调用)。堆栈是每个进程(和每个处理器模式)存在的一块内存块。有一个称为堆栈指针的硬件寄存器,用于标识堆栈的当前位置。通常,SP从堆栈的高端开始并向下工作。要在堆栈上分配内存,程序从堆栈指针中减去所需的字节数。要解除分配,它会添加到堆栈指针。分配和释放总是在同一端发生。



然后在堆栈上有两个操作。 PUSH意味着把东西放在堆栈上。 POP删除它。大多数处理器都有PUSH和POP的指令



如果你有

 在函数中定义的

,该内存由程序按照编译器的指示类似这样调整堆栈指针(我现在遗漏一个框架指针)

  SUB BYTESNEEDED,SP 

输入函数并通过执行

释放

<$ p在离开功能之前,$ p> ADD BYTESNEEDED,SP

在执行函数期间,局部变量与堆栈指针有偏移量。



这通常使用第二个寄存器,通常称为帧指针。一个函数通常在开始时这样做。

  PUSH FP;保存旧框架
MOV SP FP;保存堆栈指针
SUB BYTESNEEDED,SP

/ p>

MOV FP,SP;释放由函数
POP FP分配的所有堆栈;恢复旧堆栈指针



使用两个寄存器的原因是可以从堆栈中动态分配数据。



这是一个常见的函数(虽然我相信它不是一个标准的C函数),称为alloca,它是从堆栈分配的malloc的替代品。

  void dosomething(int amount)
{
char * data = alloca(amount);
}



使用alloca,当函数返回并重置堆栈时,数据会自动释放。



这是一个很长的答案,你的问题。是的,当声明一个char,必须有一个分配。但是,这种分配是在幕后完成的,您无需付出努力。


Primitive data types such as char, bool and int have some memory. Suppose char, which has 1 byte of memory. When we use a char variable in our code, then the variable must require 1 byte of memory. Why don't we allocate memory in this case. And as we never allocate memory for it, how does it use the memory ,i.e. Is the CPU allocate memory for it in this case.Also I read somewhere that the primitive data types are put on stack and removed when the work is done for it. What kind of stack? How does the stack comes into picture in this case?

解决方案

At the risk of being over simplistic, there are three classes of memory for data: 1) static, 2) stack 3) heap.

They are allocated in different ways.

if you have

 static char something ;

defined in a function or

char something ;

outside of a function, that data is defined by the linker using instructions from the compiler and allocated by the program loaders.

Nearly every processor in existence uses a stack to support nested data (e.g., function calls). The stack is a block of memory that exists for every process (and for every processor mode). There is a a hardware register called the Stack Pointer that identifies the current position of the stack. Usually the SP starts at the high end of the stack and works downward. To allocate memory on the stack, the program subtracts the number of bytes required from the stack pointer. To deallocate, it adds to the stack pointer. The allocations and deallocations always take place at the same end.

There are then two operations on the stack. PUSH means put something on the stack. POP removes it. Most processors have instructions to PUSH and POP

If you have

 char something

defined within a function, that memory is allocated by the program as directed by the compiler by doing something like this to adjust the stack pointer (I'm leaving out a frame pointer for now)

 SUB   BYTESNEEDED, SP

upon entering the function and freed by doing

ADD BYTESNEEDED, SP

before leaving the function. During the execution of the function, the local variables are at offsets from the stack pointer.

This usually done by using a second register, usually called a frame pointer. A function usually does something like this at the start

PUSH  FP       ; Save the old Frame Point 
MOV   SP  FP   ; Save the stack pointer
SUB   BYTESNEEDED, SP

at the end the function does something like

MOV FP, SP ; Free all the stack allocated by the function POP FP ; Restore the old stack pointer

The reason for using two registers is that it is possible to dynamically allocate data from the stack.

THere is a common function (although I believe it is not a standard C function) called alloca that is an alternative to malloc that allocates from the stack

void dosomething (int amount)
{
    char *data = alloca (amount) ;
} 

With alloca, the data is automatically freed when the function returns and resets the stack.

That is a long winded answer to your question. Yes, when declare a char, there has to be an allocation for it. However, this allocation is done behind the scenes without effort on your part.

这篇关于为什么不分配原始数据类型的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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