Thread: need help with opaque programming with struct and addresses.

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    6

    need help with opaque programming with struct and addresses.

    Well in class we are doing what i think is called opaque programming. Meaning with code some thing and the client "customer" only recieves a head that tells him how it works. My teach said we would give a object file, but at work a guy told me it would be a dll.

    So my first question would be which one would it be and how do i comile it.

    Second question would be how would i then use it with the head file.

    My last question would be, that in that file we use Struct type of programming and the struct is static in it. So i need to send a handle "address if the stuct" to be used with the fonctions i gave him. So he doesnt know how the Struct is so he doesnt mess with it and doesnt know how it is programmed. I tried sending address, which i can but then im unable to is it since its stored in a int. Dont know how to declare it so when i pass back the address my programme can then used it. Since the Struct it static in my .c file and i can use it in my header file that is associated with the .c file that should be comiled and not be viewed by this client.

    here is an example of the header file the client would recieved with the type of compiling i dont know how to do hehehe.

    Code:
    typedef  struct gps * t_gps;
    
    t_gps init_gps_dflt(void);
    
    t_gps init_gps(double x, double y, double z);
    
    double get_MY_pos( const t_gps );
    
    double get_distance( const t_gps, double x, double y, double z);
    
    void set_gps( t_distributeur, double x, double y, double z );
    
    void free_gps( t_distributeur );

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    So my first question would be which one would it be and how do i comile it.
    If it was going to be a DLL you would know. A DLL is code that is all ready to be executed, so there is no compiling. What you do instead is load the library at some point, using system specific API calls, into RAM to be used by the executable. The person running the program needs to have the DLL being used on his computer.

    What you showed us is almost an opaque pointer. Another way to say it is a pointer to an incomplete type. The details of the struct must be in a different file, so you need to include the extern keyword:
    Code:
    typedef extern struct gps *t_gps;
    Now t_gps is an opaque pointer type: the thing that t_gps points to could be defined in any C source that #include your header.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    you miss understood what i meant. first i need to know how to compile this dll, and use it in my new program.

    second i need to know how im my dll i can use address from the extarnal program to manipulate the struct from with in the dll if the struct is static in the .c. so im either sending the address incorrecly and recieving it incorrecly and im doing it wrong to declare it in my .h and .c
    Last edited by DarkAngel4ever; 03-27-2011 at 11:03 PM. Reason: error in spelling

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    no one have any clues?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DarkAngel4ever
    first i need to know how to compile this dll, and use it in my new program.
    • You did not bother to read whiteflags' post: "A DLL is code that is all ready to be executed, so there is no compiling."
    • Be patient: wait for your teacher's instructions since you are only guessing as to what your teacher will give you.


    Quote Originally Posted by DarkAngel4ever
    second i need to know how im my dll i can use address from the extarnal program to manipulate the struct from with in the dll if the struct is static in the .c. so im either sending the address incorrecly and recieving it incorrecly and im doing it wrong to declare it in my .h and .c
    Conceptually, ignore the whole DLL business. Pretend that you actually have the whole library code, except that you are forbidden on pain of death from editing it, or even looking at it. Pretend also that if you attempt to directly create an object of struct gps, you will be instantly annihilated.

    What's left is that you have a bunch of functions that form the library interface. Use them. For example, to create and initialise the struct, you might write:
    Code:
    t_gps my_gps = init_gps_dflt();
    Of course, my_gps is a pointer to the struct, but that's how you work with objects of type struct gps since you want to live.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    since its not required in the couse he will not show us how to do it. but i still do want to do it.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    It looks like you are supposed to generated the implementations of all those functions. Then you compile it into an object file (a couple seconds searching on google or manning gcc will tell you how to do this).

    Then when you want to build an executable using those functions, you can just include the header in the files you need it for the func prototypes and link in the object file. Of course making a lib would be nicer/easier to build against.

    Only thing is, this looks odd.
    Code:
    typedef  struct gps * t_gps;
    struct gps shouldbe defined in the header too.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DarkAngel4ever
    since its not required in the couse he will not show us how to do it. but i still do want to do it.
    To do what?

    Quote Originally Posted by homer_3
    It looks like you are supposed to generated the implementations of all those functions.
    No, those functions have already been implemented. DarkAngel4ever is supposed to use them.

    Quote Originally Posted by homer_3
    struct gps shouldbe defined in the header too.
    No: you have missed the entire point of the opaque pointer idiom.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    No, those functions have already been implemented. DarkAngel4ever is supposed to use them.
    What makes you think that?
    My teach said we would give a object file
    Sounds like he's supposed to generate the object file to me.

    No: you have missed the entire point of the opaque pointer idiom.
    You're right. I hadn't heard of that phrase before and just looked it up.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    with my course yesterday i have a better idea how to pass struct address and such. As he has said yes i do wish to compile my own .obj file. my teach told me that it would be in such a type of file i would have my library. Would love to know how since i can play around with them to know more about C.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by homer_3
    What makes you think that?
    If I were the teacher, I would expect the code to be handed to me, not just an object file. Coupled with the fact that DarkAngel4ever was also rather careless with language, I read "we would give a object file" as "we would be given an object file".

    Quote Originally Posted by DarkAngel4ever
    As he has said yes i do wish to compile my own .obj file. my teach told me that it would be in such a type of file i would have my library. Would love to know how since i can play around with them to know more about C.
    Just compile it. You probably have already done it before the opaque pointer idiom was introduced to you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    "Meaning with code some thing and the client "customer" only recieves a head that tells him how it works. My teach said we would give a object file"

    sorry english isnt my first language. we would give an object file, meaning we would give it to the client.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-05-2009, 05:35 AM
  2. struct pointer
    By t014y in forum C Programming
    Replies: 5
    Last Post: 01-26-2009, 03:50 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  5. pointer to a struct
    By ramdal in forum C Programming
    Replies: 13
    Last Post: 12-15-2005, 09:01 AM