Thread: static data structure in a library

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    15

    static data structure in a library

    I don't even know if it makes any sense. Our sadist professor gave us this last exercise for this term. We should write a library that reassembles ip fragments (the fragmentation will be simulated over tcp with a server the professor wrote). The assembling library has two functions, one that gets a fragment and adds it to a packet and another one (that doesnt get anything) checks and updates the TTL of the packets we currently have (sort of a timeout mechanism to purge partial old packets). He said that the data structure (hashtable) that'll hold all the fragments will be in the library, and that makes sense coz the checkttl function from the library doesn't gets anything ie it runs over the same data structure every time. Now, finally, my question: how do I implement a static data structure in a library ? Any examples or links to tutorials will be very appreciated

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    how do I implement a static data structure in a library ?
    Is it the 'library' part or the 'static' part which bothers you?
    A static struct is always declared and used the same way, be it in a library or any other binary object.
    Simply use the 'static' modifier.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    Quote Originally Posted by root4
    Is it the 'library' part or the 'static' part which bothers you?
    Well, to be frank, both. Never done either. Thats why I want to see a basic example from which I can start rolling

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    here you go:
    Code:
    static struct point {
      int x;
      int y;
    } a;
    If you already know structures (you do, right?) it does not change anything for you: it simply tells the compiler to keep this object 'a' private (i.e. not exported when linking with other binaries).

    PS: for the part related to the creation of a library (static? dynamic?) it depends on your tools and platform.
    Last edited by root4; 01-10-2009 at 05:27 AM.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    Tried that out, easier than I first assumed

    Quote Originally Posted by root4
    PS: for the part related to the creation of a library (static? dynamic?) it depends on your tools and platform.
    The library must be static, not sure it's an issue here though.

    Another thing I'd like to ask: can I hide functions in the library from the user, I mean something like private functions in Java. Because if I'm to implement a hash table in the library I wouldn't want the user to have access to those specific functions. Or is there a smarter way to achieve that (maybe create another library to operate the hash table)?
    Thanx

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    can I hide functions in the library from the user
    Functions can be declared static too, take care to not add their prototype in the library interface (.h) and you're done.

    The library must be static, not sure it's an issue here though.
    There is no issue, i just meant the way of creating a library, static or dynamic (compilation options, etc) depends on your tools.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    There's no concept of a data structure "being in" a library. The code that manipulates it is in the library, the data structure is just in the program's address space.

    It seems like your question to me is, how do you initialize the hash table before the program starts using it. My answer would depend on whether you can add any new functions to the API you've been given.

    EDIT: I'd do something like this in the library:

    Code:
    static int initialized = 0;
    
    static void doInitialization()
    {
        if(!initialized)
        {
            // initialize the hash table
        }
        initialized = 1;
    }
    
    void Function1()
    {
        doInitialization();
        ...
    }
    
    void Function2()
    {
        doInitialization();
        ...
    }
    The much preferred solution would be to give the user of the library a chance to call the initialization itself, but your professor probably won't allow that, so you have to do something stupid.
    Last edited by brewbuck; 01-10-2009 at 11:15 AM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i do this? (static structure inside class)
    By 39ster in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2008, 03:14 AM
  2. Replies: 19
    Last Post: 01-12-2006, 11:04 AM
  3. 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
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Dynamic Data Structure -- Which one is better?
    By Yin in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2002, 11:38 PM