0

这是代码:

#import <Foundation/Foundation.h>
#import "microsoft.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int a,b,sum,mul1,mul2;
        microsoft *m1=[microsoft alloc];
        NSLog(@"\nEnter first variable : ");
        scanf("%d",&a);
        NSLog(@"\nEnter first variable : ");
        scanf("%d",&b);
        [m1 sumvar1:a var2:b];
        sum=[m1 getsum];

        mul1=[m1 mulvar1:3 var2:3]; // Error Line -----
        mul2=[m1 getmul];         

        [m1 name:@"hello"];

        NSLog(@"\nName : %@ \n Sum = %d \n Mul = %d",[m1 name],sum,mul1);
    }
    return 0;
}

调试导航器 - 线程 1

libdyld.dylib`start:
0x7fff91f6d7e1:  movl   %eax, %edi
0x7fff91f6d7e3:  callq  0x7fff91f6d808            ; symbol stub for: exit
0x7fff91f6d7e8:  hlt    

调试导航器 - 线程 2

libsystem_kernel.dylib`kevent:
0x7fff9427ad0c:  movl   $33554795, %eax
0x7fff9427ad11:  movq   %rcx, %r10
0x7fff9427ad14:  syscall
0x7fff9427ad16:  jae    0x7fff9427ad1d            ; kevent + 17
0x7fff9427ad18:  jmpq   0x7fff9427b4d4            ; cerror_nocancel
0x7fff9427ad1d:  ret    
0x7fff9427ad1e:  nop    
0x7fff9427ad1f:  nop    

为什么这个语句mul1=[m1 mulvar1:3 var2:3]会导致崩溃?

4

1 回答 1

2

看来您的microsoft实例未初始化:调用alloc为实例提供内存,但未设置实例本身。在创建 的调用中替换alloc为,如下所示:newm1

microsoft *m1=[microsoft new];
于 2013-06-01T09:53:38.853 回答