CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Large blocks of code in if-else/goto

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
notbad



Joined: 10 Jan 2013
Posts: 68

View user's profile Send private message

Large blocks of code in if-else/goto
PostPosted: Wed Apr 17, 2013 3:11 am     Reply with quote

hi
my code is like this:
Code:

if (a>1)
{
  stmt1;
  a large block of code
}
else if (a==1)
{
  if (long expr)
  {
    stmt2;
    the same large block of code
  }
}
...


But those large blocks of code take too much memory and make my code unreadable. Can i use goto like this? Why ccs help says: "The goto's are used sparingly, if at all."
Code:
if (a>1)
{
  stmt1;
  goto yes;
}
else if (a==1)
{
  if (long expr)
  {
    stmt2;
    goto yes;
  }
}
goto no;
yes:
the large block of code
no:
...


Can you suggest another way ? (except declaring a function). I think macros will solve the readability problem but not memory usage. Right?
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed Apr 17, 2013 3:23 am     Reply with quote

Functions are THE way to solve this sort of problem. Whenever code is used twice or more then a function is the way to go. So, why, precisely, are they causing you problems?

Gotos are a tool of last resort, and in thirty years of professional programming I have NEVER used one. I've used many, many functions, and functions of functions, and functions within functions within functions. Provided you are aware of the limited stack space provided by most PICs, there should be very few problems.

What can cause memory bloat issues are where the compiler is forced to generate multiple copies of functions. This happens when they are inlined, or are used in ISRs AND mainline code, which forces multiple copies to prevent re-entrancy which is not possible with many PICs due to hardware limitations, such as the very restrictive stack.

So, what problems are you seeing? If you are considering using gotos then you're clearly in quite a lot of trouble.

RF Developer
temtronic



Joined: 01 Jul 2010
Posts: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Apr 17, 2013 5:17 am     Reply with quote

Instead of using 'if...then...else..) try 'switch'.
It'll make your code easier to read, produce tighter (smaller) code.
Several examples are in the examples folder that CCS supplies, as well as the manual.

hth
jay


btw, RF_D, we've all been using 'hidden' gotos for decades. It's an assembler command.Dump any listing and it'll be there. I think the reason why some programmers don't use it, is it reminds them of BASIC. Kind of a 'Ford vs Chevy' thing.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed Apr 17, 2013 7:15 am     Reply with quote

temtronic wrote:

btw, RF_D, we've all been using 'hidden' gotos for decades.It's an assembler command.Dump any listing and it'll be there.I think the reason why some programmers don't use it,is it reminds them of BASIC.Kind of a 'Ford vs Chevy' thing.


Yes, I am aware that most processors instruction sets use various forms of jumps and relative branches, which are "gotos". The fact that compilers have to implement higher level language control constructs using "goto" style machine code is largely an irrelevance to the argument about the use of gotos in high level languages. So, that is really not a relevant point.

I am largely with Dijkstra on this - the use of gotos as method of program flow of control in high level languages is largely a bad thing, and that's for reasons relevant to human use of those languages, not to the machine translations of the languges to machine code for use on machines.

I don't regard this as what I understand you to mean by a "Ford" v "Chevy" thing (which we don't have here in the UK), i.e. one of personal taste and preference, but as something that's important for code clarity/readability, reduction of likely bugs and hence quickness ot market and finally ease of maintainance not to mention code standardisation. I feel that we should not encourage programmers to use gotos, except in specific and exceptional situations, such as exception style eixting from deeply indented, complex code, which are rare and probably unlikely when programming machines with codespace as limited as PICS.

Anyway, if we're going down the line of "every control construct boils down to gotos anyway", then functions are merely pretty much what the original poster posted in their example code with gotos. There are some differences of course, but essentially procedure calls and returns ar but gotos by another name... which just happen to use a stack or other mechanism to store the return address at runtime.

My main recommendation still stands: use functions. TO the orginal poster: what your concern with using functions?

RF Developer.
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Wed Apr 17, 2013 7:34 am     Reply with quote

It is also critical, to understand that using functions, allows the code to be split up. _unless_ you use functions, you _will_ eventually run into the problem of code being too large to fit in a memory page (except with chips like the DsPIC's).
Splitting the code, rather than being 'monolithic', is an _essential_ part of allowing things to actually fit in the processor's memory.

Best Wishes
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Apr 17, 2013 7:49 am     Reply with quote

The problem I see with GOTOs is maintaining code structure.

I prefer to be able to see all of any one module/function/routine or whatever on ~one screen full.
All modules etc have one input at the top and one return output at the bottom.
That way, I can see the whole of any one module at a glance.
Any calls etc. to other sections are known to return to the next code line.

If your code does a GOTO to A, then A does a GOTO to B, things very quickly get out of hand and the whole thing rapidly degenerates into spaghetti code.
Been there, done that, got the tee-shirt.

I got out of the habit of GOTOs in the dim and distant days of Z80 assembler. The only jumps/branches I allowed myself were within the module being worked on i.e. tight loops within the screenfull I could see. Everything else was a call of some sort, or equivalent. With GOTOs I often had to print out code onto reams of paper, just to be able to follow my own work!

Mike
notbad



Joined: 10 Jan 2013
Posts: 68

View user's profile Send private message

PostPosted: Wed Apr 17, 2013 8:49 am     Reply with quote

Quote:
what your concern with using functions?

I'm migrating from 8051 assembly and I'm having problems understanding a high level language like C.
The concern with using functions is that those codes don't exactly fit in the definition of a function.
I mean they are irrelevant stuff that need to be done.
they can be put together as a function but I feel like its not structured programming!(I'm trying to imitate structured programming!)

Quote:
Been there, done that, got the tee-shirt.

what did the tee-shirt say?! Very Happy
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Wed Apr 17, 2013 11:47 am     Reply with quote

C, is a relatively low level language.
Only a step up from assembler, but aimed at giving you at least reasonable program structure, and pre-written solutions for maths etc..
Most 'high level' languages, are themselves written in something like C now.

If you want to write an object based 'superset' based on this, then do so. Define yourself a structure to contain your 'object'. Include the definitions of the operations. Hand this to your function either 'by reference' (a pointer), or 'by value' (pass the variable itself), and have the function do it's job.

Do a search for XT toolkit. This was a set of C code that would port to CCS fairly easily, which implemented many of the basic OO programming concepts. There is also OOC, which wrote a pretty complete object orientated system in C. However, you seem to be losing the point of C.
C was designed to be nearly as efficient as assembler, to write relatively low level code. Object orientation is a concept designed to improve reliability when working with complex data types and operations. It is not aimed at efficient programming at a low level. It is a complex solution, that is unlikely to suit writing efficient code for hardware. Programmers writing code for things like Windows drivers, don't use OO.

You need though to remember that the PIC does not have a variable stack (except for the DsPIC's). This makes many things done in OO very hard to implement, and not the way you should be thinking for efficient PIC coding.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group