OOP 语言在内存利用率方面与过程语言有何不同

How OOP languages differs from procedural languages in terms of memory utilization

我想了解 OOP 编程语言在内存利用方面与过程语言有何不同。更具体地说,假设我们正在讨论 JavaC 作为示例:

  1. 对象自动存储在 heap 中是真的吗,而在过程语言中您必须明确定义堆使用,例如在 C malloc 中?
  2. 如果我用 C 编写程序,OS 将创建该程序的虚拟页,包括堆和堆栈 spaces。如果我不在我的代码中使用 malloc,这意味着我的程序没有使用为其分配的堆,对吗?
  3. 由于Stack是用来存放局部变量和函数调用地址的,如果一个程序运行出Stackspace怎么办,OS是否扩展分页大小这个程序的一部分还是它只是使用堆作为扩展?

Is it true that objects are automatically stored in heap while in procedural languages you have to explicitly define the heap usage such as in C malloc?

这取决于语言。有些,例如 Object Pascal,要求所有 "objects" 都分配在堆上。其他的,例如 C++,允许对象以静态、堆或堆栈的形式存在。两种方法各有优缺点。

If I write a program in C, OS will create a virtual page of this program including heap and stack spaces. If I don't use malloc in my code, this means my program does not utilize the heap allocated for it, is that correct?

可能不会。 运行-time 库很可能会使用你背后的堆。

Since Stack is used to store local variables and function call addresses, what if a program ran out of Stack space, does OS extend the paging size of this program or it just uses the heap as an extension?

这取决于操作系统。通常,如果可以,OS 将尝试扩展堆栈。 t 不会使用堆来扩展堆栈。堆栈通常由两端不可访问的页面保护(如捕获空指针的第一页)。 运行出栈的可能结果是某种访问冲突。