You have a few problems with the following lines:
bufferlist=(KOCTET**)malloc(sizeof(KOCTET*)); bufferlist=&Buffer; lenlist=(KUINT16*)malloc(sizeof(KUINT16)); lenlist=&total_bytes;
The first two allocate memory, then overwrite the pointer with a pointer to a local variable, which will not be valid when the function returns. The same goes for the next two lines. So in these four lines you have two memory leaks (allocating memory and then changing pointers to that memory so that it is no longer available) and undefined behaviors when you indicate that the pointer points to places on the stack that valid only inside the function.
To resolve these issues, change to the following:
*bufferlist = Buffer; *lenlist = total_bytes;
Edit: I also note that you are calling this function incorrectly:
KOCTET**bufferlist; KUINT16*lenlist; process(bufferlist,lenlist);
This should be changed to:
KOCTET *bufferlist; KUINT16 lenlist; process(&bufferlist, &lenlist);
This declares the variables as a pointer to KOCTET and a KUINT16 . Then it passes the addresses of these variables to the process , creating pointers to them (i.e. a pointer to a pointer to KOCTET in the case of bufferlist and a pointer to KUINT16 in the case of lenlist ).
Now you do not need to use lenlist dereferencing in a loop:
while (z < lenlist) { printf(" %02X", (unsigned char) *temp); temp++; z++; }
Now this cycle can be rewritten as follows:
for (z = 0; z < lenlist; z++) printf(" %02X", (unsigned char) bufferlist[z]);
Edit 2: Explain pointers and pointer operators (hopefully)
Let's take this program:
I hope this simple program helps you understand pointers a little more, and especially the difference between the & and * operators.