简体   繁体   中英

Pass a buffer by reference to other function

I want to pass a pointer of buffer and length of the buffer to other function and then operate with this data and print it for example. But when I try to print it in function is impossible.

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");
    }

Thank you,

You have several problems with the following lines:

bufferlist=(KOCTET**)malloc(sizeof(KOCTET*));
bufferlist=&Buffer;
lenlist=(KUINT16*)malloc(sizeof(KUINT16));
lenlist=&total_bytes;

The first two allocates memory, then overwrites the pointer with a pointer to a local variable, which will not be valid when the function returns. The same for the next two lines. So in these four lines you have two memory leaks (allocating memory then changing the pointers to that memory so it's not available anymore) and to causes of undefined behavior when you set the pointer to point to places on the stack that are only valid inside the function.

To fix these problems, change to the following:

*bufferlist = Buffer;
*lenlist = total_bytes;

Edit: I also note that you are calling this function wrong:

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 passes the addresses of these variables to process , making pointers of them (ie pointer to pointer to KOCTET in the case of bufferlist , and pointer to KUINT16 in the case of lenlist ).

Now you don't need to use dereferencing of lenlist in the loop:

while (z < lenlist) {
    printf(" %02X", (unsigned char) *temp);
    temp++;
    z++;
}

This loop can now actually be rewritten as:

for (z = 0; z < lenlist; z++)
    printf(" %02X", (unsigned char) bufferlist[z]);

Edit 2: Explaining pointers and the pointer operators (I hope)

Lets take this sample 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 understanding pointers a little more, and specially the difference between the & and * operators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM