You are here:

C++/reading reference table

Advertisement


Question
Hello,
How are you doing? I have a program that needs to read in a reference table during runtime. The program would refer to this table from time to time. The table is basically two 1-dimensional arrays of doubles. Currently I do it by referring to a function which is nothing more than the statements for arrays:

void readRef()
{
array1[0]=2.45; array2[0]=5.17;
array1[1]=3.78; array2[1]=3.12;
.....
array1[1700]=4.67; array2[1700]=0.12;
}

I do this because I don't know how to make a text file part of the compiled application (static). Anyway, my method should work, but I run into weird problems. For example it compiles on 32 Debian to give a 1 MB executable which runs fine. When I compile the same program on 64 bit Scientific Linux (RedHat) it takes a long time to compile and the executable is 70 MB and it won't run (I get a weird message that an X-something is not found). I'm using Qt. I'm pretty sure that my functions that contain all the array definitions cause this. Do you know what is happening? I'm very thankful for any ideas.
Regards,
Andres

Answer
> I do this because I don't know how to make a text file part of the compiled application ...

I would suggest that you should put the values into two simple text files - say "array1.txt" and "array2.txt". And just read them in at run-time. This way, we can run the program with different data sets without having to rebuild the program every time.  

I would also suggest that you use std::vector<> instead of an array.

#include <vector>
#include <fstream>
#include <iterator>

std::vector<double> read_array( const char* path2file )
{
   std::ifstream file(path2file) ;
   return std::vector<double>( std::istream_iterator<double>(file),
         std::istream_iterator<double>() ) ;
}

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Begin



> When I compile the same program on 64 bit Scientific Linux (RedHat) it takes a long time to compile and the executable is 70 MB and it won't run (I get a weird message that an X-something is not found). I'm using Qt. I'm pretty sure that my functions that contain all the array definitions cause this.

I'm pretty certain that this has nothing to do with the arrays or the functions that assign values to array elements. If N is the number of elements in the array, the memory taken up by an array of N double values would be just N * sizeof(double). Add the code size for moving N double values to that, and it still won't come anywhere near 70 MB.

One way to check this out would be:
Make a copy of the program and modify it so that the number of elements in the array is just 10. And then compile the modified program on 64 bit Scientific Linux (RedHat).  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.