Thread: problem with subroutine

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    6

    problem with subroutine

    I have select() that works with two sockets, socketA and socketB.
    When there is data pocket on socketA the data is being processed by the subroutine that returns a value and also stores a carry value to be applied to the next data pocket from socketA the next time subroutine is called. The same goes for socketB. The problem is that everything works fine when there is only data on either socketA or socketB. When the data comes on socketB after socketA the carry value from socketA is being incorrectly applied to socketB and the return value is wrong.
    Both sockets have to use the same processing subroutine and since this subroutine is very big I can not duplicate it by changing the object names. Is there a way that I could load the same subroutine twice in memory and call one from socketA and the other from socketB or perhaps I can use threads for each socket. Any help is appreciated

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void foo( int socket, int carryover, int aorb )
    {
        static int acarryover, bcarryover;
        if( aorb )
        {
            acarryover = carryover;
            do_whatever( socket, acarryover );
        }
        else
        {
            bcarryover = carryover;
            do_whatever( socket, bcarryover );
        }
    }
    Hm...
    Code:
    void foo( int socket, int carryover, int aorb )
    {
        static int acarryover, bcarryover;
        int temp = aorb ? acarryover = carryover : bcarryover = carryover;
        do_whatever( socket, temp );
    }
    If it's wrong, blame the lime, not the vodka.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    6
    It works but not quite as I would like it to work. For different carry value I have to create a different structure name assigned to the same structure (my_struct) and then put it in the function (my_foo). For example if I have carry value = 1 then I have to assign "my_struct carry1", if I have carry value = 2 then it would be "my_struct carry2" and so on. Then I use if statement like: If carry = 1 then my_foo(&carry1,......), if carry = 2 then my_foo(&carry2,......). Each of this new strucuters store different values to be used in my_foo (carry1->id is not equal to carry2->id). The problem is that there could be ten or more carry values so I have to create ten or more different structure names and ten or more if statements. Also once I create the structure name I have to reuse it the next time I have the same carry value without assigning the name again as above. Is it possible to create a structure array like "my_struct carry[carry value]" and then just use it directly in my function "my_foo(carry[carry value], .....)" ?
    Last edited by pankleks; 11-24-2005 at 03:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM