Mac OSX是否应该有一个;malloc.h”一词;文件
客户代码希望在“常见可疑”位置之一找到包含文件Mac OSX是否应该有一个;malloc.h”一词;文件,c,macos,include-path,C,Macos,Include Path,客户代码希望在“常见可疑”位置之一找到包含文件malloc.h。在我的Mac电脑AFAICT上,没有malloc.h,至少在任何你希望找到它的地方都没有,比如/usr/include,/usr/local/include,或者/opt/local/include。由于malloc()通常是在stdlib.h中定义的,而且由于代码中包含stdlib.h,所以我可以通过注释malloc.h中的几个包含项来生成代码。我正在使用gcc进行构建 但有两个问题:我的gcc不知怎么搞砸了吗?那个文件应该在那里
malloc.h
。在我的Mac电脑AFAICT上,没有malloc.h
,至少在任何你希望找到它的地方都没有,比如/usr/include
,/usr/local/include
,或者/opt/local/include
。由于malloc()
通常是在stdlib.h
中定义的,而且由于代码中包含stdlib.h,所以我可以通过注释malloc.h
中的几个包含项来生成代码。我正在使用gcc
进行构建
但有两个问题:我的gcc不知怎么搞砸了吗?那个文件应该在那里吗?而且,代码几乎立即爆炸,带有一个seg故障,我还没有找到。这可能是使用错误的
malloc()
导致的吗?已弃用malloc.h
,不应使用。它还包含一些非标准函数。如果要使用malloc
,请包括stdlib.h
。甚至C89标准也没有提到malloc.h
如果这是您的问题的原因,我不知道,但很有可能。下面的列表可以提供在哪里应该/可以找到该文件的想法。不过,如果没有更多细节,我无法解释这一故障。我看不出它与malloc()有任何关系
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/malloc.h
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/malloc/malloc.h
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/sys/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/SDKs/WatchSimulator.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator.sdk/usr/include/malloc/malloc.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/malloc/malloc.h
$
malloc.h
看起来不太正常,所以您的安装很好,符合要求。如果没有找到malloc.h,它如何运行(和seg故障)?如果没有所需的头,它就不应该构建。要使用malloc,您必须#include
,而不是malloc。几乎没有进一步的研究表明,该头是glibc特定的,并且不可移植。如果您注释掉malloc.h的#include,编译器会假设malloc()
返回int
,可能是32位整数。但在64位平台上,包括现代Mac,指针是64位的。因此,如果编译器认为malloc
返回int,指针值的前32位会被刮去,这肯定会导致类似“seg故障立即爆炸”的情况。