将返回的指针重写为输出参数 [英] Rewriting a returned pointer to an output parameter
问题描述
我正在玩OpenSSL库,它需要我刷新指针,并且遇到了困难。
我有一个objective-c (unsigned char *)encryptTake1:(unsigned char *)输入inputLength:(int)inLen outputLength:(int())方法:
*)outLen;
它需要一些数据,对它进行加密并返回一个指向数据的指针和数据长度一个输出参数。
我想改变它,以便加密数据也作为输出参数处理,返回值用于指示成功或失败。这是我的:
$ b $ pre $ - (int)encryptTake2 :(无符号字符*)输入inputLength:(int)inLen输出: (unsigned char *)output outputLength:(int *)outLen;
这不起作用。我究竟做错了什么?我认为问题在于(unsigned char *)
是错误的。如果(unsigned char *)
不对,那么我认为我还需要改变方法中引用 output
的方式。如何?
这取决于您如何处理内存分配。
-encryptTake1:
是否返回?如果它返回一个新分配的缓冲区,调用者必须释放这个缓冲区,那么你可以在encryptTake2中使用 unsigned char **
:
<$ (unsigned char *)输入inputLength:(int)inLen输出:(unsigned char **)outputPtr outputLength:(int *)outLen
{$($) b $ b * outputPtr = malloc(1024);
unsigned char * output = * outputPtr;
strcpy(输出,hello);
...
}
I'm playing around with the openSSL library and it requires me to brush up on pointers, and I'm having difficulty.
I have a objective-c method:
-(unsigned char *)encryptTake1:(unsigned char *)input inputLength:(int)inLen outputLength:(int*)outLen;
It takes some data, encrypts it and returns a pointer to the data and the length of the data as an output parameter.
I want to change this so that the encrypted data is also handled as an output parameter and the return value is used to indicate success or failure. This is what I have:
-(int)encryptTake2:(unsigned char *)input inputLength:(int)inLen output:(unsigned char *)output outputLength:(int*)outLen;
This doesn't work. What am I doing wrong? I think the problem is that (unsigned char *)
is the wrong. If (unsigned char *)
is wrong then I presume I will also need to change how output
is referenced within the method. How?
It depends on how you are handling memory allocation.
What does -encryptTake1:
return? If it returns a newly allocated buffer that the caller must free then you would use unsigned char**
in encryptTake2:
-(int)encryptTake2:(unsigned char *)input inputLength:(int)inLen output:(unsigned char **)outputPtr outputLength:(int*)outLen
{
*outputPtr = malloc(1024);
unsigned char* output = *outputPtr;
strcpy(output, "hello");
...
}
这篇关于将返回的指针重写为输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!