只有静态成员的类的内存布局? [英] Memory layout of a class with only static members?

查看:80
本文介绍了只有静态成员的类的内存布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的东西


class Params {

public:

const static char nOne = 1;

const static int nTwo = 2;

const static char nThree = 3;

};


这个只是嵌入式

系统中全局使用参数的包装器。


现在,我需要在某些Flash中对这些数据进行二进制复制。

所以,我需要的是起始地址和数据的大小。


不幸的是,sizeof(Params)给出了1.


我在开头和结尾添加了虚拟变量,可以使用

来检索信息。


class Params {

public:

const static char nBegin = 0;


const static char nOne = 1;

const static int nTwo = 2;

const static char nThree = 3;


//可以在nEnd之前添加新参数


const static char nEnd = 0;

};

我认为开头是& Params :: nBegin,所有数据的大小都是

& Params :: nEnd - & Params :: nBegin + sizeof(Params :: nEnd)。


它适用于上面的这个小测试类。但是我真正的班级包含了更多的数据,VS中的测试程序将它们分类在内存中,不同的是
。在我的例子中,nBegin和nEnd放在一起。我认为对于

内存对齐的原因。

然后我将课程放在一些特殊的部分,似乎所有内容都在工作。


我的问题:


它现在只是偶然起作用还是保证?


有没有更好的方法来解决问题而不改变班级的总体布局。在我的实际应用程序中,参数是

已经引用了100次。


感谢您的评论。

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.

推荐答案

我不知道编译器关于静态成员的

布局的内部工作原理,但是......
I don''t know about the inner workings of the compiler regarding the
layout of static members, however...

现在,我需要在某些Flash中对这些数据进行二进制复制。
Now, I need to do a binary copy of this data to and from some Flash.



你已经拥有至少两个Params实例:在flash中,我猜你的程序内存中的
猜测。使用静态,你只得到一个
实例的东西,但看起来你有更多。


你可以尝试这样:
< br $>
struct Params

{

int p1;

int p2;

// ...

};


静态Params& FlashParams =

* reinterpret_cast< Params *>(0xYOUR_PARAMS_IN_FLASH_ ADDRESS_HERE);


void Test()

{

int copy1 = FlashParams.p1;

int copy2 = FlashParams.p2;

Params RamParams = FlashParams;

//等等...

}


嘿,打字方式,它甚至比Params :: p1更短!

当然,你需要注意不要在FlashParams的

初始化之前调用Test()(在我的
行的工作中非常不寻常的情况,不知道你的。)

You already have at least two instances of Params: in flash and, I
guess, in your program''s memory. Using static, you only get one
instance of the thing, but it seems you have more.

You may try like this:

struct Params
{
int p1;
int p2;
// ...
};

static Params& FlashParams =
*reinterpret_cast<Params*>(0xYOUR_PARAMS_IN_FLASH_ ADDRESS_HERE);

void Test()
{
int copy1 = FlashParams.p1;
int copy2 = FlashParams.p2;
Params RamParams = FlashParams;
// etc...
}

Hey, typing-wise, it''s even shorter than Params::p1!

Of course, you need to take care not to call Test() before the
initialization of FlashParams (highly unusual situation in my line of
work, don''t know about yours).




Goran schrieb:

Goran schrieb:

我不知道编译器关于静态成员的

布局的内部工作原理,但是......
I don''t know about the inner workings of the compiler regarding the
layout of static members, however...

现在,我需要在某些Flash中对这些数据进行二进制复制。
Now, I need to do a binary copy of this data to and from some Flash.



你已经拥有至少两个Params实例:在flash中,我猜测你的程序内存中的
猜测。使用静态,你只得到一个物品的
实例,但似乎你有更多。


You already have at least two instances of Params: in flash and, I
guess, in your program''s memory. Using static, you only get one
instance of the thing, but it seems you have more.



我宣称会员是静态的,因为程序不能更改




实际上我的程序存储器中只有一个实例。这些是

默认参数。但是现在我们需要某种参数更新

机制。在Flash中,只有二进制数据应该被复制到静态参数所在的内存位置。这不是另外一个参数实例,而是纯二进制数据(当然是

正确的格式)。

I declared the members as static because the program must not change
them.

Actually I have only 1 instance in my program memory. These are the
default parameters. But now we need some kind of parameter update
mechanism. In Flash there is just binary data that shall be copied to
the memory location where the static parameters reside. It''s not
another parameter instance but plain binary data (of course in the
right format).


您可以这样尝试:


struct Params

{

int p1;

int p2;

// ...

};


静态Params& FlashParams =

* reinterpret_cast< Params *>(0xYOUR_PARAMS_IN_FLASH_ ADDRESS_HERE);


void Test()

{

int copy1 = FlashParams.p1;

int copy2 = FlashParams.p2;

Params RamParams = FlashParams;

//等...

}
You may try like this:

struct Params
{
int p1;
int p2;
// ...
};

static Params& FlashParams =
*reinterpret_cast<Params*>(0xYOUR_PARAMS_IN_FLASH_ ADDRESS_HERE);

void Test()
{
int copy1 = FlashParams.p1;
int copy2 = FlashParams.p2;
Params RamParams = FlashParams;
// etc...
}



这是我的另一个解决方案,但后来我必须更改所有代码

参数被引用。在我这样做之前,我想确保

没有更简单的解决方案。当然,我想知道标准对这个问题说的是什么


That was my other solution but then I have to change all the code where
parameters are referenced. Before I do this I want to be sure that
there are no easier solutions. And, of course I would like to know what
the standard says about this issue.


嘿,打字方式,它''甚至比Params :: p1短!
Hey, typing-wise, it''s even shorter than Params::p1!



很久以前我决定使用带有静态成员的类接近

,因为它保存了全局对象(静态参数...)。这看起来好像

这是错误的决定......:o)


问候


Henryk

Long time ago I decided to use the "class with static members" approach
because it saves the global object (static Params ...). It looks like
this was somehow the wrong decision... :o)

Greetings

Henryk


Henryk写道:
Henryk wrote:

我有类似的东西


class Params {

public:

const static char nOne = 1;

const static int nTwo = 2;

const static char nThree = 3;

};


这只是嵌入式全局使用参数的包装器

系统。


现在,我需要在某些Flash中对这些数据进行二进制复制。

所以,我需要的是起始地址和数据大小。
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.



假设不正确。在此之后,您假设1(一)个起始地址和一个连续的

内存块。没有理由这样做。事实上,对于

对齐

的目的,通常最好将字符存储在一起,一起使用



etctera。订单nTwo,nOne,nThree通常是最紧凑的。


HTH,

Michiel Salters

Incorrect assumption. You assume 1 (one) start address and a contiguous
block of memory after that. There is no reason for that. In fact, for
alignment
purposes, it often is better to store the chars together, the ints
together,
etctera. The order nTwo, nOne, nThree often is the most compact.

HTH,
Michiel Salters


这篇关于只有静态成员的类的内存布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆