0

I've got a project with a pure c code and c code handled by the ObjC compiler [.m file]. The one handled by ObjC compiler has a following class:

unsigned long getMessage(char ** message) {
 *message = (char*)calloc(1, [dMessage bytes], [dMessage length]);
 memcpy(message, [dMessage bytes], [dMessage length])
return [dMessage length];
}

dMessage is an NSData object filled with text.

On C side, I do:

char* msg = NULL
unsigned long length = getMessage(&msg)

After the call, msg is empty, but the length variable is set to correct size.

What should I do to pass char* between objc and c?

Thank you

2
  • What is getMessage really doing? The data is already allocated in the NSData object so just keep it around and access the raw bytes via [dMessage bytes]. Commented Nov 1, 2015 at 0:58
  • It's a function written in C, accessing some data which is stored in database, which I access usinc ObjC Library and code. I have to pass it on to a library written in pure C and I don't want to change that lib, want to keep it as close to original as possible. Commented Nov 1, 2015 at 1:10

2 Answers 2

1

You're passing the wrong arguments to calloc(). It takes two arguments, but you're passing three. The compiler should be screaming at you about that.

Since the second argument is the bytes pointer from the NSData, you're effectively requesting some huge allocation. It's probably failing. There would usually be a message logged to the console about that failure.

You want:

*message = (char*)calloc([dMessage length], 1);
Sign up to request clarification or add additional context in comments.

1 Comment

Hmm no, there's no message, code compiles and "runs", but doesn't work. The code you proposed didn't solve it.
0

It was a mistake in my code.

I should havecalled

memcpy(*message, [dMessage bytes], [dMessage length])

instead of

memcpy(message, [dMessage bytes], [dMessage length])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.