Provide a proper variable

how should I right a variable in int gindex (unsigned int PC, int bank, long long hist, folded_history * ch_i) for "folded_history"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class folded_history
{
public:


  unsigned comp;
  int CLENGTH;
  int OLENGTH;
  int OUTPOINT;

    folded_history ()
  {
  }


  void init (int original_length, int compressed_length)
  {
    comp = 0;
    OLENGTH = original_length;
    CLENGTH = compressed_length;
    OUTPOINT = OLENGTH % CLENGTH;

  }

  void update (uint8_t * h, int PT)
  {
    comp = (comp << 1) ^ h[PT & (HISTBUFFERLENGTH - 1)];
    comp ^= h[(PT + OLENGTH) & (HISTBUFFERLENGTH - 1)] << OUTPOINT;
    comp ^= (comp >> CLENGTH);
    comp = (comp) & ((1 << CLENGTH) - 1);
  }

};


Bests
Hello aminmax,

I am having a hard time understanding this. What is:
int gindex (unsigned int PC, int bank, long long hist, folded_history * ch_i). This looks like a regular variable that you are trying to make into a function call or a forward declaration for a function that you did not show or have not defined.

Any way what is in () is confusing because I have nothing to match it with.

A few adjustments I made:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class folded_history
{
    public:
        unsigned comp{};  // <--- Usually variables are made private.
        int CLENGTH{};    // <--- Best to use lower case letters. This implies the variable is defined as a constant.
        int OLENGTH{};;   // <--- Better names would be helpful. I have no idea what the "O" stands for.
        int OUTPOINT{};

        folded_history() {}

        // <--- You could also write as an overloaded ctor:
        //folded_history(int original_length, int compressed_length) : comp(0), OLENGTH(original_length), CLENGTH(compressed_length), OUTPOINT(OLENGTH % CLENGTH) {}

        void init(int original_length, int compressed_length)
        {
            comp = 0;
            OLENGTH = original_length;
            CLENGTH = compressed_length;
            OUTPOINT = OLENGTH % CLENGTH;

        }

        void update(uint8_t * h, int PT)
        {
            comp = (comp << 1) ^ h[PT & (HISTBUFFERLENGTH - 1)];
            comp ^= h[(PT + OLENGTH) & (HISTBUFFERLENGTH - 1)] << OUTPOINT;
            comp ^= (comp >> CLENGTH);
            comp = (comp) & ((1 << CLENGTH) - 1);
        }

};


Andy
Dear Andy,

Sorry, it is my fault, asking correctly a question is a soft-skill that should I learn :)

How should I correctly initialize gindex in the fourth variable that is folded_history * ch_i?

I mean, for example: the first variable of gindex is unsigned int PC that I know that I should write "10, or 22, and any unsigned integer", but for the folded_history * ch_i, I don't know write what?

Bests
Last edited on
@aminmax,
you don't really give enough context here.

In the calling procedure you can create a variable of type folded_history; say
folded_history myvar;

Then you can call this function of yours with the memory address of that variable,
&myvar
as an argument.

If that is not your intention then give more context.
typically a pointer is initialized to one of 3 things:
nullptr keyword. this is the same as {} initialize for a pointer. This implies that it will get a variable by new or address of etc later on.

the address of a variable:
folded_history fh;
...
folded_histroy * pfh = & fh;
or a new piece of memory:
folded_history * phf = new folded_history[42];


however, ask yourself why you want a pointer in the first place. Is there a good reason that cannot be solved via a vector or a reference or the like?
Last edited on
Topic archived. No new replies allowed.