ios – obj-c return native array

[ad_1]

I am new to obj-c.After I research about array I discover one thing fascinating,after I run following code:

#import <Basis/Basis.h>

@interface TestClass:NSObject

-(int*)generateIntArray;
@finish

@implementation TestClass

-(int*)generateIntArray{
   int  r[5];
   int i;
   /* set the seed */
   srand( (unsigned)time( NULL ) );
   for ( i = 0; i < 5; ++i) {
      r[i] = rand()%100;
      NSLog( @"r[%d] = %dn", i, r[i]);
   }
   //NSLog(@"addr1=%xn",r);
   return r;
}
@finish

int foremost(){
    TestClass *check = [[TestClass alloc]init];
    int *a=[test generateIntArray];
    NSLog(@"addr2=%xn",a);
    for(int i=0;i<5;i++){
        NSLog(@"a[%d] = %dn", i, a[i]);
    }  
}

I get consequence:

2022-04-19 14:59:04.695 array.exe[9916:9916] r[0] = 50
2022-04-19 14:59:04.695 array.exe[9916:9916] r[1] = 43
2022-04-19 14:59:04.695 array.exe[9916:9916] r[2] = 99
2022-04-19 14:59:04.695 array.exe[9916:9916] r[3] = 94
2022-04-19 14:59:04.695 array.exe[9916:9916] r[4] = 59
2022-04-19 14:59:04.695 array.exe[9916:9916] addr2=7e8c5930
2022-04-19 14:59:04.695 array.exe[9916:9916] a[0] = 8
2022-04-19 14:59:04.695 array.exe[9916:9916] a[1] = 48
2022-04-19 14:59:04.695 array.exe[9916:9916] a[2] = 2123127136
2022-04-19 14:59:04.695 array.exe[9916:9916] a[3] = 32765
2022-04-19 14:59:04.695 array.exe[9916:9916] a[4] = 2123126912

I do know that is appropriate as a result of I go native variable to a.

However after I uncomment NSLog in operate generateIntArray I get consequence like:

2022-04-19 15:00:08.052 array.exe[9986:9986] r[0] = 38
2022-04-19 15:00:08.052 array.exe[9986:9986] r[1] = 45
2022-04-19 15:00:08.052 array.exe[9986:9986] r[2] = 46
2022-04-19 15:00:08.052 array.exe[9986:9986] r[3] = 94
2022-04-19 15:00:08.052 array.exe[9986:9986] r[4] = 32
2022-04-19 15:00:08.052 array.exe[9986:9986] addr1=537b9050
2022-04-19 15:00:08.052 array.exe[9986:9986] addr2=537b9050
2022-04-19 15:00:08.052 array.exe[9986:9986] a[0] = 38
2022-04-19 15:00:08.052 array.exe[9986:9986] a[1] = 45
2022-04-19 15:00:08.052 array.exe[9986:9986] a[2] = 46
2022-04-19 15:00:08.052 array.exe[9986:9986] a[3] = 94
2022-04-19 15:00:08.052 array.exe[9986:9986] a[4] = 8

the fist 4 component weren’t been destroyed,solely the final component modified.
can somebody inform my why?
thanks quite a bit

[ad_2]

Leave a Reply