P
pozzugno
I have a structure defined as:
struct MyStruct {
<some fields of different types>
unsigned char buffer[MAX_BUFLEN];
};
The array buffer[] can store different types of variables: int, unsigned int, long, unsigned long, null-terminated strings (of course, MAX_BUFLEN >=sizeof(long)).
Am I allowed to use a cast to long or int, as in the following instructions?
struct MyStruct s;
(long *)s.buffer = -100000L;
(unsigned long *)s.buffer = +100000UL;
(int *)s.buffer = -100;
(unsigned int *)s.buffer = +100U;
long l = *(long *)s.buffer;
unsigned long ul = *(unsigned long)s.buffer;
int i = *(int *)s.buffer;
unsigned int ui = *(unsigned int *)s.buffer;
I think this could work on some platforms, but is not fully portable (mostly on 16- and 32-bits). It would work only if buffer[] field is correctly aligned in the structure, but it's not guaranteed (it may depends from the previous fields in the structure).
I think there are two solutions.
Avoiding to use cast and use memcpy (but I don't like it):
long l = -100000L;
unsigned long ul = 100000UL;
int i = -100;
unsigned int ui = +100;
memcpy(s.buffer, &l, sizeof(l));
memcpy(s.buffer, &ul, sizeof(ul));
memcpy(s.buffer, &i, sizeof(i));
memcpy(s.buffer, &ui, sizeof(ui));
memcpy(&l, s.buffer, sizeof(l));
memcpy(&ul, s.buffer, sizeof(ul));
memcpy(&i, s.buffer, sizeof(i));
memcpy(&ui, s.buffer, sizeof(ui));
Moving the buffer[] field at the top of the structure, so it is aligned forsure and I can use casting.
Another solution could be to define MyStruct as:
struct MyStruct {
<some fields of different types>
struct {
unsigned char buffer[MAX_BUFLEN];
} buf_aligned;
};
Are thre other solutions?
PS: I'm working on an embedded platform, so I don't have dynamic allocationmalloc/free (otherwise buffer could had be defined as a pointer and the memory space could had be allocated dynamically with malloc that surely returns aligned pointers).
struct MyStruct {
<some fields of different types>
unsigned char buffer[MAX_BUFLEN];
};
The array buffer[] can store different types of variables: int, unsigned int, long, unsigned long, null-terminated strings (of course, MAX_BUFLEN >=sizeof(long)).
Am I allowed to use a cast to long or int, as in the following instructions?
struct MyStruct s;
(long *)s.buffer = -100000L;
(unsigned long *)s.buffer = +100000UL;
(int *)s.buffer = -100;
(unsigned int *)s.buffer = +100U;
long l = *(long *)s.buffer;
unsigned long ul = *(unsigned long)s.buffer;
int i = *(int *)s.buffer;
unsigned int ui = *(unsigned int *)s.buffer;
I think this could work on some platforms, but is not fully portable (mostly on 16- and 32-bits). It would work only if buffer[] field is correctly aligned in the structure, but it's not guaranteed (it may depends from the previous fields in the structure).
I think there are two solutions.
Avoiding to use cast and use memcpy (but I don't like it):
long l = -100000L;
unsigned long ul = 100000UL;
int i = -100;
unsigned int ui = +100;
memcpy(s.buffer, &l, sizeof(l));
memcpy(s.buffer, &ul, sizeof(ul));
memcpy(s.buffer, &i, sizeof(i));
memcpy(s.buffer, &ui, sizeof(ui));
memcpy(&l, s.buffer, sizeof(l));
memcpy(&ul, s.buffer, sizeof(ul));
memcpy(&i, s.buffer, sizeof(i));
memcpy(&ui, s.buffer, sizeof(ui));
Moving the buffer[] field at the top of the structure, so it is aligned forsure and I can use casting.
Another solution could be to define MyStruct as:
struct MyStruct {
<some fields of different types>
struct {
unsigned char buffer[MAX_BUFLEN];
} buf_aligned;
};
Are thre other solutions?
PS: I'm working on an embedded platform, so I don't have dynamic allocationmalloc/free (otherwise buffer could had be defined as a pointer and the memory space could had be allocated dynamically with malloc that surely returns aligned pointers).