简体   繁体   English

任何编程语言的内存分配取决于编译器还是系统架构?

[英]Memory allocation of any programming language depends on compiler or the system architecture?

Memory allocation of any programming language depends on compiler or the system architecture? 任何编程语言的内存分配取决于编译器还是系统架构? If it depends on compiler, then what difference 32 bit/64 bit architecture makes? 如果取决于编译器,那么32位/ 64位体系结构有何不同? If it depends on architecture, then why the memory size of variables are constant for 16/32/64 bit architectures? 如果取决于体系结构,那么为什么对于16/32/64位体系结构,变量的内存大小是恒定的? What is the impact of slack bytes on the system architecture? 松弛字节对系统体系结构有什么影响?

It's not either or. 既不是也不是。 Compiler, system architecture and system conventions have an effect on how memory allocation works. 编译器,系统架构系统约定会影响内存分配的工作方式。

For one, the system architecture includes the size of pointers. 首先,系统架构包括指针的大小。 So a compiler can't use more memory than a system provides, and can't address more bytes or have larger pointers than the CPU supports. 因此,编译器不能使用比系统提供的更多的内存,也不能寻址超过CPU支持的字节或更大的指针。 (I mean actual RAM, and real addresses. Of course it can use Virtual Memory-like constructs to remap the addresses the programmer uses to something that CPU can handle). (我的意思是实际的RAM和实际的地址。当然,它可以使用类似于虚拟内存的构造将程序员使用的地址重新映射到CPU可以处理的地址)。

Similarly, usually operating system vendors have conventions what a function call should be like: Whether parameters are passed on the stack or in registers. 同样,通常操作系统供应商都有函数调用的约定:参数是在堆栈上还是在寄存器中传递。 Whether parameters that are larger than a certain size should be copied onto the stack or provided as a pointer to a stack/heap object. 大于特定大小的参数应该复制到堆栈上还是作为指向堆栈/堆对象的指针提供。 If you want to call system functions, you will have to use these conventions. 如果要调用系统函数,则必须使用这些约定。

However, beyond that, it is left to the compiler to decide details. 但是,除此之外,编译器有权决定细节。 Eg many Pascal compilers pass additional hidden pointers to enclosing scope into built-in functions defined in the same language. 例如,许多Pascal编译器传递了额外的隐藏指针,以将范围包含在用同一语言定义的内置函数中。 As long as you know code has been written using the same language, you can agree on differences in calling conventions within the limits the system architecture permits. 只要您知道使用相同语言编写的代码,就可以在系统体系结构允许的限制内就调用约定的差异达成一致。 Eg if you know that your software has to run on similar CPUs with stricter requirements, you can choose to apply these strict requirements even on CPUs that don't need it (like compiling code so everything is at least 2-byte-aligned so your MC68040 code will also run on the older MC68000 which had that requirement). 例如,如果您知道您的软件必须在具有更严格要求的类似CPU上运行,则可以选择将这些严格要求应用于甚至不需要的CPU(例如编译代码,以便所有内容至少对齐2个字节,因此您可以MC68040代码也将在具有该要求的旧版MC68000上运行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM