4

I have to define a large array (lookup table) in a code. It contains 256 elements and takes nearly 1 computer screen.

There are two functions that are using this array. I want to define this array under functions, so I could access them very quickly during development.

But if I try to do it inside one file, compiler will give "Undeclared identifier" errors around functions - because they use array.

So, I must put the functions and the array to separate files.

Here is the structure of my program:

main.cpp:

#include "lookup.h"

...uses two functions...

-

lookup.h:

#ifndef SubMaster_lookup_h
#define SubMaster_lookup_h

void func1(void);
void func2(void);

char LookupTable[][3]={ "00", "01", "02" "03", "04", "05", "06", "07", "08", "09",
"0a", "0b", "0c", "0d", "0e", "0f", "00", "01", "02" "03", "04", "05", "06", "07",
"08", "09", "0a", "0b", "0c", "0d", "0e", "0f", "10", "11", "12", "13", and so on...}

-

lookup.cpp:

#include "lookup.h"

void func1() {
   ...body of func1...
}

void func2() {
   ...body of func2...
}

The following structure gives me "ld: duplicate symbol _LookupTable" during build. Is there any way to change the structure, so it will not give errors?

Wooble
  • 87,717
  • 12
  • 108
  • 131
Jake Badlands
  • 1,016
  • 3
  • 23
  • 46
  • 2
    You're storing numbers as strings...? – Matti Virkkunen Feb 12 '12 at 17:44
  • 1
    Lookup Table is the fastest way of converting hex values to char. – Jake Badlands Feb 12 '12 at 17:56
  • 1
    @Jake: Are you sure? On modern CPUs such a large table would consume a large amount of cache, which is expensive. Moreover, such a table gets stored in your binary, which makes startup time slower, as it has to be read from disk. Try this against the normal method of a couple of IFs and only continue if you can see a measureable difference in favor of the lookup table. – Billy ONeal Feb 12 '12 at 18:01
  • @BillyONeal I will have a lot of data to process, and a lot of operations with lookup table. I agree that startup time will be slower, but my main interest is processing performance. – Jake Badlands Feb 12 '12 at 18:07
  • @BillyONeal Thank you for your answer, after I will finish my app, I will try both ways and benchmark them. – Jake Badlands Feb 12 '12 at 18:26

3 Answers3

11

Two ways:

  • Declare the array in the header and define it in a cpp file
  • Make it static. But this is pretty nasty (each translation unit will get its own copy)

Try this:

lookup.h
extern char LookupTable[][3];

lookup.cpp
#include "lookup.h"

char LookupTable[][3] = ...

This answer is good.

Community
  • 1
  • 1
cnicutar
  • 178,505
  • 25
  • 365
  • 392
3

You can have extern char const lookupTable[][3]; in the header, and the actual implementation in a source file:

#include "header.h"

char const lookupTable[][3] = { /* ... */ }

Alternatively, declare the array as static char const lookupTable[][3] = /*...*/ in the header, but then you get repeated copies in each TU (each with static (i.e. internal) linkage).

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
2

Put it into a .cpp and write an extern declaration for it into the .h. Or, mark it as static (but that will result in an additional copy of that lookup table for each .cpp that includes the .h, so you should probably avoid it).

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299