5

This must be very simple, but I can't figure out how to do this: I have a C-function to monitor current memory usage:

natural_t report_memory(void) {
    struct task_basic_info info;
    mach_msg_type_number_t size = sizeof(info);
    kern_return_t kerr = task_info(mach_task_self(),
                               TASK_BASIC_INFO,
                               (task_info_t)&info,
                               &size);
    if( kerr == KERN_SUCCESS ) {
        return info.resident_size;
    } else {
        NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
        return 0;
    }
}

Now, I would like to use it. How do I declare it in the .h? I tried the (for me) obvious within the objective c methods:

natural_t report_memory(void);

Calling this somewhere in the code:

NSLog(@"Memory used: %u", rvC.report_memory());

The Compiler complains error: called object is not a function. Thus, I assume, the declaration is somehow wrong. I tried several options, but the best I could get was a runtime error... How to fix this?

5
  • Did you declare it as an instance method ? Commented Jan 21, 2011 at 17:15
  • And what is the type of rvC? Commented Jan 21, 2011 at 17:15
  • rvC is the RootViewController, where the code resides. Commented Jan 21, 2011 at 17:25
  • I probably would want to have an instance method? Or is it possible to use the code from a class method? Commented Jan 21, 2011 at 17:26
  • Since the function has no relation whatsoever to view controllers, I would keep it as a C function (declared in a separate header file, defined in a separate implementation file). You can use it from instance or class methods. Commented Jan 21, 2011 at 17:32

2 Answers 2

10
rvC.report_memory()

should be replaced with

report_memory()

since it is a C function.

Sign up to request clarification or add additional context in comments.

2 Comments

but I want to use it from different classes
@angrest C functions are independent of classes. Simply call report_memory() wherever you need it.
2

If you want to use this function in other modules, you should also put in your header (.h) file this line

extern natural_t report_memory(void);

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.