内存分配/释放? [英] Memory Allocation/Deallocation?
问题描述
我最近一直在看内存分配,我有点困惑的基础。我不能把我的头围绕在简单的东西。分配内存是什么意思?发生了什么?我会感谢以下任何问题的答案:
- memory被分配?
- 这是什么记忆?数组中的空间?或者是其他东西?
- 这个记忆分配时会发生什么?
- 内存取消分配时会发生什么情况?
-
如果有人可以回答malloc在这些C ++行中的行为,它也会真的帮助我:
char * x;
x =(char *)malloc(8);
谢谢。
内存模型
C ++标准具有内存模型。它尝试以通用方式对计算机系统中的内存建模。该标准定义一个字节是存储器模型中的存储单元,并且该存储器由字节组成(§1.7):
基本存储单元在C ++内存模型中是字节。 [...] C ++程序可用的内存由一个或多个连续字节序列组成。
h3>
标准总是提供一个对象模型。这指定对象是存储区域(因此它由字节组成并驻留在内存中)(§1.8):
C ++程序中的结构创建,销毁,引用,访问和操作对象。一个对象是一个存储区域。
所以我们走。内存是存储对象的地方。要在内存中存储对象,必须分配所需的存储区域。
分配和释放功能
该标准提供了两个隐式声明的全局范围分配函数:
void * operator new(std :: size_t);
void * operator new [](std :: size_t);
如何实现这些不是标准的问题。所有重要的是,他们应该返回一个指向某个存储区域的指针,其中对应于传递的参数(§3.7.4.1)的字节数:
分配函数尝试分配所请求的存储量。如果它成功,它将返回存储块的开始的地址,其字节长度应至少与请求的大小一样大。
它还定义了两个相应的解除分配函数: / p>
void operator delete(void *);
void operator delete [](void *);
定义为重新分配先前已分配的存储(§3.7.4.2):
如果给标准库中的释放函数的参数是不是空指针值(4.10)的指针,则释放函数将释放
新的指针指向的存储引用的所有指针
和 delete
通常,您不需要使用分配和释放功能直接因为他们只给你未初始化的内存。相反,在C ++中,应该使用 new
和 delete
来动态分配对象。通过使用上述分配函数之一, new-expression 获得所请求类型的存储,然后以某种方式初始化该对象。例如 new int()
将为 int
对象分配空间,然后将其初始化为0.见§5.3。 4:
new-expression通过调用分配函数(3.7.4.1)获取对象的存储。
[...]
新建表达式 [...]
反方向, delete
一个对象的操作数的值(如果有),然后释放存储(§5.3.5):
delete-expression 不是空指针值, delete-expression 将调用对象或要删除的数组元素的析构函数p>
[...]
如果 delete-expression >不是空指针值, delete-expression 将调用释放函数(3.7.4.2)。
其他分配
但是,这些并不是存储被分配或释放的唯一方式。语言的许多构造隐式地需要存储的分配。例如,给定对象定义,如 int a;
也需要存储(§7):
定义导致保留适当的存储量,并进行任何适当的初始化(8.5)。
C标准库: malloc
和免费
此外,< cstdlib>
头导入 stdlib.h
C标准库的内容, malloc
和免费
函数。它们也被C标准定义为分配和释放内存,非常类似于C ++标准定义的分配和释放功能。以下是 malloc
(C99§7.20.3.3)的定义:
void * malloc(size_t size);
描述
malloc
函数为大小由size
和
(其值不确定)指定的对象分配空间。
返回
malloc
函数返回空指针或指向分配空间的指针。
并且 free
(C99§7.20.3.2)的定义:
void free(void * ptr);
说明 br>
free
函数导致ptr
指向的空间被释放,
可用于进一步分配。如果ptr
是空指针,则不会执行任何操作。否则,如果参数与calloc
,malloc
或> realloc
函数,或者如果通过调用free
或realloc
,
行为未定义。
但是,从来没有好的借口使用 malloc
和免费
。如前所述,C ++有自己的选择。
问题的答案
因此,直接回答您的问题:
-
正在分配的记忆在哪里?
C ++标准不在乎。它简单地说,程序有一些由字节组成的内存。
-
这是什么记忆?数组中的空间?或者是其他东西?
就标准而言,内存只是一个字节序列。这是非常通用的,因为标准只尝试建模典型的计算机系统。
-
当这个内存被分配时,会发生什么?
分配内存使得某些存储区域可供程序使用。对象在分配的内存中初始化。所有你需要知道的是,你可以分配内存。
-
当内存被释放时会发生什么?
取消分配以前分配的内存会导致程序无法使用内存。
-
如果有人可以回答malloc在这些C ++行中的行为,它也会真的帮助我:
char * x;
x =(char *)malloc(8);
这里,
malloc
的内存。返回的指针被转换为char *
并存储在x
中。
I have been looking at memory allocation lately and I am a bit confused about the basics. I haven't been able to wrap my head around the simple stuff. What does it mean to allocate memory? What happens? I would appreciated answers to any of these questions:
- Where is the "memory" that is being allocated?
- What is this "memory"? Space in an array? Or something else?
- What happens exactly when this "memory" gets allocated?
- What happens exactly when the memory gets deallocated?
It would also really help me if someone could answer what malloc does in these C++ lines:
char* x; x = (char*) malloc (8);
Thank you.
The Memory Model
The C++ standard has a memory model. It attempts to model the memory in a computer system in a generic way. The standard defines that a byte is a storage unit in the memory model and that memory is made up of bytes (§1.7):
The fundamental storage unit in the C++ memory model is the byte. [...] The memory available to a C++ program consists of one or more sequences of contiguous bytes.
The Object Model
The standard always provides an object model. This specifies that an object is a region of storage (so it is made up of bytes and resides in memory) (§1.8):
The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is a region of storage.
So there we go. Memory is where objects are stored. To store an object in memory, the required region of storage must be allocated.
Allocation and Deallocation Functions
The standard provides two implicitly declared global scope allocation functions:
void* operator new(std::size_t);
void* operator new[](std::size_t);
How these are implemented is not the standard's concern. All that matters is that they should return a pointer to some region of storage with the number of bytes corresponding to the argument passed (§3.7.4.1):
The allocation function attempts to allocate the requested amount of storage. If it is successful, it shall return the address of the start of a block of storage whose length in bytes shall be at least as large as the requested size. There are no constraints on the contents of the allocated storage on return from the allocation function.
It also defines two corresponding deallocation functions:
void operator delete(void*);
void operator delete[](void*);
Which are defined to deallocate storage that has previously been allocated (§3.7.4.2):
If the argument given to a deallocation function in the standard library is a pointer that is not the null pointer value (4.10), the deallocation function shall deallocate the storage referenced by the pointer, rendering invalid all pointers referring to any part of the deallocated storage.
new
and delete
Typically, you should not need to use the allocation and deallocation functions directly because they only give you uninitialised memory. Instead, in C++ you should be using new
and delete
to dynamically allocate objects. A new-expression obtains storage for the requested type by using one of the above allocation functions and then initialises that object in some way. For example new int()
will allocate space for an int
object and then initialise it to 0. See §5.3.4:
A new-expression obtains storage for the object by calling an allocation function (3.7.4.1).
[...]
A new-expression that creates an object of type T initializes that object [...]
In the opposite direction, delete
will call the destructor of an object (if any) and then deallocate the storage (§5.3.5):
If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted.
[...]
If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will call a deallocation function (3.7.4.2).
Other Allocations
However, these are not the only ways that storage is allocated or deallocated. Many constructs of the language implicitly require allocation of storage. For example, giving an object definition, like int a;
, also requires storage (§7):
A definition causes the appropriate amount of storage to be reserved and any appropriate initialization (8.5) to be done.
C standard library: malloc
and free
In addition, the <cstdlib>
header brings in the contents of the stdlib.h
C standard library, which includes the malloc
and free
functions. They are also defined, by the C standard, to allocate and deallocate memory, much like the allocation and deallocation functions defined by the C++ standard. Here's the definition of malloc
(C99 §7.20.3.3):
void *malloc(size_t size);
Description
Themalloc
function allocates space for an object whose size is specified bysize
and whose value is indeterminate.
Returns
Themalloc
function returns either a null pointer or a pointer to the allocated space.
And the definition of free
(C99 §7.20.3.2):
void free(void *ptr);
Description
Thefree
function causes the space pointed to byptr
to be deallocated, that is, made available for further allocation. Ifptr
is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by thecalloc
,malloc
, orrealloc
function, or if the space has been deallocated by a call tofree
orrealloc
, the behavior is undefined.
However, there's never a good excuse to be using malloc
and free
in C++. As described before, C++ has its own alternatives.
Answers to Questions
So to answer your questions directly:
Where is the "memory" that is being allocated?
The C++ standard doesn't care. It simply says that the program has some memory which is made up of bytes. This memory can be allocated.
What is this "memory"? Space in an array? Or something else?
As far as the standard is concerned, the memory is just a sequence of bytes. This is purposefully very generic, as the standard only tries to model typical computer systems. You can, for the most part, think of it as a model of the RAM of your computer.
What happens exactly when this "memory" gets allocated?
Allocating memory makes some region of storage available for use by the program. Objects are initialized in allocated memory. All you need to know is that you can allocate memory. The actual allocation of physical memory to your process tends to be done by the operating system.
What happens exactly when the memory gets deallocated?
Deallocating some previously allocated memory causes that memory to be unavailable to the program. It becomes deallocated storage.
It would also really help me if someone could answer what malloc does in these C++ lines:
char* x; x = (char*) malloc (8);
Here,
malloc
is simply allocating 8 bytes of memory. The pointer it returns is being cast to achar*
and stored inx
.
这篇关于内存分配/释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!