Thread: lookup tables storage location

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    39

    lookup tables storage location

    Hi folks. I am solving some equations and then storing the solutions in a lookup table that also serves as an interpolation table. My questions are: 1) Where is the lookup table stored? (The reason I don't know where the table is stored is because I use the maths package gsl and don't know enough programming myself.)

    2) If I make the lookup table in a c file and then use it in a couple of other c files and only de allocate the memory after the program is about to end, will it make my code slow?

    Instead of copying the entire code here, I am pasting this link to the gsl example.

    The general format seems to be
    1) allocate memory 2) use the lookup table for your caculations 3) de allocate the memory. But my problem is that I do the step 2) i.e use the lookup table in other c files. My code would do something like this

    in file one 1) allocate memory 2) create a lookup table
    in file two 3) use the lookup table in various calculations repeatedly
    from which file, one or two should I de allocate the memory? or it does not matter as long as it is done. Here is the link

    Interpolation Example programs - GNU Scientific Library -- Reference Manual

    Can you please also tell me topics I need to read about that are related to what I am trying to do here. I have some idea that I should read malloc and dynamic memory allocation. I am still a couple of chapters behind in the book, will get there soon. Do I need to know how to store data to cache?
    Last edited by ali.franco95; 09-12-2011 at 08:28 AM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    From the start of your post :

    1 - The table is stored in memory. If you use malloc or a similar function, the pointer returned tells you how to access it. If you just declare a fixed-sized array as a variable, you use the variable name. You don't need to know anything else to use it.
    2 - Maybe. Depends on how you do it and what you mean by slow. Slow's usually relative to other possible ways to solve the problem so we'd only be guessing.

    You can deallocate the memory from any place you have access to the variable pointing to it. It doesn't matter - just don't do it before you're done with it and don't deallocate it twice.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    thank you for responding. I am bad at making myself clear. I was worried about that if the lookup table is stored in RAM then that would be a waste of memory. Wouldn't it be a better thing if instead the table were saved in say a .dat file and read from when required. How do the guys who know what they are doing it do it? And the other thing is that I would have about 10 different lookup tables. I guess it is easily handled by c, just have to create a struct with all the lookup tables as its members.
    Last edited by ali.franco95; 09-12-2011 at 08:44 AM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    How much RAM you mind using depends on the circumstances.

    Unless the table is well into the hundreds of Mb or you are running very limited hardware it's fine to use RAM for this. Probably quite a few of the things running on your desktop right now are anyway, right?

    If this a process that is going to run continuously in the background, you probably want to be more polite and conservative with resources. However, "resources" includes CPU usage and disk access, which I think that's what you will end up consuming by "saving RAM" in this case, and IMO -- again, unless this is a really huge table -- that tradeoff will not be worth it. So use RAM.

    If instead it is something intended to exploit the power of the processor as much as possible -- ie, you want your calculations done ASAP, even if that completely occupies the computer for whatever time period -- definitely use RAM up to the point where there is none left to use, then swap to disk.
    Last edited by MK27; 09-12-2011 at 08:51 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    Ok. thanks heaps

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ali.franco95 View Post
    thank you for responding. I am bad at making myself clear. I was worried about that if the lookup table is stored in RAM then that would be a waste of memory.
    How big is the table? If you're on anything but an imbedded microcontroller it's not much of a concern. Most desktop systems these days have gobs of memory, 4gb is typical, 16gb is not uncommon. So even a few megabytes of memory is not an issue.

    Besides, where else you gonna put it?


    Wouldn't it be a better thing if instead the table were saved in say a .dat file and read from when required. How do the guys who know what they are doing it do it? And the other thing is that I would have about 10 different lookup tables. I guess it is easily handled by c, just have to create a struct with all the lookup tables as its members.
    Any programmer with a lick of sense might store the table in a disk file, but would load it into memory for use... Accessing memory is literally hundreds of times faster than accessing a disk drive.

    You can create a struct... but your tables will need to be in arrays inside the struct... so all you're really doing is creating "overhead". They can exist just fine as separate arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ storage location for various variables
    By sed_y in forum C++ Programming
    Replies: 14
    Last Post: 07-18-2011, 07:55 PM
  2. Index with lookup tables
    By Syley in forum C Programming
    Replies: 6
    Last Post: 06-21-2011, 02:21 PM
  3. Lookup in lists
    By agentsmith in forum C Programming
    Replies: 1
    Last Post: 12-07-2007, 02:14 PM
  4. DNS lookup?
    By Devil Panther in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2004, 07:05 AM
  5. DNS Lookup
    By xxx in forum Windows Programming
    Replies: 1
    Last Post: 01-08-2003, 02:43 PM