Choose to use compile time or dynamic run time for big array.

Discussion in 'C++' started by Immortal Nephi, Dec 23, 2009.

  1. The programmers want to create a big array. The array can have 1
    million elements. They need to decide how to store big array in
    either source code or binary file.
    They choose compile time. They fill data in each element of big
    array on the source code.
    They choose dynamic run time. They load binary file through fstream.
    Please let me know. Are both examples acceptable to the programmer’s
    preference? If you are against compile time or dynamic run time,
    please explain your opinion.

    For example:

    // Compile Time

    class ArrayName
    {
    public:
    ArrayName() {}
    ~ArrayName() {}

    private:
    static const unsigned char s_kData[ 1000000 ];
    };

    const unsigned char ArrayName::s_kData[ 1000000 ] =
    {
    0x12, 0x15, 0x45, // …..more data go into elements
    };

    You can create multiple ArrayName objects. All ArrayName objects
    share s_kData.


    // Dynamic Run Time

    class ArrayName
    {
    public:
    ArrayName()
    {
    if( s_pData == 0 )
    {
    s_pData = new unsigned char[ 1000000 ];
    // Do something
    // Use fstream to open and read data from binary file
    // Copy and store data into s_pData memory
    // Close binary file
    }
    }
    ~ArrayName()
    {
    if( s_pData != 0 )
    {
    delete [] s_pData;
    s_pData = 0;
    }
    }

    private:
    static unsigned char* s_pData;
    };

    unsigned char* ArrayName::s_pData = 0;

    If you create more than one ArrayName objects, first object allocates
    memory before second object or more objects do not need to reallocate
    and share s_pData.
    After you are ready to destruct all ArrayName objects, last object
    tests to see if pointer is not zero before it deallocates memory and
    add zero to s_pData. First object and other objects are prevented to
    deallocate s_pData.
    Please share with me your opinion.
     
    Immortal Nephi, Dec 23, 2009
    #1
    1. Advertising

  2. Immortal Nephi wrote:
    > The programmers want to create a big array. The array can have 1
    > million elements. They need to decide how to store big array in
    > either source code or binary file.
    > They choose compile time. They fill data in each element of big
    > array on the source code.
    > They choose dynamic run time. They load binary file through fstream.
    > Please let me know. Are both examples acceptable to the programmer’s
    > preference? If you are against compile time or dynamic run time,
    > please explain your opinion.
    >


    [snip]

    For linux (I don't know for other OSes) you can mmap() a file, and have
    that data in virtual memory. Not sure if that is what your question is
    about.

    If not, then I would prefer loading a binary file at the start of the
    program.
     
    Vladimir Jovic, Dec 23, 2009
    #2
    1. Advertising

  3. On 23 déc, 04:31, Immortal Nephi <> wrote:
    >         The programmers want to create a big array.  The array can have 1
    > million elements.  They need to decide how to store big array in
    > either source code or binary file.
    >         They choose compile time.  They fill data in each element of big
    > array on the source code.
    >         They choose dynamic run time.  They load binary file through fstream.
    >         Please let me know.  Are both examples acceptable to the programmer’s
    > preference?  If you are against compile time or dynamic run time,
    > please explain your opinion.

    [snip]

    IMO it depends on your environment. Compile time is painless (no need
    for configuration, no load, no availability time, no security/
    compliance issue ...) while load time is more flexible (you can change
    the array, save it, restore it archive it ... outside the program).

    In doubt, you could do both :) Use an internal compile time one for
    default configuration and optionally load one.

    Note: xxd has an option to output a file in static C array definition
    format. You can keep the data as an external binary file and use it in
    your build process to generate the compilable version.
    In the release process, you can decide to ship or not the binary file
    (or to ship a modified version).

    --
    Michael
     
    Michael Doubez, Dec 23, 2009
    #3
  4. Immortal Nephi

    James Kanze Guest

    On Dec 23, 8:48 am, Michael Doubez <> wrote:
    > On 23 déc, 04:31, Immortal Nephi <>
    > wrote:
    > > The programmers want to create a big array. The array can
    > > have 1 million elements. They need to decide how to store
    > > big array in either source code or binary file. They choose
    > > compile time. They fill data in each element of big array
    > > on the source code. They choose dynamic run time. They
    > > load binary file through fstream. Please let me know. Are
    > > both examples acceptable to the programmer’s preference? If
    > > you are against compile time or dynamic run time, please
    > > explain your opinion.


    > [snip]


    > IMO it depends on your environment. Compile time is painless
    > (no need for configuration, no load, no availability time, no
    > security/ compliance issue ...) while load time is more
    > flexible (you can change the array, save it, restore it
    > archive it ... outside the program).


    I think it also depends on where the data comes from. Typing
    the initializers for an array of a million elements is not
    painless. But if you can generate the C++ automatically, it's
    often the best option.

    --
    James Kanze
     
    James Kanze, Dec 23, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. FerrisUML

    Choose Membership Provider at Run-time

    FerrisUML, Apr 14, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    516
    Cowboy \(Gregory A. Beamer\)
    Apr 14, 2008
  2. Shaguf
    Replies:
    0
    Views:
    524
    Shaguf
    Dec 24, 2008
  3. Shaguf
    Replies:
    0
    Views:
    481
    Shaguf
    Dec 26, 2008
  4. Shaguf
    Replies:
    0
    Views:
    259
    Shaguf
    Dec 26, 2008
  5. Shaguf
    Replies:
    0
    Views:
    236
    Shaguf
    Dec 24, 2008
Loading...

Share This Page