how to structure without a goto

Jul 23, 2010 at 4:08pm
I am using a DSP and am trying to be conservative on memory. I have this fairly big code block which I need to use 2 times within main program (the stuff within the if block):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
	if(calbp<8){
		probecals=EEaddP[3];						
		usercals=EEaddP[4];							
		cboa=calbp*0x100+0x100;						//block pointer
//Grab previous calibration block
		EEaddP=setadd13_19(0x90010000+cboa);		//Set pointer to Calibration settings block
		lrcr=EEaddP[cboa+1];
		hrcr=EEaddP[cboa+2];
		for (lus1=3;lus1<8;lus1++)
			measlo[lus1-3]=(float)EEaddP[cboa+lus1];//get lo measurements
		for (lus1=8;lus1<13;lus1++)
			meashi[lus1-8]=(float)EEaddP[cboa+lus1];//get high measurements
		for (lus1=13;lus1<18;lus1++)
			callo[lus1-13]=(int)EEaddP[cboa+lus1];	//get lo rnage calibration numbers	
		for (lus1=18;lus1<23;lus1++)
			calhi[lus1-18]=(int)EEaddP[cboa+lus1];	//store hi range calibration numbers	
		lus1=EEaddP[cboa+23];						//get high word of ref comp
		lus2=EEaddP[cboa+24];						//get low word of ref comp
		li=lus2+lus1*10000;							//recompose refcomp
		refcomplo=(float)li/100000;
		lus1=EEaddP[cboa+25];						//get high word of ref comp
		lus2=EEaddP[cboa+26];						//get low word of ref comp
		li=lus2+lus1*10000;							//recompose refcomp
		refcomphi=(float)li/100000;
		for (y=0;y<lrcr;y++){						//calc slopes and intercepts for low range based on EEPROM data
			slopelo[y]=(float)(callo[y]*10-callo[y+1]*10)/(measlo[y]-measlo[y+1]);	//Calc slope
			interceptlo[y]=(float)(callo[y]*10)-(measlo[y])*slopelo[y];				//Calc intercept
		}
		for (y=0;y<hrcr;y++){						//calc slopes and intercepts for high range based on EEPROM data	
			slopehi[y]=(float)(calhi[y]-calhi[y+1])/(meashi[y]-meashi[y+1]);	//Calc slope
			intercepthi[y]=(float)(calhi[y])-(meashi[y])*slopehi[y];			//Calc intercept
		}
		calibrated=1;								//set calibrated flag good
	}


My issue is if I use a function/subroutine, I have so many variables to extract out of it which makes it seem pointless. What is the most effective way. I was thinking about separating this block of code and using the evil goto in this case. Any suggestions. Thanks in advance.
Jul 23, 2010 at 4:14pm
If you have many variable to extract you could use a class/struct to hold the variables. Alternatively use a loop, since you have to run it twice. Whatever you do DO NOT USE GOTO.
Jul 23, 2010 at 4:39pm
Could you show us where you'd put the goto and label?
Jul 23, 2010 at 4:59pm
in reply to Spaggy:
I would have to run it in 2 different areas of the program one at the very beginning for initialization and one if they want to recall a different block of data through a menu sequence, so a loop option is not an alternative.


In reply to Helios
regarding the Goto, I have never used one yet. although, I am a beginning programmer, I have been able to stay away from them so far.

I do see examples, and see a return at the end of the goto label loop, will that return you to the place that the goto was executed?

Could I make all the variables needed in a global kind of mode so that they will be updated within the function call and be available for the main. I have done this already and am familiar with it. I have never worked with structures yet and feel a little hesitant about trying to invoke one.
Last edited on Jul 23, 2010 at 5:03pm
Jul 23, 2010 at 5:41pm
Are all of the variables that you want to use of the same type, if they are you could pass a pointer to that type to a function and the function can write the data into the memory allocated for that pointer. Alternatively, you could create a namespace to put the global variables in, but don't do using namespace yourNamespaceName.

Are you using C or C++ for this?
Jul 23, 2010 at 5:42pm
I do see examples, and see a return at the end of the goto label loop, will that return you to the place that the goto was executed?
No. In older languages such as BASIC, there was a command called gosub which had the same behavior as goto, with the exception that it would return control to the statement that followed the gosub when it found a return statement.

You definitely need a function. There are few cases when using global data is a good idea, but I don't think this is one of them. Judging by what you've told us, I think the best way to organize this would be to put the data in a class and have member functions modify its contents.
Jul 23, 2010 at 5:55pm
I am using C.

I will attempt to use a class, I guess.

Last edited on Jul 23, 2010 at 5:58pm
Jul 23, 2010 at 6:31pm
I am using Code Composer 3, a Texas Instruments environment for DSP controllers.

It turns out I cannot use a class, as it is not supported. However, I can use a structure, would this work.
Jul 23, 2010 at 7:33pm
Doesn't C and C++ code integrate seamlessly? :p

If you'd use a C++ compiler/IDE you could have the comfort of your C knowledge while getting the power of classes, I guess.
Jul 23, 2010 at 9:10pm
Yeah, sure, a struct works, too. Since classes aren't supported, member functions are probably not supported, either, so you'll have to use normal functions and pass a pointer to the struct.
Topic archived. No new replies allowed.