Creating Library w/4MB Array in UNIX Environment

  • Thread starter Hurkyl
  • Start date
In summary: 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.
  • #1
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,981
26
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?
 
Computer science news on Phys.org
  • #2
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.
 
  • #3
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.
 
  • #4
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
 
  • #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.
 
  • #6
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:
  • #7
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.

Programs and libraries regularly read from data files, so I don't understand your aversion to them.

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? :-p

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:
  • #8
Hurkyl said:
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.

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.
 
  • #9
Hrm, I'd expect gcc to choke, even if it was just the array. However,

even if you have to use a tool other than gcc to construct the object file.

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:
 
  • #10
Hurkyl said:
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.
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.
 
  • #11
Hurkyl said:
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:

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.
 
  • #12
If only I remembered any assembly. :frown:
 
  • #13
Hurkyl said:
If only I remembered any assembly. :frown:

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.
 

Related to Creating Library w/4MB Array in UNIX Environment

1. How do I create a library with a 4MB array in a UNIX environment?

To create a library with a 4MB array in a UNIX environment, you will first need to allocate memory for the array using the malloc() function. Then, you can use the ar command to create a static library by specifying the name of the library and the object files containing the array. Finally, you can use the ranlib command to add an index to the library.

2. What is the purpose of creating a library with a 4MB array in a UNIX environment?

Creating a library with a 4MB array in a UNIX environment allows you to store and organize a large amount of data in a single location. This can be useful for programs that require access to a large dataset, as it allows for efficient memory usage and faster access to the data.

3. How can I access the array in the library from my program?

To access the array in the library from your program, you will need to link your program to the library using the -l flag when compiling. Then, you can use the #include directive to include the library header file in your program and access the array as you would any other array in your program.

4. Can I modify the array in the library after it has been created?

No, once the array has been created and added to the library, it cannot be modified. However, you can create a new array and add it to the library using the ar command, or you can create a dynamic library which allows for modification of the array at runtime.

5. How can I delete a library with a 4MB array in a UNIX environment?

To delete a library with a 4MB array in a UNIX environment, you can use the rm command followed by the name of the library file. If the library contains an index, you will also need to delete the index file using the rm command. Additionally, you can use the ar command with the -d flag to delete specific files from the library.

Similar threads

  • Science and Math Textbooks
Replies
1
Views
988
  • Feedback and Announcements
Replies
17
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
15
Views
5K
Back
Top