Pass the buffer by reference to another function

I want to pass the buffer pointer and buffer length to another function, and then work with this data and print it, for example. But when I try to print it in a function, this is not possible.

This is part of my code:

void process(KOCTET**bufferlist,KUINT16*lenlist){ KOCTET * Buffer,*temp; KUINT16 BufferSize=5000; KUINT16 WritePos=0; KUINT16 total_bytes; Buffer=(KOCTET*)malloc(5000*sizeof(KOCTET)); total_bytes = stream.CopyIntoBuffer( Buffer, BufferSize, WritePos); bufferlist=(KOCTET**)malloc(sizeof(KOCTET*)); bufferlist=&Buffer; lenlist=(KUINT16*)malloc(sizeof(KUINT16)); lenlist=&total_bytes; //Print Buffer in Hexadecimal int z=0; temp=Buffer; while (z<total_bytes){ printf(" %02X",(unsigned char)*temp); temp++; z++; } printf("\n"); } void function () { KOCTET**bufferlist; KUINT16*lenlist; process(bufferlist,lenlist); //Print Buffer in Hexadecimal int z=0; temp=*bufferlist; while (z<(*lenlist)){ printf(" %02X",(unsigned char)*temp); temp++; z++; } printf("\n"); } 

Thanks,

0
source share
1 answer

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:

 #include <stdio.h> int main() { int a = 5; /* Declares a variable, and set the value to 5 */ int *p = &a; /* Declares a pointer, and makes it point to the location of `a` */ /* The actual value of `p` is the address of `a` */ printf("Value of a: %d\n", a); /* Will print 5 */ printf("Value of p: %d\n", p); /* Will print a large number */ printf("The address of a: %d\n", &a); /* Prints the same large number as `p` above */ printf("The contents p: %d\n", *p); /* Prints 5 */ return 0; } 

I hope this simple program helps you understand pointers a little more, and especially the difference between the & and * operators.

+6
source

All Articles