Home > Software engineering >  These two code blocks in C look very similar, but have different effects. What causes the difference
These two code blocks in C look very similar, but have different effects. What causes the difference

Time:10-07

This block has no effect on the result at all, even though I set the values to zero:

int i;
for(i = 0; i < net.n ;   i){
    layer l = net.layers[i];
    if(l.type == CONVOLUTIONAL){
        //save_convolutional_weights(l, fp);
        l.weights_quant_multipler = 0;
    }
}

On the other hand, the values are set to zero, which dramatically changes the result:

int i;
for (i = 0; i < net.n;   i) {
    layer *l = &net.layers[i];

    if (l->type == CONVOLUTIONAL) {

        l->weights_quant_multipler = 0;
    }
}

What causes the difference? Thanks for your help.

CodePudding user response:

The first code block make a copy of net.layers[i] and changes that. The second changes the original net.layers[i] through a pointer.

CodePudding user response:

In the first case, l is a different instance of layer than net.layers[i] - any changes made to l do not affect net.layers[i] (any more than changes to net.layers[i 1] affect net.layers[i].

In the second case, l is a pointer to net.layers[i] - instead of being a separate instance of layer, it's a reference to net.layers[i]).

Writing l->weights_quant_multiplier = 0 is exactly equivalent to writing net.layers[i].weight_quant_multiplier = 0.

 l == &net.layers[i] // layer * == layer *
*l ==  net.layers[i] // layer   == layer
  •  Tags:  
  • c
  • Related