pointers - Can you explain why I get 2 different results passing the same structure in Objective-C? -
have been struggling hope people can me. suppose have defined following structure
struct _sparticleeffect { enumparticletypes type; int count; glkvector3 initialpos; glkvector3 direction; float triggerradius; cgsize prtsize; float prtclspeedmax; float prtclspeedinitial; float prtcllifemax; glkvector4 color; }; typedef struct _sparticleeffect sparticleeffect;
then make instance of structure , pass single function in 2 ways - pointer , value:
- (void) initgeometry { sparticleeffect splasheffect; splasheffect.count = 15; [splashprt testfunc: &splasheffect : splasheffect]; } //test function 2 parameters - pointer , value //method of splashprt - (void) testfunc: (sparticleeffect*) attrpointer: (sparticleeffect) attr { nslog(@"1: %d", attrpointer->count); nslog(@"2: %d", attr.count); }
and result get
2015-07-01 16:58:28.766 [1738:707] 1: 15
2015-07-01 16:58:28.770 [1738:707] 2: 804245704
as can see seems in second case, 'count' lost. worst part result not consistent, because if put count parameter first parameter of structure in both cases result 15. can explain me why or tell way try debug it?
how reproduce error: reproduce create single-view app xcode (6.3.2.) template deplayment target 6.0 , ios base sdk 8.3
no changes in appdelegate.h , appdelegate.m. add new files particleeffect.h , particleeffect.m following content
content of particleeffect.h
#import <foundation/foundation.h> #import <glkit/glkit.h> struct _sparticleeffect { glkvector3 initialpos; int count; glkvector3 direction; float triggerradius; cgsize prtsize; float prtclspeedmax; float prtclspeedinitial; float prtcllifemax; glkvector4 color; }; typedef struct _sparticleeffect sparticleeffect; @interface particleeffect : nsobject { } - (id) initwithattributes:(sparticleeffect *)pointer viacopiedstruct:(sparticleeffect)copiedstruct; @end
content of particleeffect.m
#import "particleeffect.h" @implementation particleeffect - (id) initwithattributes:(sparticleeffect *)pointer viacopiedstruct:(sparticleeffect)copiedstruct; { nslog(@"1: %d", pointer->count); nslog(@"2: %d", copiedstruct.count); self = [super init]; if (self != nil) { } return self; } @end
content of file viewcontroller.h
#import <uikit/uikit.h> #import "particleeffect.h" @interface viewcontroller : uiviewcontroller @end
contents of file viewcontroller.m
#import "viewcontroller.h" @interface viewcontroller () { particleeffect *splashprt; } @property (strong, nonatomic) particleeffect *splashprt; @end @implementation viewcontroller @synthesize splashprt; - (void)viewdidload { [super viewdidload]; sparticleeffect splasheffect; splasheffect.count = 15; splashprt = [[particleeffect alloc] initwithattributes: &splasheffect viacopiedstruct:splasheffect]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end
no other changes in project. run on ipod 5, ios 8.2, appears on other devices too. following output:
2015-07-02 16:06:18.633 teststruct[617:96867] 1: 15 2015-07-02 16:06:18.637 teststruct[617:96867] 2: 715393
the code posted doesn't show you're doing. gives "15" both times.
by way, coding style way off, , you're breaking naming conventions.
this how should written objective-c way, easier read , understand, way.
- (void)initgeometry { sparticleeffect splasheffect; splasheffect.count = 15; [splash testpassviapointer:&splasheffect viacopiedstruct:splasheffect]; } - (void) testpassviapointer:(sparticleeffect *)pointer viacopiedstruct:(sparticleeffect)copiedstruct { nslog(@"1: %d", pointer->count); nslog(@"2: %d", copiedstruct.count); }
in short: method names start lower case letters, nothing abbreviated, parameters/arguments have names.
Comments
Post a Comment