I need your help regarding the initialization of a data structure member with non-zero value. This is my data structure:
struct sSTESTE_DLL_X1
{
// declare the structure here
double lastT;
unsigned int cnt;
bool clk_n1;
unsigned long long int cnti;
double pwr;
double pwr_n1;
double iref;
// technically, we need initialize only non-zero data members
sSTESTE_DLL_X1() : lastT(0.0), cnt(0), clk_n1(0), cnti(0), pwr(0.0), pwr_n1(0.0), iref(2.0){};
};
The iref param should have a value higher than zero at simulation start. After the start it will be updated according to a specific rule (incremented or decremented by a fixed term, +/- 0.2). I used an output port to check it but I notice that at start the initialization value is zero. However, the increment/decrement part works correctly.
OK, the constructor isn’t called because you’re allocating the struct with malloc(). Constructors are a C++ feature; you need to use new/delete. See attached.
Note: The DMC compiler is a C++ compiler, i.e., you’re really compiling a C++ program, not a C program.
If you do, I suggest that you learn using a more modern compiler toolset. The DMC compiler is no longer maintained and ended with support for C++98/C99 standards.
For people like me with limited knowledge in programming, I have to document the method for future reference. By including this constructor initialization, I have learned from you guys that there are three methods to initialize member variables so far.
struct sDLLVAR {
// declare the structure here
double a; // maybe float was ok but looses precision
double lastT; // type should match t...
// sDLLVAR() : a(1.0), lastT(0.0) {};
sDLLVAR() {
a = 1.0;
lastT = 0.0;
}
};
Again, this example uses a C++ constructor and must be allocated with “new” as before.
Note that the constructor is a function/method. The prior version had an empty function body. Above, we’ve moved the initialization into the function body. We could, in fact, do much more interesting things in the function body…
Continuing with C++, we can also pass a value as a constructor parameter. Let’s say that our component passes in a “gain” value as an attribute named “GainAttr”. We want to store that in our instance data. We could do this:
struct sDLLVAR {
// declare the structure here
double a; // maybe float was ok but looses precision
double lastT; // type should match t...
double gain; // gain from component attribute
sDLLVAR(double gainVal) : a(1.0), lastT(0.0), gain(gainVal) {};
// or
sDLLVAR(double gainVal) : a(1.0), lastT(0.0), { gain = gainVal; };
};
In the initialization, we’d do this:
*opaque = new sDLLVAR(GainAttr);
We could do more by adding a “destructor” that’s called when “delete inst;” is invoked. That would be useful to (for example) open a file in the constructor and close it in the destructor. But I’ll stop here – I imagine that I’ve muddied the water enough.
Sure. I use Visual Studio 2022 Community Edition (free from Microsoft) and MinGW (also free). VS includes a (possibly overwhelmingly complete) IDE with debugger and compiler. MinGW is a compiler-only toolset and does not have an IDE. You can use VSCode (a free simplified IDE from Microsoft; IDE only but supports many compiler toolsets for many platforms via plug-ins) with both compilers.