0
#include<iostream>
#include<string>


using namespace std;
class LFSR
{
private:

    bool lfsr[];
    private: int tap;
    private: int t;
    private: string s;

public: 
    LFSR(string seed, int tap)
    {
        s = seed;
        t = tap;


        lfsr = bool[s.length()];

        t = s.length() - 1 - t;

        for (int i = 0; i < seed.length(); i++)
        {

            if (seed.charAt(i) == 48)
            {

                lfsr[i] = false;
            }
            else
            {

                lfsr[i] = true;
            }
        }
    }
    int step()
    {

        boolean newBit = lfsr[0] ^ lfsr[tap];
        for (int i = 0; i < lfsr.length - 1; i++)
        {

            lfsr[i] = lfsr[i + 1];
        }
        lfsr(lfsr.length - 1) = newBit;
        return newBit == false ? 0 : 1;
    }
    int generate(int k)
    {

        int temp = 0;
        for (int i = 0; i < k; i++)
        {

            temp = 2 * temp;
            temp = temp + step();

        }
        return temp;

    }
};
int main()
{

    LFSR lfsr("01101000010", 8);
    for (int i = 0; i < 10; i++)
    {

        int bit = lfsr.step();
        cout << lfsr << "+" << " " << bit;
    }
    LFSR lfsr("0110100010", 8);
    cout << "Testing generate()";

    for (int i = 0; i < 10; i++)
    {

        int r = lfsr.generate(5);
        cout << lfsr.to_string() << " " << r;
    }
    return 0;
}

At this line :

      lfsr=bool[s.length()];

I am getting the above stated error.

What am I trying to do?

I am trying to create a LFSR of size 10(seed length or number of registers).

My questions?

Why am I getting this errror?

How can I fix this error?

The code in java that I am trying to do in C++ I will give you the code, let's see.

public class LFSR {
 private boolean[] lfsr;
 private int tap;
/**
 * @param args the command line arguments
 */
public static void main(String[] args)
 {
 LFSR lfsr = new LFSR("01101000010", 8);
System.out.println("Testing step()");
 for (int i = 0; i < 10; i++)
 {
 int bit = lfsr.step();
System.out.println(lfsr + " " + bit);
}
 lfsr = new LFSR("01101000010",8);
System.out.println("\nTesting generate()");
 for (int i = 0; i < 10; i++)
 {
 int r = lfsr.generate(5);
System.out.println(lfsr + " " + r);
}
 }
 public LFSR(String seed, int tap)
 {
 lfsr = new boolean[seed.length()];
 this.tap = (seed.length()-1)-tap;
 for(int i = 0; i < seed.length(); i++)
 {
 if(seed.charAt(i) == 48)
 {
 lfsr[i] = false;
}
 else
{
 lfsr[i] = true;
}
 }
 }

 public int step()
 {
 boolean newBit = lfsr[0] ^ lfsr[tap];
 for(int i = 0; i < lfsr.length-1; i++)
 {
 lfsr[i] = lfsr[i+1];
}
 lfsr[lfsr.length-1] = newBit;
 return newBit == false ? 0 : 1;
}
 public int generate(int k)
 {
 int temp = 0;
 for(int i = 0; i < k; i++)
 {
 temp *= 2;
temp += step();
}
 return temp;
}
 public String toString()
 {
 String representation = "";
 for(int i = 0; i < lfsr.length; i++)
 {
 representation += lfsr[i] == false ? 0 : 1;
}
 return representation;
}
}
A M
  • 14,694
  • 5
  • 19
  • 44
chwass247
  • 11
  • 2
  • Use `std::array` instead of C arrays. You can't assign a C array to a C array. If you don't know the size at compile time use `std::vector`. You should use a constructor initializer list. – Thomas Sablik Aug 14 '20 at 09:22
  • the closer of `lfsr=bool[s.length()];` is `lfsr=new bool[s.length()];` having `bool * lfsr;` but you come from Java and certainly far to use `delete`, so use a `std::vector` as proposed in the answer – bruno Aug 14 '20 at 09:28
  • There are many many more bugs in the Code. C++ has nothing to do with Java. It is a different Language. – A M Aug 14 '20 at 09:36
  • @bruno I am not coming from Java. TBH, i found that code in java in some russian websites. I come from C/C++ background. – chwass247 Aug 14 '20 at 12:20
  • @chwass247 the code you gave was (unchanged) from a site ? just never go again on that site then if hat code was presented as a 'good' code ... – bruno Aug 14 '20 at 12:34
  • **Do not ever use flexible arrays in modern C++ code**; there are almost always better alternatives. But fo further reading about C flexible arrays, see this: https://stackoverflow.com/questions/20221012/unsized-array-declaration-in-a-struct/20221073 – Red.Wave Aug 14 '20 at 12:41

3 Answers3

4

Don't try and program C++ like it is Java, the syntax is different, the semantics are completely different.

Like this (perhaps)

#include <vector>
#include <string>

private:
    std::string s;
    int t;
    std::vector<bool> lfsr;

public:
    LFSR(const std::string& seed, int tap) : s(seed), t(tap), lfsr(seed.length())
    {
        ...
    }
john
  • 85,011
  • 4
  • 57
  • 81
  • Using `std::vector` always requires a special mention. Normally it's fine, but it can give you trouble. – Bathsheba Aug 14 '20 at 09:40
  • I was not trying to translate the code. But, after understanding http://bits.usc.edu/cs103-sp15/coursework/lfsr/ from here, I did it myself, but as a reference, I found that java code. – chwass247 Aug 14 '20 at 11:58
0

Here is a possible solution. I think, you can compare it with your Java code and see the differences.

#include <vector>
#include <string>
#include <iostream>

class LFSR {
private:
    std::vector<bool> lfsr;
    int tap;

public:
    LFSR(std::string seed, int tap) {
        lfsr.reserve(seed.length());
        this->tap = (seed.length()-1) - tap;
        for(std::size_t i = 0; i < seed.length(); i++) {
            lfsr.push_back(seed.at(i) == '0');
        }
    }

    int step() {
        bool newBit = lfsr[0] ^ lfsr[tap];
        for(std::size_t i = 0; i < lfsr.size()-1; i++) {
            lfsr[i] = lfsr[i+1];
        }
        lfsr[lfsr.size()-1] = newBit;
        return newBit == false ? 0 : 1;
    }

    int generate(int k) {
        int temp = 0;
        for(int i = 0; i < k; i++) {
            temp *= 2;
            temp += step();
        }
        return temp;
    }

    std::string toString() const {
        std::string  representation = "";
        for(std::size_t i = 0; i < lfsr.size(); i++) {
            representation += lfsr[i] == false ? 0 : 1;
        }
        return representation;
    }

    friend std::ostream& operator<<(std::ostream &stream, LFSR const &lfsr) {
        return stream << lfsr.toString();
    }
};


int main() {
    LFSR lfsr("01101000010", 8);
    std::cout << "Testing step()";
    for (int i = 0; i < 10; i++) {
        int bit = lfsr.step();
        std::cout << lfsr << " " << bit;
    }
    LFSR lfsr2("01101000010",8);
    std::cout << "\nTesting generate()";

    for (int i = 0; i < 10; i++) {
        int r = lfsr2.generate(5);
        std::cout << lfsr2 << " " << r;
    }
}
Vasilij
  • 1,861
  • 1
  • 5
  • 9
  • The code doesn't executes once it reaches "Testing step()". – chwass247 Aug 14 '20 at 10:11
  • Yes, there was an error there. member variable tap didn't get initilized. Changed tap to this->tap in the constructor. Didn't notice it first type because of a typo in main, this part was not excuted. I edited the code. Have a look at online compilator with PVS-studio tool: https://godbolt.org/z/3nn54z Uncomment the line and look at the output - it points out at this mistake immediately. – Vasilij Aug 14 '20 at 10:27
0

You can change the declaration of lfsr to std::unique_ptr<bool[]> lfsr;. Then:

lfsr=new bool[s.length()];

You need not worry about deleting the array pointer anymore; It is a fire & forget std utility and behaves like an array, but it will cost copyability of the class; i.e LFSR won't be assignable nor copy constructible.

Another option would be std::vector as mentioned in earlier replies.

PS: Don't forget the header:

#include <memory>

Cheers, FM.

Red.Wave
  • 2,790
  • 11
  • 17