mercredi 1 juillet 2015

Can you explain why I get 2 different results passing the same structure in Objective-C?

Have been struggling with this so hope people can help me. Suppose I 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 I make an instance of this structure and pass it to single function in two ways - pointer and by value:

- (void) InitGeometry
{
    SParticleEffect splashEffect;
    splashEffect.count = 15;
    [splashPrt TestFunc: &splashEffect : splashEffect];
}

//test function with 2 parameters -  by pointer and by value
//method of splashPrt
- (void) TestFunc: (SParticleEffect*) attrPointer: (SParticleEffect) attr
{
    NSLog(@"1: %d", attrPointer->count);
    NSLog(@"2: %d", attr.count);
}

And this is the result I get

2015-07-01 16:58:28.766 [1738:707] 1: 15

2015-07-01 16:58:28.770 [1738:707] 2: 804245704

As you can see it seems like in second case 'count' is lost. The worst part is that this result is not consistent, because if I put this count parameter as first parameter of structure then in both cases result is 15. Can you explain to me why this is or tell some way to try to debug it?

Aucun commentaire:

Enregistrer un commentaire