Thread: program design question

  1. #1
    Registered User
    Join Date
    Mar 2004

    program design question

    I have this program, dealing with sockets and threading (which is not of concern). There is a main loop inside this program. Currently, there is a lot of messy code inside here. I'd like to sort this out into a few functions, declared in a header file. The problems are:

    1. The functions I've wrote use lots of variables from the main program (is there a way to fix this, save for passing 3-6 arguements into each function? most of the variables are already global in the main program)

    2. The functions have certain circumstances when they need to break or continue the original main loop from the main program. .. Heh.. Any ideas?

    I guess I COULD just make a long, ugly, unorganized, cryptic, single-filed program... Or include ALL the function declarations inside that source (to fix the variable problem).

    Are there any tips/tricks to get around situations like this? Thanks for any input...

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    use a class?
    Join is in our Unofficial Cprog IRC channel
    Channel: #Tech

    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    echo major,

    designing with classes will give you several advantages: clear code, portability, security.

    some entropy with that sink?

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    1. You can pass-in a pointer to a structure (or class) that contains the needed variables (or accessor functions). This is quite common when you need to pass-in several variables, especially when the same variables are used by many different functions.

    2. I'm not sure I understand, but maybe you can use the return value from the function. i.e. if 0 is returned, continue, if 1 is returned, break, etc. But, I suppose some of the functions already have a return value used for something else. You could use a pointer or reference to some status variable.

    Unfortionately, the above approaches might requre you to restructure and rewrite the whole program!

  5. #5
    Registered User
    Join Date
    Mar 2004
    Thanks for the ideas.. I think I'll get it running. Classes don't really seem to fit the problem, however I certainly will think about it more.

    DougdBug - The structure idea is a very good one. I've already done both clauses in #2; both worked/didn't work. It's hard to say; I'm going for another rewrite tomorrow.

    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program crashes + fscanf() question
    By happyclown in forum C Programming
    Replies: 27
    Last Post: 01-16-2009, 03:51 PM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM