Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ loading large amount of data at compile time

I have a C++ object which needs a huge amount of data to instantiate. For example:

  class object {
      public object() { 
          double a[] = { array with 1 million double element }; 
      /* rest of code here*/};
      private:
      /* code here*/    
  }

Now the data (i.e 1 million double numbers) is in a separate text file. The question: How can I put it after "double a[]" in an efficient way and eventually compile the code? I do not want to read the data at run time from a file. I want it compiled with the object. What can be a solution? Ideally I would like the data to sit in the separate text file as it presently resides and somehow also have an assignment like double a[] =..... above.

Is this possible? Thanks in advance!

like image 392
user1612986 Avatar asked Nov 30 '12 01:11

user1612986


People also ask

Is C slow to compile?

C is slow. It suffers from the same header parsing problem as is the accepted solution. E.g. take a simple windows GUI program that includes windows. h in a few compilation unit, and measure the compile performance as you add (short) compilation units.

Is sizeof determined at compile time?

sizeof is evaluated at compile time, but if the executable is moved to a machine where the compile time and runtime values would be different, the executable will not be valid.

How long does it take to compile C?

A default compile takes about 0.4 seconds of processor time.


1 Answers

Something like:

class object
{
  public
  object(){ double a[] = { 
     #include "file.h"
  }; 
   /* rest of code here*/};
  private:
  /* code here*/    
}

The file has to be formatted correctly though - i.e. contain something like:

//file.h
23, 24, 40,
5, 1.1, 

In general, you can use #include directives to paste content into files. I've seen virtual methods being pasted like that, if they were common for most derived classes. I personally don't really like this technique.

like image 172
Luchian Grigore Avatar answered Sep 21 '22 13:09

Luchian Grigore