如何复制结构数据联合 [英] How to copy a structure data union

查看:76
本文介绍了如何复制结构数据联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有一个struture&联盟如下:

My code has a struture & union as below:

struct TagModalDB 
{
 char c[4000];
 float f[5000];
 int i[6000];
}
 
union UTagModalDB
{
struct TagModalDB S;
char Buffer[sizeof(struct TagModalDB)];
};  
struct TagModalDB Esim is declared globally. Esim is mapped to a shared memory
Esim = (struct TagModalDB *) MapViewOfFile(htModelDB,FILE_MAP_ALL_ACCESS,0,0,0);
if(!Esim)
{
   AfxMessageBox("Unable to MapViewOfFile");
   return FALSE;
}



此Esim初始化后包含共享内存中的值。我添加了以下代码,将整个值从Esim复制到UEsim。


After this Esim is initialized and contains values from shared memory. I added the below code to copy the entire values from Esim to the UEsim.

UTagModalDB UEsim;
UEsim.S = *Esim;



但UEsim.S内的所有值都显示为零。例如:UEsim.S.f [20] = 0.0000而Esim-> f [20]是4.5534



我不知道代码中有什么问题。任何人,请让我知道如何将整个Esim复制到UEsim.S


请注意,上面提到的结构TagModalDB作为示例显示。我的代码中的原始结构TagModalDB很大,大小为862492bytes。

推荐答案

因为POD结构可以直接复制,为什么不使用更简单的代码:

Since POD struct can be copied directly, why not uses the simpler code:
UESim.S = *SE;



这通常更安全以这种方式编写代码,因为您不必费心去了解结构是否可按位复制,因为编译器将执行正确的操作(对于没有重载赋值运算符的任何成员的按位复制)。



如果没有一个字段有重载赋值运算符,我们可以期望编译器足够聪明,可以立即复制整个结构。



如果代码是普通的C,那么上述代码没有理由为手动调用 memcpy 的速度慢。



但是在其他方面,联盟内部的结构必须与全局结构的类型相同。最好的方法是修改这样的联合:


This is generally safer to write the code that way as you don't have to bother to know if the structure is bitwise copyable as the compiler will do the proper thing (bitwise copy for any member that do not have overloaded assignment operator).

If none of the field have overloadded assignment operator, the we can expect that the compiler will be smart enough to copy the whole structure at once.

If the code is plain C, then there are no reason why above code would be slower that calling memcpy manually.

But in other to do that, the structure Inside the union must be of the same type as the global structure. The best way to do this would be to modify the union like this:

union UTest
{
    struct STest S;
    char Buffer[sizeof(struct STest)];
};





修改后问题的附加评论



您已经以一种难以理解现有答案的方式修改了问题。基本上考虑到你做了重要的修改,最好在原始问题的末尾附加它(正如我在这里所做的那样)。



如前所述回答4,问题可能在于代码中没有显示,所以很难帮助你。使用调试器可能有助于确定内容或指针是否在意外时间被修改。



一种可能解释奇怪行为的可能性可能是变量冲突局部变量与全局变量同名,或者名称稍有不同。例如,您确定没有 ESim Esim 变量,如上面的代码所示。这可以解释为什么它无法正常工作。



Additionnal comments for modified question

You have modified the question in a way that it is hard to make sense of some existing answers. Essentially given that you have made important modifications, it would have been better to append it at the end of original question (as I have done here).

As mentionned in answer 4, the problem is probably in code that is not shown anyway so it is hard to help you. Using a debugger might help to figure out if the content or the pointer are modified at unexpected time.

One possibility that might explain the strange behavior might be a variable conflict where a local variable has the same name as a global variable or where name differ just a bit. For example, are you sure that you don't have ESim and Esim variables as shown in your code above. That can explain why it does not works properly.


您是否使用调试器来检查值?



Are you using the debugger to check the values?

typedef struct STest 
 {
 int a[1000];
 float b[1000];
 char c[1000];

 } stest;

 union UTest
 {
 stest S;
 char Buffer[sizeof(stest)];
 };

 stest *SE = new stest;

 SE->a[0] = 2;
 SE->b[25] = 2.2f;

 union UTest UEsim;

 UEsim.S.a[0] = 1;

 cout << UEsim.S.a[0] << endl;

 //memcpy(UEsim.Buffer, SE, sizeof(stest));

 UEsim.S = *SE;

 cout << UEsim.S.a[0] << endl;
 cout << UEsim.S.b[25] << endl;

_getch();





使用此代码和VS2010,结果输出正确。当我在调试器中检查union值时,UEsim.Buffer中的值看起来没问题,但是UEsim.S中的值无法解析而产生错误。在观察窗口中评估此表达式确认了副本 -



Using this code and VS2010 the results output correctly. When I check the union values in the debugger the values in UEsim.Buffer look OK but the values in UEsim.S cannot be resolved giving an error. Evaluating this expression in the watch window confirmed the copy -

(stest *) &UEsim.S

有各种可能的原因,但是从您显示的几行代码中都看不到。



1.您是否修改了内存缓冲区映射文件和复制之间? E. g。你打电话给Unmap吗?你有没有在复制之前确认正确的值确实在映射的缓冲区中?



2.你确定你真正读到了正确的值吗?你使用什么方法 - 如果你从代码中打印出来,请显示你的代码。如果您使用调试器,请告诉我们您将如何查看值。您可能找错了地方。



3.是否还有其他代码可能会再次覆盖缓冲区?



如果以上都没有帮助,那么在Map函数和读取缓冲区内容的那一刻之间显示代码可能有助于解决问题。中间任何地方都可能存在问题。
There are various possible reasons, but none that can be seen from the few lines of code you have shown.

1. Have you modified the memory buffer between mapping the file and copying? E. g. did you call Unmap? Have you verified that the correct values are indeed in the mapped buffer immediately before copying?

2. Are you sure you're really reading the correct values? What method are you using - if you print it out from code, please show your code. If you use the debugger, tell us how you go about to view the values. You may be looking in the wrong place.

3. Is there any other code that may overwrite the buffer again?

If none of the above helps, showing the code between the Map function and the moment you read the contents of the buffer might help in resolving the issue. There may be a problem anywhere in-between.


这篇关于如何复制结构数据联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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