1

In C, how can I detect if a some form of malloc has been made available, regardless of target platform or compiler? Is it enough to detect _STDLIB_H_?

I would like to include a header utility function that uses malloc but only if it's already made available by library user.

UPDATE: The library requires some heap or stack memory allocation, the amount of which may or may not be known at compile time. The library can calculate, at runtime, the exactly amount of memory needed. So for those who need to wait till runtime and can/want to use heap memory, I want to make automatic allocation available. But I do not want the library itself to load 'stdlib.h'.

12
  • See C11 p4 --> ie, if you're on a hosted implementation, you have the malloc() functionality "for free" (may not be available in freestanding implementations). Commented Jan 18, 2022 at 17:39
  • 2
    I don't get it. You have some nefarious plans for malloc, but only if the user has implicitly provided access to its prototype prior to including your header? Forgive me, but that is (a) incredibly brittle, and (b) trivially circumventable. Perhaps I misunderstood the question. Commented Jan 18, 2022 at 17:40
  • 1
    Although a standard/conforming implementation will have malloc it might not have other functions. (e.g.) Most but not all impl have strdup. Look at what autoconf does. It creates tiny test programs to probe for functions (or .h files or libraries, etc). It compiles them. If they compile and link cleanly, then the [given] function is provided. If it found that strdup was available, it would output (e.g.) #define HAVE_STRDUP 1 to a "features" file (e.g. features.h). Include that. There are other such defines (e.g. HAVE_STDIO_H or HAVE_LIBC_SO) Commented Jan 18, 2022 at 17:49
  • 1
    C18 4.6 The two forms of conforming implementation are hosted and freestanding. A conforming hosted implementation shall accept any strictly conforming program. A conforming freestanding implementation shall accept any strictly conforming program in which the use of the features specified in the library clause (Clause 7) is confined to the contents of the standard headers <float.h>, <iso646.h>, <limits.h>, <stdalign.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, <stdint.h>, and <stdnoreturn.h>. Commented Jan 18, 2022 at 19:00
  • 1
    @codechimp in MS VC it is also declared in malloc.h but the C standard doesn't mention malloc.h. I don't know what is sufficient to test for function malloc() or stdlib.h hence no answer. With MS VC your _STDLIB_H isn't known whether or not I #include <stdlib.h> It has a header guard but that is only set if the header is included, not whether available. The comment was to show that a conforming implementation does not need to provide malloc(). Commented Jan 19, 2022 at 20:24

1 Answer 1

2

It sounds like what you want to do is produce a single library binary that can be used in both freestanding (embedded) and hosted (non-embedded) environments. This is tricky but possible.

If you are building a static library (which it probably needs to be anyways -- generally shared libraries will not be useful in an embedded environment), you can just use malloc in part of your library and, as long as that part is not used by the application, it will not be included and all will be fine. If an embedded application uses the thing that requires malloc, they'll then get a link-time error if the embedded environment does not provide it.

So what you end up needing to do is offer two entry points for your library -- one that requires malloc and one that does not -- and the user (application writer) needs to call the one that is appropriate for their environment. You need to make sure that these two entry points are included in the library such that only one needs to be linked (generally, just make sure they are in different compilation units).

But I do not want the library itself to load 'stdlib.h'

This statement indicates a fundamental misunderstanding about what is going on. stdlib.h is a header file, not a library, so you do not (and cannot) "load" it. You can include it in your source code, but that just gives you the declarations for what is in the library; it does not include any of the library in your library. If you don't use anything from the header, it will have no effect. If you do use things from the header, that will create dependencies in your library on the standard library that will need to be resolved at (application) link time.

Sign up to request clarification or add additional context in comments.

6 Comments

The point of my question is whether it is garunteed that a malloc deceleration is ALWAYS confined to stdlib.h, even in embedded build system. But @Weather Vane, seemed to answer that question in the form of a comment regarding C18 4.6.
Why do you care if malloc is declared (only) in stdlib.h or not?
@codechimp The compiler can still have the headers there. BUT, it is the developers choice to use them or not. And if they use them, they should also make sure, the heap is actually configured in the linker command file. Because the embedded compiler can be used for both a hosted and freestanding environment still.
@ChrisDodd Because I assume in non-conforming build sys, the library are non-standard (i.e. the 'std' in 'stdlib.h'). I want it to use which ever malloc the end-user chooses to implement/use, based on their particular target platform, and not throw error or warning simply for being there if their target does not support/declare malloc. Equally important, I want to throw error if malloc is NOT supported and they attempt to use the add-on function.
For such a use, you'll need to provide the end user a way to specify the allocation function to use -- you could have them provide a pointer to an allocation function (which could be malloc if that is what they want to use, or might have a different name) and possibly a deallocation function as well
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.