I've created a class with a couple of variables in it. I can read these variables from my functions just fine. I seem to be able to update them to new values, however when I try to get the new values in the main()
function it gives me the original values.
I'm sure it's something simple I'm missing, I just can't seem to figure it out.
header.h
#pragma once
#include <string>
#include "gmp.h"
class my_class {
public:
const static int CPU_GRP_SIZE = 16;
mpz_t tempx, tempy;
my_class()
{
mpz_init_set_str(tempx, "79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16);
mpz_init_set_str(tempy, "483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16);
}
};
void AddDirect(mpz_t p2x, mpz_t p2y);
main.cpp
#include <string>
#include <iostream>
#include <gmp.h>
#include "header.h"
void main()
{
my_class secp;
int hLength = (secp.CPU_GRP_SIZE / 2 - 1);
mpz_t point2x, point2y;
mpz_init_set_str(point2x, "c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5", 16);
mpz_init_set_str(point2y, "1ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a", 16);
AddDirect(point2x, point2y);
std::cout << "+++++++++++++ main \n";
mpz_out_str(stdout, 16, secp.tempx);
std::cout << " \n";
}
void AddDirect(mpz_t p2x, mpz_t p2y)
{
my_class secp;
mpz_t point2x, point2y;
mpz_init_set(point2x, p2x);
mpz_init_set(point2y, p2y);
// ---------------------------
mpz_set(secp.tempx, point2x);
mpz_set(secp.tempy, point2y);
//------------------
std::cout << "adddirect secp.tempx\n";
mpz_out_str(stdout, 16, secp.tempx);
std::cout << " \n";
}
It shouldn't make any difference in this case, but I'm using visual studio 2022 on Windows 10.
Expected/wanted result:
adddirect secp.tempx
c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5
+++++++++++++ main
c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5
Real result:
adddirect secp.tempx
c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5
+++++++++++++ main
79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
Your AddDirect
creates a new my_class
instance called secp
and sets the values in that instance. When the function returns, that instance is destroyed. The instance you've defined in main
is unaffected by any of that.
What you probably want to do is to make AddDirect
a member function in my_class
. Calling that (on an instance of my_class
type) will make it possible to set the member variables.
The class definition could then look like this:
#include <gmp.h>
//
#include <iostream>
class my_class {
public:
const static int CPU_GRP_SIZE = 16;
mpz_t tempx, tempy;
my_class() {
mpz_init_set_str(
tempx,
"79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798",
16);
mpz_init_set_str(
tempy,
"483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8",
16);
}
my_class(const my_class&) = delete;
my_class(my_class&&) = delete;
my_class& operator=(const my_class&) = delete;
my_class& operator=(my_class&&) = delete;
~my_class() {
// free memory when the instance is destroyed:
mpz_clear(tempx);
mpz_clear(tempy);
}
// make this a member function:
void AddDirect(mpz_t p2x, mpz_t p2y);
};
And the implementation of AddDirect
specifically:
void my_class::AddDirect(mpz_t p2x, mpz_t p2y) {
/* unnecessary:
mpz_t point2x, point2y;
mpz_init_set(point2x, p2x);
mpz_init_set(point2y, p2y);
*/
// ---------------------------
// set the member variables in the my_class instance:
mpz_set(tempx, p2x);
mpz_set(tempy, p2y);
//------------------
std::cout << "adddirect tempx\n";
mpz_out_str(stdout, 16, tempx);
std::cout << " \n";
}
In main
you'd then call AddDirect
on the my_class
instance that you've defined in main
:
int main() { // not void main()
my_class secp;
mpz_t point2x, point2y;
mpz_init_set_str(
point2x,
"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5", 16);
mpz_init_set_str(
point2y,
"1ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a", 16);
secp.AddDirect(point2x, point2y); // call the member function to set these
// ^^^^^
// manually free these that aren't needed anymore:
mpz_clear(point2x);
mpz_clear(point2y);
std::cout << "+++++++++++++ main \n";
mpz_out_str(stdout, 16, secp.tempx);
std::cout << " \n";
}