H
Henryk
I have something like
class Params {
public:
const static char nOne = 1;
const static int nTwo = 2;
const static char nThree = 3;
};
This is just a wrapper for globally used parameters in an embedded
system.
Now, I need to do a binary copy of this data to and from some Flash.
So, what I need is the start address and the size of the data.
Unfortunatelly, sizeof(Params) gives 1.
I added dummy variables at the beginning and the end that can be used
to retreive the information.
class Params {
public:
const static char nBegin = 0;
const static char nOne = 1;
const static int nTwo = 2;
const static char nThree = 3;
// new parameters can be added before nEnd
const static char nEnd = 0;
};
I thought that the begin is &Params::nBegin and the size of all data is
&Params::nEnd - &Params::nBegin + sizeof(Params::nEnd).
It works for this little test class above. But my real class contains
much more data and a test program in VS sorts them in memory
differently. In my case nBegin and nEnd are put together. I think for
memory alignment reasons.
Then I put the class in some special section and everything seems to
work.
My questions:
Does it now work only by accident or is this guarantied?
Are there any better ways to solve the problem without changing the
general layout of the class. In my real application the parameters are
referenced already 100s of times.
Thank you for your comments.
class Params {
public:
const static char nOne = 1;
const static int nTwo = 2;
const static char nThree = 3;
};
This is just a wrapper for globally used parameters in an embedded
system.
Now, I need to do a binary copy of this data to and from some Flash.
So, what I need is the start address and the size of the data.
Unfortunatelly, sizeof(Params) gives 1.
I added dummy variables at the beginning and the end that can be used
to retreive the information.
class Params {
public:
const static char nBegin = 0;
const static char nOne = 1;
const static int nTwo = 2;
const static char nThree = 3;
// new parameters can be added before nEnd
const static char nEnd = 0;
};
I thought that the begin is &Params::nBegin and the size of all data is
&Params::nEnd - &Params::nBegin + sizeof(Params::nEnd).
It works for this little test class above. But my real class contains
much more data and a test program in VS sorts them in memory
differently. In my case nBegin and nEnd are put together. I think for
memory alignment reasons.
Then I put the class in some special section and everything seems to
work.
My questions:
Does it now work only by accident or is this guarantied?
Are there any better ways to solve the problem without changing the
general layout of the class. In my real application the parameters are
referenced already 100s of times.
Thank you for your comments.