Thread: typecasting data for parameter passing

  1. #1
    Registered User daluu's Avatar
    Join Date
    Dec 2002
    Posts
    42

    typecasting data for parameter passing

    See code below. Due to # of parameter passing limitations, I would like to pass a structure holding a set of parameters to a function as a single parameter and then the function would know how to appropriately make use of the structure. I'm new to this so I don't know if I'm implementing the typecasting correctly for this scenario. I get the following errors on compile time:

    Code:
    fork.c:93: request for member `param1' in something not a structure or union
    fork.c:94: request for member `param2' in something not a structure or union
    fork.c: In function `main':
    fork.c:115: aggregate value used where an integer was expected
    Please advise me on what I need to modify to get this to work as intended.

    Code:
    typedef struct myargs{
    	int param1;
    	int param2;
    }MYARGS;
    
    void
    eventual_function(int id)
    {
        //this is the forked function
        Wait((MYARGS) id.param1);
        Signal((MYARGS) id.param1);
        Signal((MYARGS) id.param2);
        ...
    }
    
    int
    main()
    {
        //note fork & semaphore functions custom defined (Nachos OS)
        //establish user semaphores
        int usrmutex = CreateSemapore(1); 
        int usrprocsema4 = CreateSemapore(5);
    
        //parse semaphore handles for passing to forked children
        MYARGS fncargs;
        fncargs.param1 = usrmutex;
        fncargs.param2 = usrprocsema4;
    
        Fork(MyThreadA,(int) fncargs);
        Wait(usrprocsema4);

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    What system/threading library/process library is this?

    You can only do unsafe reinterpret_casts when the types have the same size. This is not the case here.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure why you're even trying to type cast at all. You're type casting an integer into a structure.
    Code:
    typedef struct myargs{
    	 int id.param1;
    	int param2;
    }MYARGS;
    
    void
    eventual_function(int id)
    {
        //this is the forked function
        Wait((MYARGS) id.param1);
        Signal((MYARGS) id.param1);
        Signal((MYARGS) id.param2);
    If that function just takes a single integer as its argument, why are you trying to cast it to a structure instance? You're basicly saying "This function takes a MYARGS as an argument." But you're passing it a single integer. That wouldn't work even if you did cast it right, because what would happen if the function wanted to access more than just that first integer?

    A type cast it used to convert one data type to another. Usually for places where you have a single character, and the function wants an integer, or something similar.

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

  4. #4
    Registered User daluu's Avatar
    Join Date
    Dec 2002
    Posts
    42
    the (simulated OS) code uses ints for parameter passing across the system. the simulated registers for storing data hold ints, I believe.

    what i posted is what I intended to do. Is there an alternate way to workaround being able to pass 1 parameter to a function?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It could be some kind of handle to a very global data structure.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post those function prototypes. I highly doubt that they take what you're trying to give them. First off, look at it again:

    1) You're passing it a single integer. Is a single integer what the function is supposed to take?
    2) You're telling the function that this integer is not in fact an integer, but is a structure. Do the functions actually take a structure as their argument? If so, why aren't you just passing the structure to it, instead of an integer?

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

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You can pass in a pointer into an integer parameter as long as the size of the integer parameter is large enough to hold the pointers value.

    reinterpret_cast<> can be used to safely convert a pointer value to an integral value, then back to a pointer value of the same type we started with. Here's an example:
    Code:
    #include <iostream>
    using namespace std;
    
    // compile time assertion utility
    template <bool assertion> struct compile_time_assert;
    template <> struct compile_time_assert<true> {};
    
    #define CT_ASSERT(assertion) \
        compile_time_assert<(assertion)>()
    
    struct Args
    {
        int a, b;
    };//Args
    
    void foo(int param)
    {
        // don't compile this code if the size of param isn't 
        // big enough to hold a pointer
        CT_ASSERT(sizeof(param) >= sizeof(void*));
    
        Args *pargs = reinterpret_cast<Args*>(param);
    
        cout << "Called with a = " << pargs->a 
             << ", b = " << pargs->b << endl;
    }//foo
    
    int main()
    {
        Args args;
        
        args.a = 42;
        args.b = 69;
    
        foo(reinterpret_cast<int>(&args));
    
        return 0;
    }//main
    gg

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> fork.c
    You're in the wrong forum

    My example is in C++

    gg

  9. #9
    Registered User daluu's Avatar
    Join Date
    Dec 2002
    Posts
    42
    thanks for the input. I ended up defining the functions differently to get it to work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. Data member passing
    By wirefree101 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2006, 01:41 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Passing structure data to a function?
    By diddy02 in forum C Programming
    Replies: 8
    Last Post: 08-17-2002, 04:15 PM