运行时的 OOP 与过程式

发布于 2024-10-01 05:37:33 字数 200 浏览 9 评论 0原文

我有一个非常简单的问题,我在互联网上找不到答案。

所以,我的问题是,在过程编程中,代码位于代码段中,该段进入只读内存区域。变量位于堆栈或堆上。

但 OOP 说对象是在内存中创建的。那么,这是否意味着函数也被写入R/W内存区域?

而且,操作系统是否必须有一些内置的 OOP 程序支持?例如,如果操作系统不允许读取只读代码段之外的指令。谢谢。

I have very simple question I cant find answer anywhere on the internet.

So, my question is, in procedural programming, code is in code section, which goes into Read Only memory area. Variables are either on stack or heap.

But OOP says that object are created in memory. So, does it mean even functions are written into R/W memory area?

And, does Os have to have some inbuilt OOP programs support? For example if OS doesent allowed to read instruction outside Read only code section. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

和我恋爱吧 2024-10-08 05:37:33

一般来说,面向对象编程和过程编程都是仅存在于源代码级别的抽象。一旦程序被编译成可执行的机器代码,这些抽象就不再存在。因此,特定语言是否是面向对象的或过程性的,与它使用的内存区域或执行期间指令放置的位置无关。

操作系统本身通常不知道或关心特定的可执行文件是用 OOP 还是过程语言编写的。它只关心可执行文件使用与其本机指令集兼容的二进制操作码,并且可执行文件具有它可以理解的 ABI(二进制接口)。

Generally, both OOP and procedural programming are abstractions which exist only at the source-code level. Once a program is compiled into executable machine-code, these abstractions cease to exist. So whether or not a particular language is OOP or procedural has no bearing on what regions of memory it uses, or where instructions are placed during execution.

The OS itself usually doesn't know or care whether a particular executable was written in an OOP or procedural language. It only cares that the executable uses binary op-codes compatible with its native instruction set, and that the executable has an ABI (binary interface) that it understands.

稀香 2024-10-08 05:37:33

这是一个好问题。

虽然对象构成函数数据理论上被放置在同一位置,但大多数实现将其分开。这样做的方法是将代码拆分出来并存储到 RO 段中。 RW 区域中的对象可以通过某种方式引用 RO 区域中的该代码。代码和数据的耦合仅在概念上由人类程序员和类型检查器使用,以确保您不违反规则和原则。

类似 Java/C# 的语言通常会使得每个对象都有一个标识对象类型的标签。对象本身只是一个结构体,其中包含按预先指定的顺序排列的所有字段。然后可以使用该标签来查找 RO 区域中要调用的函数。 RO 区域中的函数被更改为采用一个额外的参数,称为 thisself,通过它们可以访问所述对象的内容。当该方法需要引用字段时,它知道预先指定的顺序,因此可以正确执行该操作。请注意,解决继承需要一些技巧,但这是这个想法的关键。

类似 Python/Ruby 的语言通常会使对象成为哈希表,其中方法是指向 RO 区域中代码的指针(前提是该语言是编译的并且不通过字节码解释器运行)。函数调用是通过查找哈希表内容并跟随代码指针来进行的。字段也在同一个哈希表中查找。

有了这些基础知识,大多数实现都会采取一些技巧来避免遵循指针来查找要调用的函数的部分。他们试图找出并缩小对单个函数的可能调用范围。然后他们可以通过直接调用正确的函数来替换查找,这是一种更快的解决方案。

tl;dr 版本:语言语义将字段和方法视为对象的一部分。 实现将它们分为 RO 和 RW 段。因此不需要操作系统支持。

This is a good question.

Whereas as object constitutes functions and data as being placed in the same spot theoretically, most implementations split it. The way you do it, is that code is split out and stored into the RO segment. An object in the RW area then have a way to refer back to that code in the RO area. The coupling of code and data is only used conceptually by the human programmer and the type checker to ensure that you do not violate the rules and principles.

A Java/C#-like language will usually be made such that each object has a tag identifying the type of the object. The object itself is simply a struct containing all the fields laid out in a prespecified order. This tag can then be used to look up which function in the RO-area to call. The function in the RO-area is altered to take an extra parameter, called this or self through which the contents of said object can be reached. When the method needs to refer to fields, it knows the pre-specified order, so it can do that correclty. Note that there are some tricks needed to solve inheritance, but this is the crux of the idea.

A Python/Ruby-like language will usually make an object be a hash-table where a method is a pointer to the code in the RO-area (provided that the language is compiled and not run through a bytecode interpreter). Function calls are made by looking up the hash-table contents and following the code pointer. Fields are also looked up in the same hash table.

With those basics down, most implementations make tricks to avoid the part where a pointer is followed to find the function to call. They try to figure out and narrow down the possible call to a single function. Then they can replace the lookup with a direct call to the right function, a much faster solution.

the tl;dr version: The language semantics views fields and methods as part of an object. The implementation split them into RO and RW segments. As such no OS support is needed.

野心澎湃 2024-10-08 05:37:33

OOP没有这么说。我不知道你在哪里读到的,如果你添加一个引用的话会有帮助。

对象变量,因此您对变量的了解对于对象来说是正确的。在像 C#(实际上是 .net 框架)这样的语言中,对象只能存储在堆中,因为它们是所谓的引用类型。在 C++ 中,它们可以生活在任何地方。

但是 OOP 说对象是在内存中创建的。那么,这是否意味着函数也被写入R/W内存区域?

由此我得出的结论是,您认为函数是对象。并非所有 OOP 语言都是如此。它来自函数语言,其中函数是第一类对象。在大多数情况下,函数是不可变的,并且放置在只读部分中。

Windows、Linux 和 MacOsx 等常见操作系统无法识别对象。这纯粹是程序概念。 .net框架和java vm提供了抽象层。它们是具有内置对象支持的执行环境。

OOP doesn't say this. I have no idea where you read it, if you add a quote that would help.

Objects are variables, so what you know about variables is correct for objects. In languages like C# (.net framework actually) objects can only be stored in heap, because they are so called reference types. In C++ they can live anywhere.

But OOP says that object are created in memory. So, does it mean even functions are written into R/W memory area?

From this i concluded that you think that functions are objects. That is true in far not every OOP language. It is from functional languages where functions are first class objects. Functions are in majority of cases immutable and are placed in read only sections.

Common OSes like Windows, Linux and MacOsx are unaware of objects. This is purely program concept. .net framework and java vm provide layer of abstraction. They are execution environments that have build in object support.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文