Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Library and big object

  1. Apr 7, 2005 #1

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I want to write a library that has a large object in it.

    Specifically, it's a 4 megabyte array, but I'm curious about general objects too.

    I want to create a library (on a UNIX machine) that includes this object. I know of three ways I don't like so far:

    (1) Construct the object at runtime
    It's time consuming, and I'd not like to have to wait every time I run my programs.

    (2) Load the object from a separate file at runtime
    I don't like having zillions of files lying around -- besides, it seems silly... isn't the point of an archive to hold these things?

    (3) Generate a (huge) header file that gets included when building the library.
    gcc chokes on it, and it takes forever to compile, though I do get what I want at the end.


    Any suggestions?
     
  2. jcsd
  3. Apr 7, 2005 #2

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I should add that I'd be happy if I can get it into an object file too, but I don't expect that to be easy.
     
  4. Apr 7, 2005 #3

    dduardo

    User Avatar
    Staff Emeritus

    What about using zlib to compress the array and writing the output into your source code. When you run your code the compressed array will be loaded into memory. Then use zlib to inflate the array in memory.
     
  5. Apr 7, 2005 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    How are you including the array in the header file? I don't see why gcc should choke on it. You could also consider using assembly; you can reserve a large data segment quite easily.

    In either case, you really should not put 4 MB on the stack -- you should allocate such memory on the heap.

    - Warren
     
  6. Apr 7, 2005 #5
    The other option I would consider is, if you say you can "generate" the object at runtime, would it be possible to merely have a function which calculates the value as it is needed?

    The only other option that makes sense to me is (2). As for having "zillinos" of data files lying around, why would you need anything more than one file ? Programs and libraries regularly read from data files, so I don't understand your aversion to them.
     
  7. Apr 7, 2005 #6

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I wrote the array into a text file named "array.inc"

    ----------------
    0x12345678, 0x23456789, 0x3456789a,
    0x456789ab, 0x56789abc, 0x6789abcd,
    ...
    ----------------

    then, in my header file,

    ----------------
    unsigned int array[1<<19] = {
    #include "array.inc"
    };
    ----------------

    gcc ran on it, but it seemed to be thrashing -- the disk was running pretty heavily. It took several minutes to compile something that included the header. (The system I'm using isn't exactly state of the art...)


    What I would most like is to have my header have something like:

    extern const int *const array;

    and write the bytes to a file that becomes part of the library, and have the pointer linked to the file, if that's at all possible.
     
    Last edited: Apr 7, 2005
  8. Apr 7, 2005 #7

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    At the moment, the only way I know how to generate the data is linearly -- if it can be done as random access, it will take a good bit of cleverness.

    I'm extraordinarily picky. :smile: Overly curious too. Basically, it seems to me that there should be some straightforward way to pack my data into an object file, or into a library, so that it can be directly linked with stuff. After all, isn't that what libraries and object files are for? :tongue2:

    And if I can do it, it would probably be the more efficient than the alternatives, as well as keeping down the number and types of things I would need to manage.

    Thus, despite having workable alternatives, I would like to find out if it can be done this way. :smile:
     
    Last edited: Apr 7, 2005
  9. Apr 7, 2005 #8
    Why haven't you tried doing that? (I'm assuming you haven't, since you only mentioned trying to put the data directly into a header file) Just put the array in a code file, and put an extern declaration in the header file.

    Of course, gcc might just choke on the code file anyway, I don't know if it'll be too large. But that approach should work, even if you have to use a tool other than gcc to construct the object file.
     
  10. Apr 7, 2005 #9

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Hrm, I'd expect gcc to choke, even if it was just the array. However,

    What other tools do I have available to do this? At the moment, the only way I know how to make it into an object file is a massive C source file. :frown:
     
  11. Apr 7, 2005 #10

    NoTime

    User Avatar
    Science Advisor
    Homework Helper

    Sure, compile the array seperately with an export directive.
    Then the extern gets resolved at link time.
    You can put it in a library or use the .o.
     
  12. Apr 8, 2005 #11
    You could try using assembly language (I think that was already suggested). An assembler would probably be able to handle a large data block much better than gcc would handle a huge array initializer, although that's only a guess.

    It wouldn't be that difficult; you'd just have to declare a initialized data segment with an make sure that a pointer to it is exported.
     
  13. Apr 8, 2005 #12

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    If only I remembered any assembly. :frown:
     
  14. Apr 8, 2005 #13
    Well, you wouldn't actually have to write any real assembly language; you'd just have to use a couple of assembler directives.

    For example, in NASM you'd just need something like:

    global array
    array:
    dd 0x12345678, 0x23456789, ...
    dd 0x456789ab, 0x56789abc, ...
    ...

    Depending on what assembler you have available, the commands might be slightly different, but basically that's all you'd need.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Library and big object
  1. Visual Basic Library! (Replies: 3)

  2. Big arrays (Replies: 13)

Loading...