On Sat, 06 Oct 2007 19:43:45 +0200, Win Sock wrote:
Hi All,
I need one clarification regarding memory allocation for structure. The
details are as given below :
let us consider one structure
struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);
When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0 or
len1 and pointer1 .
Am I having a dejÃ*-vu, or was this identical question already
posted here a few weeks ago?
What are you trying to do?
I think that you could allocate space for len0 and pointer0 by
doing malloc(offsetof(struct foo, len1)), but I don't have the
courage to try it on my DS9K. And anyway, there is probably a
*much* better way to do what you ultimately need.
As for len1 and pointer1, in some cases
unsigned char *realptr = malloc(sizeof struct unnamed
- offsetof(struct unnamed, len1));
struct unnamed *fakeptr = realptr - offsetof(struct unnamed, len1);
could work, but there will be big problems if e.g. realptr happens
to be at the beginning of a segment, or whatever, so *don't do
that*.
I repeat, what you are trying to do? It is *very* likely that you
picked the wrong path towards that goal.
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.