开发者

Rewriting a returned pointer to an output parameter

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*)out开发者_运维知识库Len;

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜