通过引用其他函数来传递缓冲区 [英] Pass a buffer by reference to other function

查看:121
本文介绍了通过引用其他函数来传递缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将缓冲区的指针和缓冲区的长度传递给其他函数,然后使用这些数据进行操作并打印出来。但是,当我尝试打印它的功能是不可能的。



这是我的代码的一部分:

  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;

//以十六进制打印缓冲区
int z = 0;
temp =缓冲区;
while(z printf(%02X,(unsigned char)* temp);
temp ++;
z ++;
}
printf(\\\
);
}


void function()
{
KOCTET ** bufferlist;
KUINT16 * lenlist;

process(bufferlist,lenlist);

//以十六进制打印缓冲区
int z = 0;
temp = * bufferlist;
while(z <(* lenlist)){
printf(%02X,(unsigned char)* temp);
temp ++;
z ++;
}
printf(\\\
);
}

谢谢,

  bufferlist =(KOCTET *)解决方案

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

前两个分配内存,然后用指针覆盖指针一个局部变量,当函数返回时它不会有效。接下来的两行也一样。因此,在这四行中,当您将指针设置为指向位于其上的位置时,会出现两次内存泄漏(分配内存然后更改指向该内存的指针,以至于不再可用)以及导致未定义行为只有在函数内才有效。



要解决这些问题,请更改为以下内容:

  * bufferlist = Buffer; 
* lenlist = total_bytes;

编辑:我也注意到你正在调用这个函数:

  KOCTET ** bufferlist; 
KUINT16 * lenlist;

process(bufferlist,lenlist);

这应该更改为:

  KOCTET * bufferlist; 
KUINT16 lenlist;

过程(& bufferlist,& lenlist);

这将变量声明为指向 KOCTET KUINT16 。然后将这些变量的地址传递给 process ,从而指向它们的指针(即指向指向 KOCTET 的指针 bufferlist ,以及 lenlist KUINT16 的指针>)。

现在,您不需要在循环中使用 lenlist 的解引用:

  while(z  printf(%02X,(unsigned char)* temp); 
temp ++;
z ++;

$ / code>

现在这个循环可以被重写为:$ b $ (z = 0; z printf(%02X,(unsigned char)bufferlist [z])b

  ; 

编辑2:解释指针和指针运算符(我希望)



让我们看一下这个示例程序:

  #include  
int main()
{
int a = 5; / *声明一个变量,并将该值设置为5 * /

int * p =& a; / *声明一个指针,并使它指向`a`的位置* /
/ *`p`的实际值是`a`的地址* /

printf (a的值:%d \ n,a); / *将打印5 * /
printf(p的值:%d \ n,p); / *将打印一个大数字* /

printf(a:的地址:%d \\\
,& a); / *打印与上面'p'相同的大号* /
printf(The contents p:%d \ n,* p); / *打印5 * /

返回0;
}

我希望这个简单的程序能帮助您更多地理解指针,特别是& * 运营商之间的差异。


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 (i.e. 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.

这篇关于通过引用其他函数来传递缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆