#### Archived

This topic is now archived and is closed to further replies.

# Shared memory space for 2 variables...

This topic is 6146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, I''m learning sockets, and was trying to develop a simple method to send data packets. Let''s see if i can explain this, I would like to have a memmory area that is shared by both a structure and a char[256] when sending, i send a packet of 256 chars, but if i could write my structure on top of the same memmory the char[256] ocupies, i would actually be sending my strcuture... so, what i want is to write into a structure, that ocupies the same memmory space than a char[256] declared variable, which is the one who will get sent in the socket functions... Hope i could make myself clear, Season''s Greetings,

##### Share on other sites
  struct MyStruct{ // Whatever ...};union SharedSpace{ char data[256]; struct MyStruct p;};

Be careful, though ... there may be some issues with regards to struct padding.

##### Share on other sites
The way I''d do this is very similar to what Martee suggested; I''d just use a nameless union.

##### Share on other sites
Wait, why would you want to use the union? If you want to send a structure then qx452''s example is what you want. If you always want to send exactly 256 bytes (why would you want to do that?) then use the union, or something like the following:

char buffer[256];
struct my_struct *struct_data = (struct my_struct *)buffer;

// fill in your struct data
struct_data->foo = 1;

// send the buffer
send(sock, buffer, 256);

I guess it''s personal preference as to which way you think is "cleaner" .. this way, or a union. At least this way is very explicit what you are doing (trying a generic buffer as a specific data type).

-Brannon

##### Share on other sites
Unions? What are unions? In the two C++ books i''ve read (styC++21, sams C++ Unleashed) they have not even mentioned Unions. I understand pentium3id''s method, but i don''t get the other one. Could someone explain?

Dan

RELIGION IS THE ROOT OF ALL EVIL

##### Share on other sites
A union is like a structure, except that each "member" occupies the same physical space. The union itself is as large as the largest member (padded out to whatever the current pack size is).

That''s really all there is to it.

Unions look like this:

union my_union
{
char a;
long b;
char *c;
};

union my_union x;
// these are the same (more or less)
x.b = 0;
x.c = NULL;

A common use for unions is something like this:

struct my_struct
{
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
};

union my_union
{
struct my_struct bytes;
unsigned long dword;
};

and then:
union my_union x;

x.bytes.a = 0;
x.bytes.b = 1;
x.bytes.c = 2;
x.bytes.d = 3;

printf("0x%08X\n", x.bytes.dword);
will print out:
0x00010203
[or (0x03020100), i''m too lazy to think of which one exactly, you get the point).

does that help?

-Brannon

##### Share on other sites
Yes, thank you i understand now, but i still don''t see what the point is. I kinda understand the example, but i just can''t think of a time such a capability was necessary. Some more examples would be great.

RELIGION IS THE ROOT OF ALL EVIL

##### Share on other sites
  union LARGE_INT{__int64 i64;int i32[2];short i16[4];char i8[8];};

You can use LARGE_INT as an __int64 or access it as seperate DWORDs, WORDs, or BYTEs. It''s often useful when working with compound types and with streams (e.g. networking).

So is this, which I only recently discovered:
  struct BitFieldDefintions{char OneBit:1;chat TwoBits:2;DWORD TwentyFourBits:24;};

1. 1
Rutin
28
2. 2
3. 3
4. 4
5. 5

• 13
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009410
• ### Who's Online (See full list)

There are no registered users currently online

×