Shared memory space for 2 variables...

Started by
6 comments, last by Prozak 22 years, 3 months ago
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,

[Hugo Ferreira][Positronic Dreams][]

Advertisement
  struct MyStruct{	// Whatever ...};union SharedSpace{	char data[256];	struct MyStruct p;};  

Be careful, though ... there may be some issues with regards to struct padding.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
The way I''d do this is very similar to what Martee suggested; I''d just use a nameless union.
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
-Brannon
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
Ambassador: Mr. Bush are you stoned or just really, REALLY dumb?Pres. Bush - I assure you I am not stoned.
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
-Brannon
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
Ambassador: Mr. Bush are you stoned or just really, REALLY dumb?Pres. Bush - I assure you I am not stoned.
  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;};  
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement