When I declare my variables, should be it be like int delay = 0 then initialize to int delay = 0 or int delay then initialize to int delay = 0?

I'm a bit confused about initializing my variables...

void calc_pipe() {
	/* Declare local vars */
	int i;
	int n;
	int k;
	int dep = 0;
	int delay = 0;	
	int prev_delay = 0;
	int total_delay = 0;

	
	/* Check for dependencies */
	/* For-loop: i = 2 to n */
	for(i = 2; i <= n; i++) {
		[B]/* Initialize local vars */[/B] <------ HERE
		delay = 0;
		
		/* For-loop: k = i - 2 to i - 1 */
		for(k = i - 2; k <= i - 1; k++) {
			[B]/* Initialize local vars */[/B] <------ HERE
			if((set[k].dest == set[i].src1) || (set[k].dest == set[k].src2)) { /* RAW dependency */
				dep = 1; /* ????? */
				
				if(i - k == 2) {
					if(prev_delay == 1) {
						delay = 0;
						prev_delay = 0;
					}
					else{
						delay = 1;
						prev_delay = 1;
					}
				}
				else{
					delay = 2;
					prev_delay = 1;
				}
			}
			else{ /* No RAW dependency */
				if(dep == 0)
					prev_delay = 0;
				else
					dep = 0;
			}
		} /* End of k-loop */
		set[i].delay = delay + 1 + set[i - 1].delay;
		total_delay += delay + 1;
	} /* End of i-loop */

Recommended Answers

All 8 Replies

int delay = 0; initializes the variable when it is declared delay = 0; is done when you need to re-initialize the variable, such as when its used within a loop as you posted so that it can be re-used for something.

Does it make a difference if it's declared as int delay = 0; than int delay; ?

Yes. And no. It depends. All you really need to understand is int delay; creates a variable with an unknown value. Don't use it without setting a value. int delay = 5; creates a variable with a known value. delay = 5; sets an already created variable with a known value.

Then it's legal to declare [IQUOTE]int delay;[/IQUOTE] then initialize [IQUOTE]delay = 0;[/IQUOTE] in a loop? Since it set a value in the loop...

commented: Couldn't you see that [iquote] wasn't right? -4

Yes, that is ok. And its [icode] , not IQUOTE

Whoops, typo. Last question, how do I know where to initialize the variables? In other words, why is delay = 0; initialized in "for-loop: i" rather than "for-loop: k"?

for(i = 2; i <= n; i++) {
		/* Initialize local vars */
		delay = 0;
		
		/* For-loop: k = i - 2 to i - 1 */
		for(k = i - 2; k <= i - 1; k++) {
			/* Initialize local vars */
			if((set[k].dest == set[i].src1) || (set[k].dest == set[k].src2)) { /* RAW dependency */
				dep = 1; /* ????? */
				
				if(i - k == 2) {
					if(prev_delay == 1) {
						delay = 0;
						prev_delay = 0;
					}
					else{
						delay = 1;
						prev_delay = 1;
					}
				}
				else{
					delay = 2;
					prev_delay = 1;
				}
			}
			else{ /* No RAW dependency */
				if(dep == 0)
					prev_delay = 0;
				else
					dep = 0;
			}
		} /* End of k-loop */
		set[i].delay = delay + 1 + set[i - 1].delay;
		total_delay += delay + 1;
	} /* End of i-loop */

Last question, how do I know where to initialize the variables?

You initialize a variable where you require it to be a specific value.

In other words, why is delay = 0; initialized in "for-loop: i" rather than "for-loop: k"?

Study the loops. What happens if you initialize it in the K loop? Does the code still work? If not, that's not a good place.

You have to analyze the code to determine the best place.

I understand the whole initializing basics and where to put 'em now. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.