Thread: help with strategy to tackle a project

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    31

    help with strategy to tackle a project

    Hello,

    I am hoping someone may have a suggestion as to what is the best way to do what I need to do.

    I am writing a program to simulate a physical system. The simplest analogy of what I need to do would be to simulate random walkers flowing down a tube. This, in itself, is relatively easy. However, there are two complicating factors. First, I need to generate new random walkers at regular intervals (of simulated time) at one end of the tube. Second, I need to monitor them while they are inside the tube, and then forget about them when they exit through the other end of the tube.

    The way I would do it is with a vector of structs (sloppy code follows)

    Code:
    typedef struct _walkerType
    {
        ... stuff I need for the walkers ...
        int       activeFlag;
    } walkerType;
    
    std::vector< walkerType > walkers;
    And then, down in the code
    Code:
    if ( time_is_right )
    {
        generateRandomInitialWalker();        // call it walker0, with activeFlag = 1
        walkers.push_back(walker0)
    }
    
    for ( i=0; i<walkers.size(); i++ )
    {
        if ( walkers[i].activeFlag )
        {
             do_stuff();
        }
    
        if ( reach_other_end ) 
        {
             walker[i].activeFlag = 0;
        }
    }
    
    time += delta_time;
    While I think this would work, I can see two big problems.

    1) After a while of running, most of the walkers would be inactive, yet I still have to loop through them

    2) the vector could potentially become huge, gulping loads of memory.

    Does anyone have any suggestion as to a better way to implement this?

    Thanks a lot,

    mc61

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by mc61 View Post
    Hello,

    I am hoping someone may have a suggestion as to what is the best way to do what I need to do.

    I am writing a program to simulate a physical system. The simplest analogy of what I need to do would be to simulate random walkers flowing down a tube.
    Does the ordering of walkers remain constant? i.e., is this a first in, first out tube? If so, there is a standard container that would be a better choice than std::vector (hint).

    Also, I'd consider a container of smart pointers (like boost::shared_ptr) instead of a container of objects, unless you don't mind all of the copying.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    31
    No, the walkers will exit the tube at a different order than they were created.

    While I have a little experience in C, I am totally new to C++. Could you tell me where I could read about the smart pointers you talk about?

    Thanks,

    mc61

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by mc61 View Post
    No, the walkers will exit the tube at a different order than they were created.

    While I have a little experience in C, I am totally new to C++. Could you tell me where I could read about the smart pointers you talk about?
    http://boost.org/doc/libs/1_35_0/lib.../smart_ptr.htm

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    TR1 implementations also have boost's smart pointers, so if you use a compiler with TR1 support (Visual Studio 2008 Professional or GCC I would believe, among some), you could that as well (std::tr1::shared_ptr, for example).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by mc61 View Post
    While I think this would work, I can see two big problems.

    1) After a while of running, most of the walkers would be inactive, yet I still have to loop through them

    2) the vector could potentially become huge, gulping loads of memory.

    Back to this part of the problem, you can erase (remove) elements from standard containers.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    31
    Thanks for all the suggestions. I am just learning how to erase elements from standard containers, together with a few other things you can do with them that I did not know. I think I'll manage.

    Thanks again,

    mc61

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM