I have made a simple code to try enigma in C++. I am getting this error on rotor passing function (it is supposed to modify letter depending on the given rotor's letters array)
void pass_through(char& letter)
{
//this->t[0] tried it alone - its the cause (cant even std::cout)
letter = this->t[int(letter - 'A')];
}
The function is a member of class rotor, I thought the cause is too small stack size, so I changed class constructor to work on dynamic memory - nothing changed
I tried different initalizations. All these produce same error
const char t[26]; const char n[2]
char t[26]; char n[2]
char* t = new char[26]; char* n = new char[2]
I know that using initalizer list for 28 arguments is not a proper way to initalize such class. Then which method should I use to initalize each rotor with given 26 letters and <=2 notch letters? char array or std::string? Its strange for me to receive sigsegv on accessing class own array inside class function.
I tried checking values
int(letter - 'A')
It produces numbers 0 - 25 which are exactly what I wanted I tried creating whole enigma class both as stack and heap variable, nothing changes.
class initalizer
rotor(char A, char B, char C, char D, char E, char F, char G, char H, char I, char J, char K, char L, char M, char N, char O, char P, char Q, char R, char S, char T, char U, char V, char W, char X, char Y, char Z, char n0, char n1)
: t{A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z}, n{n0, n1}
{}
of course its different for dynamic version
MCVE - compiles and runs perfectly
#include <iostream>
#include <conio.h>
using namespace std;
class rotor{
private:
const char t[26];
const char n[2];
//char* t = new char[26];
//char* n = new char[2]; //NEED TO CREATE DESSTRUCTOR
public:
rotor(char A, char B, char C, char D, char E, char F, char G, char H, char I, char J, char K, char L, char M, char N, char O, char P, char Q, char R, char S, char T, char U, char V, char W, char X, char Y, char Z, char n0, char n1)
: t {A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z}, n{n0, n1}
{}
public:
void make_error(char& letter)
{
cout<<"\n1 - acces letter: "<<letter;
cout<<"\n2 - show t's index: "<<int(letter - 'A');
cout<<"\n3 - access one loaction: "<<this->t[0];
cout<<"\n4 - access desired location: "<<this->t[int(letter - 'A')];
letter = this->t[int(letter - 'A')];
cout<<"\n5 - letter after change: "<<letter;
}
};
rotor rotor_I('E', 'K', 'M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J', 'Q', 'Q');
int main()
{
char letter;
cout<<"enter letter: "; letter=getche();
rotor_I.make_error(letter);
}
I have placed some cout's to see if the class enigma works well with it's rotors members. Now I see something get's wrong
rotor* rotors[4]; //for the M4 enigma too, curently just building M3
//DoAllSetupFunctions();
cout<<"\nrotor[0] address"<<this->rotors[0];
cout<<"\nrotor[1] address"<<this->rotors[1];
cout<<"\nrotor[2] address"<<this->rotors[2];
result
rotor[0]: 0
rotor[1]: 0x4a82d0
rotor[2]: 0x4a82c0
Solved
I have found the cause - in a long way program one function was passing indexes 0, 1, 2 instead of 1, 2, 3 (I wanted to enumerate rotors exactly as Wikipedia to avoid confusion)
some pointers are initalized by many **if()**s (for user specefication)
the if() couldnt find any matching instruction for argument 0 which resulted in leaving uninitialized pointer poniting to NULL