How do I pre-allocate an output buffer for the decoder?

To pre-allocate your own buffer, you should:

  1. declare a pointer that will reference the buffer you wish to allocate. (This pointer can either be of type void or of the compiler-generated structure's type that you wish to decode.)
  2. use a memory allocating function (such as malloc() or calloc()) to allocate the needed memory buffer and set the declared pointer to reference the beginning of this new buffer.
  3. call the ossSetDecodingLength() function passing it the length of your allocated buffer so that the decoder will be aware of its size.
  4. call ossDecode() with the last parameter set to the address of the pointer to the buffer provided, and it will use your allocated buffer to store its output.

This procedure is illustrated using a void pointer as follows:

void *DecodedDataPtr = NULL; /* address of decoded data */;
. . . .
DecodedDataPtr = (void*)malloc(1024*sizeof(char));
ossSetDecodingLength(world, 1024);
rc = ossDecode(world, &myPduNum,
&encodedData, (void **)&DecodedDataPtr);

or equivalently, if you want to use a non-void pointer:

MyASN1DataType *DecodedDataPtr = NULL; /* address of decoded data */;
. . . .
DecodedDataPtr = (MyASN1DataType*)malloc(1024*sizeof(char));
ossSetDecodingLength(world, 1024);
rc = ossDecode(world, &myPduNum,
&encodedData, (void **)&DecodedDataPtr);

where MyASN1DataType is the compiler-generated data type intended for decoding.

Note: In the above examples, we allocate an output buffer with a capacity of 1024 bytes.


(a) Two common errors are not allocating enough space for the output buffer and not calling the ossSetDecodingLength() function before calling the decoder.

(b) If you want to allocate space for a compiler-generated C struct, it is not enough to just perform a sizeof() operation on the compiler-generated data type, since a sizeof() operation will not account for the space needed for subsidiary buffers referenced by pointer fields (e.g., char* fields).

(c) Please note that when allocating storage from a user-defined output buffer the decoder always does alignment on the strictest alignment boundary for the machine (e.g., 8-byte boundary for Solaris SPARC). This means that (on Solaris SPARC) if a C struct simply contains a 4 byte pointer to a 2 byte integer, the amount of memory that must be provided by the user is 16 bytes, not 6 bytes as it might at first appear. To determine the number of bytes needed for the buffer, you can first call ossDecode() with the last parameter as a pointer to a NULL pointer forcing the decoder to allocate the output memory for you. You then call ossGetDecodingLength() that would return the number of bytes needed to hold the entire decoded PDU tree. You can use that number to allocate the output buffer for the decoder.


The samples included with some of the Knowledge Center answers are meant for your general understanding of the OSS products. Different versions of the products might produce slightly different outputs. Consult the products documentation and samples for the most up-to-date products information and code examples.

Contact Support
contact Our office hours
24 hours/day, 7 days/week

  • Phone: 1-888-OSS-2761 (USA and Canada)
  • Phone: 1-732-302-9669 (International)
  • Fax: 1-732-302-0023
  • Email:
Free Trial

Test drive the OSS Nokalva ASN.1, 4G and XML Tools now! Your trial includes complete software, documentation, sample programs, free 24x7 technical support and more.

Learn ASN.1
Learn ASN.1

Our expert personnel can help you learn ASN.1!

We offer 4-day ASN.1 courses at our headquarters or your premises.