Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
My code has a struture & union as below:
C++
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;
}

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.
C++
UTagModalDB UEsim;
UEsim.S = *Esim;

But all values inside UEsim.S are showing zeroes. eg: UEsim.S.f[20] = 0.0000 whereas Esim->f[20] is 4.5534

I dont know what is wrong in the code. Anyone, Please let me know how to copy the entire Esim to UEsim.S

Please note that the structure TagModalDB mentioned above is shown as an example. The original structure TagModalDB in my code is big which sizes to 862492bytes.
Posted
Updated 17-Sep-13 18:27pm
v6
Comments
Richard MacCutchan 13-Sep-13 12:47pm    
In both your memset and memcpy you have no size specified for the operation. Please edit your question and show the actual code that you are using.

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

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:
C++
union UTest
{
    struct STest S;
    char Buffer[sizeof(struct STest)];
};


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.
 
Share this answer
 
v3
Comments
[no name] 13-Sep-13 19:41pm    
The OP has given no use for Buffer other than copy so this covers everything.
CPallini 17-Sep-13 3:12am    
5.
Are you using the debugger to check the values?

C++
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();


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 -
C++
(stest *) &UEsim.S
 
Share this answer
 
v6
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.
 
Share this answer
 
OK, I would define the Buffer within the union as char *Buffer instead.

Then, your memcpy needs to be memcpy(UESim.Buffer, SE, sizeof(struct STest)).

Try that and see if it works.
 
Share this answer
 
Comments
Peter_in_2780 13-Sep-13 10:32am    
No! OP's intention seems to be having a char array overlaying the struct in the union. Your suggestion would replace that with a (small) pointer, which is almost certainly not what is wanted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900