![]() |
![]() |
![]() |
![]() |
![]() |
This version of this document is no longer maintained. For the latest documentation, see http://www.qnx.com/developers/docs. |
Allocate memory
#include <stdlib.h> void* malloc( size_t size );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The malloc() function allocates a buffer of size bytes.
If size is zero, malloc() returns a valid non-NULL pointer which is valid only to a corresponding free() call. You shouldn't assume that a pointer returned by malloc(0) actually points to any valid memory.
![]() |
This function allocates memory in blocks of _amblksiz bytes (a global variable defined in <stdlib.h>). |
A pointer to the start of the allocated memory, or NULL if an error occurred (errno is set).
#include <stdlib.h>
int main( void )
{
char* buffer;
buffer = (char* )malloc( 80 );
if( buffer != NULL ) {
/* do something with the buffer */
...
free( buffer );
}
return EXIT_SUCCESS;
}
| Safety: | |
|---|---|
| Cancellation point | No |
| Interrupt handler | No |
| Signal handler | No |
| Thread | Yes |
In QNX 4, nothing is allocated when you malloc() 0 bytes. Be careful if your code is ported between QNX 4 and QNX Neutrino.
calloc(), free(), realloc(), sbrk()
The Heap Analysis: Making Memory Errors a Thing of the Past chapter of the Neutrino Programmer's Guide.
![]() |
![]() |
![]() |
![]() |