是否有可能写在C一致性实现的malloc? [英] Is it possible to write a conformant implementation of malloc in C?

查看:113
本文介绍了是否有可能写在C一致性实现的malloc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续到能否在字符数组可以与任何数据类型用?

我知道动态内存和malloc的普遍的实现,引用可以在维基被发现。我也知道,由malloc返回的指针可以转换为任何程序员希望,甚至没有一个警告,因为在6.3.2.3指针§1

I know about dynamic memory and common implementations of malloc, references can be found on wikipedia. I also know that the pointer returned by malloc can be cast to whatever the programmer wants, without even a warning because the standard states in 6.3.2.3 Pointers §1

要作废的指针可以转换或从一个指向任何不完全或对象
  类型。一个指向任何完整或对象类型可被转换成一指向void
  并再次返回;结果应比较等于原始指针

A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

问题是假设我有没有的malloc 免费,我怎么可以建立兼容标准的C独立环境的这两个函数的实现?

The question is assuming I have a freestanding environment without malloc and free, how can I build in conformant C an implementation of those two functions?

如果我把有关标准的一些自由,很容易:

If I take some freedom regarding the standard, it is easy:


  • 开始用大字符数组

  • 使用一个相当大的调整(8应该是足够的许多体系)

  • 实施从数组返回地址,在该路线,跟踪的东西已经被分配算法 - 很好的例子可以在发现的malloc执行?

  • start with a large character array
  • use a reasonably large alignment (8 should be enough for many architectures)
  • implement an algorithm that returns addresses from that array, at that alignment, keeping track of what has been allocated - nice examples can be found in malloc implementation?

的问题是有效的类型由该实现返回的指针将仍然字符*

The problem is that the effective type of the pointers returned by that implementation will still be char *

和标准说,在同一段§7

And standard says in same paragraph § 7

一个指针到对象或不完整的类型可被转换成一个指针到一个不同的
  对象或不完全类型。如果所得到的指针不正确的对准的
  指向的类型,行为是不确定的。否则,转换回时再中,
  结果应比较等于原始指针

A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type. If the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined. Otherwise, when converted back again, the result shall compare equal to the original pointer.

这似乎并没有让我pretend什么声明为简单的字符,可以神奇地包含另一种类型,甚至不同的类型在这个阵列的不同部分或在同一部位不同时刻。说不同的解引用这样的指针看起来与标准的严格间pretation未定义的行为。这就是为什么常见的成语使用,当你在一个字符串缓冲区对象的字节重新presentation,例如当您从网络阅读的memcpy ,而不是混淆流。

That does not seem to allow me to pretend that what was declared as simple characters can magically contains another type, and even different types in different part of this array or at different moments in same part. Said differently dereferencing such pointers seem undefined behaviour with a strict interpretation of standard. That is why common idioms use memcpy instead of aliasing when you get a byte representation of an object in a string buffer, for example when you read it from a network stream.

所以,我怎么能建立在纯C成为符合的malloc ???

So how can I build a conformant implementation of malloc in pure C???

推荐答案

C标准的作者提出更加精力投入到其指定的行为并没有的明显高于那些人,因为他们希望预计明智的编译器编写者将支持有益的行为标准是否不强制它,因为钝的编译器作家能产生已经完全兼容的,但完全没用兼容的实现(*)。

The authors of the C Standard put far more effort into specifying behaviors which weren't obviously desirable than those that were, since they expected that sensible compiler writers would support useful behaviors whether or not the Standard mandated it, and since obtuse compilers writers could produce "compliant" implementations that were fully-compliant but completely useless(*).

这是可以编写可靠和高效的malloc()上之前C89的出现多种平台当量,我看不出有任何理由相信,作者意图的人写C89编译器为它已经能够处理平台malloc()函数当量previously不会让这些实现同样能够为他们的predecessors。不幸的是,(这是C89的组合超和predecessors),这是流行在20世纪90年代的语言已经换成了劣质方言省略了C89的作者会认为是理所当然的,并希望别人特点也这样做。

It was possible to write reliable and efficient malloc() equivalents on many platforms prior to the advent of C89, and I see no reason to believe that the authors intended that people writing C89 compilers for a platform which had been able to handle malloc() equivalents previously would not make those implementations just as capable as their predecessors. Unfortunately, the language which was popular in the 1990s (which was a combined superset of C89 and its predecessors) has been replaced by a poor-quality dialect which omits features that the authors of C89 would have taken for granted and expected others to do likewise.

甚至超越人们如何获得内存的问题,更大的问题是,
的malloc()承诺,新分配的内存会,在最坏的情况,举行
不确定的值;因为结构类型有没有陷阱再presentations,
阅读使用结构类型的指针这样的存储将定义
行为。如果内存是previously使用一些其他类型的书面,
然而,结构型的读取会具有不确定的行为,除非任何
免费()或malloc()函数物理擦除所有有问题的存储,
从而否定其的malloc(的性能优势),而不是仅仅
释放calloc()。

Even beyond the question of how one acquires memory, a larger issue is that malloc() promises that newly-allocated memory will, at worst, hold Indeterminate Value; because structure types have no trap representations, reading such storage using a pointer of structure type will have defined behavior. If the memory was previously written using some other type, however, a structure-type read would have Undefined Behavior unless either the free() or malloc() physically erases all of the storage in question, thus negating the performance benefit of having malloc() rather than just calloc().

(*),前提是至少存在一组,在兼容的方式实施过程没有UB,给任何其他组源文件时的实现可能需要的堆栈空间任意(也许不可能大)大量的源文件,和行为在任意的方式,如果空间是不可用的。

(*)Provided that there exists at least one set of source files that the implementation processes in compliant fashion without UB, an implementation may require arbitrary (perhaps impossibly large) amounts of stack space when given any other set of source files, and behave in arbitrary fashion if that space is unavailable.

这篇关于是否有可能写在C一致性实现的malloc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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